-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'注解你的类。 –