1

I have a Spring MVC that uses an external library which i have no access to the code. This external library reads some properties using standard system.getProperty calls. I have to set these values before i use the service.

As my application is a Spring MVC application, i am not sure how to initialise these properties. Here is what i have done so far but i for some reason the values are always null.

I put the properties in a properties file /conf/config.properties

my.user=myuser
my.password=mypassowrd
my.connection=(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx.xxxx.xxxx)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xxx.xxx.xxx)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myService)))

I then added the following two lines in my applicationContext.xml

<context:annotation-config/>
<context:property-placeholder location="classpath*:conf/config.properties"/>    

I read the documentation that to setup up initialisation code, you can implement the InitializingBean interface so i implemented the interface and implemented the afterPropertiesSet() method.

private static @Value("${my.user}") String username;
private static @Value("${my.password}") String password;
private static @Value("${my.connection}") String connectionString;  

@Override
    public void afterPropertiesSet() throws Exception {     
        System.setProperty("username",username);
        System.setProperty("password",password);
        System.setProperty("connectionString",connectionString);
    } 

The problem is that the values are always null when the afterPropertiesSet() method is called.

  • Is the above approach the correct way of initializing code especially for controllers? What happens if a second call is made to the Controller?
  • Are the values null because of the initialisation? i.e. spring has not set them yet?
  • Is it possible to add the initialisation code away from the Controller?

2 Answers 2

2

Are you sure that the definition of your bean/controller is in the same spring context config file as where you have the property-placeholder definition?

Have a look at Boris' answer to this question: Spring @Value annotation in @Controller class not evaluating to value inside properties file

If you wanted to move your code from your controller, you could add a component that listens for when spring has finished initializing, and hen calls the code:

@Component
public class ApplicationStartedListener implements ApplicationListener<ContextRefreshedEvent> {

    private static @Value("${my.user}") String username;
    private static @Value("${my.password}") String password;
    private static @Value("${my.connection}") String connectionString;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.setProperty("username",username);
        System.setProperty("password",password);
        System.setProperty("connectionString",connectionString);
    } 
}
Sign up to request clarification or add additional context in comments.

3 Comments

I only have the one applicationContext.xml file located in the WEB-INF folder. The System.setProperty calls are all in the Controller's no-arg constructor. Maybe that is what is causing the problem.
@ziggy I was assuming from your question that all the @Value variables were null. Are you saying that their values are filled in from the config file properly, but that System.setProperty() isn't setting the values?
No you are correct in that the @Value values are never set so they are null before they get to the System.setProperty calls.
1

The fix should be fairly simple, just remove the static modifier from your fields, then the AutoWiredAnnotationPostProcessor which is responsible for injecting in fields annotated with @AuotWired and @Value, will be able to inject the value in correctly and your afterPropertiesSet should get called cleanly

1 Comment

That didnt seem to resolve it. It was wrong to use static variables so i changed them to instance variables but the properties are still null.

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.