如何在类级变量中使用 Spring @Value 注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25257237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:59:04  来源:igfitidea点击:

How to use Spring @Value annotation in class level variables

springannotations

提问by Paramesh Korrakuti

I need to use injected parameter by @Valuein instance variable of a class and can be reused that variable in all its child classes.

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

   @Value(server.environment)
   public String environment;

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

Here, the problem is fileName initializing first and then environment injection is happening. So I am getting always null-SomeFileName.xls.

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

Anyway to convey to initialize first @Valuein spring.

无论如何要@Value在spring中首先传达初始化。

回答by Francisco Spaeth

You can use @PostConstructtherefore. From documentation:

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

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

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

@PostConstructallows you to perform modification after properties were set. One solution would be something like this:

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

public class MyService {

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

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

}

Another way would be using an @Autowiredconfig-method. From documentation:

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

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

...

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.

将构造函数、字段、setter 方法或配置方法标记为由 Spring 的依赖注入设施自动装配。

...

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

Example:

例子:

public class MyService {

    private String propertyValue;

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

}

The difference is that with the second approach you don't have an additional hook to your bean, you adapt it as it is being autowired.

不同之处在于,使用第二种方法,您的 bean 没有额外的钩子,您可以在它自动装配时对其进行调整。

回答by msduk

You can use @Value to read in values from properties files which sounds more like something you are looking to achieve.

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

If you configure PropertySourcesPlaceholderConfigurer in the xml or bean configuration method the value will get set by spring for you.

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

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

And the configuration....

还有配置....

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

or the xml approach

或 xml 方法

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