什么配置支持@Value注释的求值

  • 本文关键字:注释 @Value 配置 支持 spring
  • 更新时间 :
  • 英文 :


我想做一个非常小的基于编程/注释的Spring配置,做一些命令行的东西,我希望能够从系统属性注入一些bean值的值。

我像这样使用@Value:

@Value("${MigrateDb.task:default}")
private String task;

它有点工作,但它不是评估值定义,我只是得到"${MigrateDb。在实际字段中,而不是Spring计算它并给我migration .db.task系统属性的值(或默认值)。

我需要添加什么到我的Configuration类来启用此行为?

试着这样使用:

@Value("${MigrateDb.task:default}")
private String task;

XML配置:

<context:property-placeholder
        location="your.filelocation.properties" />`
Java Config:
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setLocation(new ClassPathResource("file.properties"));
    return propertyPlaceholderConfigurer;
}

从shadowwray的回答来看,启用请求行为的最小代码是:

  @Bean
  public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
      return new PropertyPlaceholderConfigurer();
  }

方法应该是静态的,按照https://stackoverflow.com/a/14943106/924597

最新更新