-2
我試圖注入值這樣的字段:春天@ConfigurationProperties不注射任何東西,場保持零
@Component
@ConfigurationProperties("paypal")
public class Paypal
{
private List<List<List<String>>> deleterequest;
public List<List<List<String>>> getDeleterequest()
{
return deleterequest;
}
public void setDeleterequest(List<List<List<String>>> deleterequest)
{
this.deleterequest = deleterequest;
}
private String date;
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
}
注:貝寶豆本身加載成功,而不是空。 使用@Value讀取值在該類中正常工作。我的主類,也標註有:
@EnableConfigurationProperties
但日期和deleterequest在此代碼保持空:
@Autowired
private Paypal propsPayPal;
this.propsPayPal.getDeleterequest(); //returns null (no NPE!)
this.propsPayPal.getDate(); //returns null (no NPE!)
在Spring的Java配置我寫道:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{
PropertySourcesPlaceholderConfigurer o = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource(Constants.PROPERTIES_FILE_NAME));
o.setProperties(yaml.getObject());
return o;
}
@Bean
public Paypal paypal()
{
return new Paypal();
}
和我application.yml看起來像這樣:
paypal:
date: Datum
deleterequest:
-
-
- Typ
- Allgemeine Abbuchung
-
- Status
- Abgeschlossen
任何幫助?
編輯:我已經想通了,當我創建一個新的Spring項目(只是爲了測試這個問題)與AnnotationConfigApplicationContext(這是在我的主程序使用)作爲背景,它不注入任何值。使用ConfigurableApplicationContext(通過.SpringApplication.run(...))工作得很好,變量將被注入。但我必須讓它在AnnotationConfigApplicationContext上工作。
您正在使用Spring Boot,因此您應該使用'@ PropertySource'而不是另一個'PropertySourcesPlaceholderConfigurer',因爲它已被Spring Boot加載。而且你不應該自己創建bean,Spring Boot會使用'@EnableConfgurationProperties(PayPal.class)'爲你創建它。 –
@ M.Deinum所以我必須刪除PropertySourcesPlaceholderConfigurer Bean,並添加PropertySource註釋在哪裏? –
用'@ SpringBootApplication'註解你的類。 –