问题 如何在类级变量中使用Spring @Value批注


我需要使用注入参数 @Value 在类的实例变量中,可以在其所有子类中重用该变量。

   @Value(server.environment)
   public String environment;

   public String fileName = environment + "SomeFileName.xls";

这里的问题是fileName首先初始化然后发生环境注入。所以我总是得到null-SomeFileName.xls。

无论如何要先传达初始化 @Value 在春天。


9307
2017-08-12 06:06


起源



答案:


您可以使用 @PostConstruct 因此。从 文件

PostConstruct注释用于需要的方法   在完成依赖注入以执行任何操作之后执行   初始化。

@PostConstruct 允许您在设置属性后执行修改。一个解决方案是这样的:

public class MyService {

    @Value("${myProperty}")
    private String propertyValue;

    @PostConstruct
    public void init() {
        this.propertyValue += "/SomeFileName.xls";
    }

}

另一种方法是使用 @Autowired 配置法。从 文件

标记构造函数,字段,setter方法或配置方法   由Spring的依赖注入工具自动启动。

...

配置方法可以有任意名称和任意数量的参数;   每个参数都将使用匹配的bean自动装配   弹簧容器。 Bean属性setter方法实际上只是一个   这种通用配置方法的特例。这样的配置方法呢   不必公开。

例:

public class MyService {

    private String propertyValue;

    @Autowired
    public void initProperty(@Value("${myProperty}") String propertyValue) {
        this.propertyValue = propertyValue + "/SomeFileName.xls";
    }

}

区别在于,对于第二种方法,您没有额外的bean挂钩,您可以在自动装配时对其进行调整。


16
2017-08-12 06:13



非常感谢。它的工作正常。我用了第二种方法。只是它期待b / w @Value(“$ {myProperty}”)和propertyValue的返回类型。添加相同和工作正常。 - Paramesh Korrakuti
你是对的,它错过了类型,只是纠正了它,谢谢你的指点! - Francisco Spaeth
我看到你的解决方案包含方法级别如何在类级别使用该属性? <code> @Document(indexName =“somevalue”,type =“applicationstore”)@JsonIgnoreProperties(ignoreUnknown = true)public class ESApplicationEntity {</ code> ...需要从属性中获取某些值 - stallion


答案:


您可以使用 @PostConstruct 因此。从 文件

PostConstruct注释用于需要的方法   在完成依赖注入以执行任何操作之后执行   初始化。

@PostConstruct 允许您在设置属性后执行修改。一个解决方案是这样的:

public class MyService {

    @Value("${myProperty}")
    private String propertyValue;

    @PostConstruct
    public void init() {
        this.propertyValue += "/SomeFileName.xls";
    }

}

另一种方法是使用 @Autowired 配置法。从 文件

标记构造函数,字段,setter方法或配置方法   由Spring的依赖注入工具自动启动。

...

配置方法可以有任意名称和任意数量的参数;   每个参数都将使用匹配的bean自动装配   弹簧容器。 Bean属性setter方法实际上只是一个   这种通用配置方法的特例。这样的配置方法呢   不必公开。

例:

public class MyService {

    private String propertyValue;

    @Autowired
    public void initProperty(@Value("${myProperty}") String propertyValue) {
        this.propertyValue = propertyValue + "/SomeFileName.xls";
    }

}

区别在于,对于第二种方法,您没有额外的bean挂钩,您可以在自动装配时对其进行调整。


16
2017-08-12 06:13



非常感谢。它的工作正常。我用了第二种方法。只是它期待b / w @Value(“$ {myProperty}”)和propertyValue的返回类型。添加相同和工作正常。 - Paramesh Korrakuti
你是对的,它错过了类型,只是纠正了它,谢谢你的指点! - Francisco Spaeth
我看到你的解决方案包含方法级别如何在类级别使用该属性? <code> @Document(indexName =“somevalue”,type =“applicationstore”)@JsonIgnoreProperties(ignoreUnknown = true)public class ESApplicationEntity {</ code> ...需要从属性中获取某些值 - stallion


您可以使用@Value从属性文件中读取值,这听起来更像是您希望实现的内容。

如果在xml或bean配置方法中配置PropertySourcesPlaceholderConfigurer,则spring将为您设置值。

@Value("${server.env}")
private String serverEnv;

和配置....

@Configuration
public class Cfg {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    final PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("/foo.properties"));
    return propertySourcesPlaceholderConfigurer;
    }
}

或xml方法

<context:property-placeholder location="classpath*:foo.properties"/>

0
2017-08-12 15:14



工作@Value的配置已经完成,并为我工作。如果变量初始化,则问题是顺序。弗朗西斯科的方法对我来说很好。无论如何,谢谢你的时间。 - Paramesh Korrakuti