20

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.

4 Answers 4

30

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. Its working fine. I used second approach. Just its expecting return type in b/w @Value("${myProperty}") & propertyValue. Added the same and working fine.
You are right, its missing the type, just corrected it, thanks for pointing it!
I see your solution contains for method level How to use the property at class level? <code> @Document(indexName = "somevalue", type = "applicationstore") @JsonIgnoreProperties(ignoreUnknown = true) public class ESApplicationEntity { </code>... some value needs to be fetched from property
0

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"/>

1 Comment

The configuration to work @Value is already done and working for me. The problem is order if variable initialization. Francisco's approach is working fine for me. Anyhow, thanks for your time.
0

You could also evict the @PostConstruct with pure :

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

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

Comments

0

Make your class implements InitializingBean, and override the method afterPropertiesSet()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.