简体   繁体   中英

How to use Spring @Value annotation in class level variables

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

   @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.

Anyway to convey to initialize first @Value in spring.

You can use @PostConstruct therefore. From documentation :

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

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

public class MyService {

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

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

}

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

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.

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.

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

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

@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

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

You could also evict the @PostConstruct with pure :

@Value("${server.environment}")
public String environment;

@Value("#{${server.environment} + 'SomeFileName.xls'}")
public String fileName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM