Spring @ConfigurationProperties 不注入任何东西,字段保持为空
Spring @ConfigurationProperties doesn't inject anything, field stays null
我正在尝试向这样的字段中注入值:
@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;
}
}
注意:Paypal bean 本身已成功加载且不为空。 在 class 中使用 @Value 读取值工作正常。我的主 class 也被注释为:
@EnableConfigurationProperties
但是 date 和 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
有什么帮助吗?
编辑:我发现当我使用 AnnotationConfigApplicationContext(在我的主程序)作为上下文,它不注入任何值。使用 ConfigurableApplicationContext(通过。SpringApplication.run(...))工作得很好,变量将被注入。不过,我必须让它在 AnnotationConfigApplicationContext 上工作。
解决方案是切换到 ConfigurableApplicationContext。
我使用了不支持此功能的 AnnotationConfigApplicationContext。
我正在尝试向这样的字段中注入值:
@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;
}
}
注意:Paypal bean 本身已成功加载且不为空。 在 class 中使用 @Value 读取值工作正常。我的主 class 也被注释为:
@EnableConfigurationProperties
但是 date 和 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
有什么帮助吗?
编辑:我发现当我使用 AnnotationConfigApplicationContext(在我的主程序)作为上下文,它不注入任何值。使用 ConfigurableApplicationContext(通过。SpringApplication.run(...))工作得很好,变量将被注入。不过,我必须让它在 AnnotationConfigApplicationContext 上工作。
解决方案是切换到 ConfigurableApplicationContext。 我使用了不支持此功能的 AnnotationConfigApplicationContext。