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?
ziggy
  • 15,677
  • 67
  • 194
  • 287

2 Answers2

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);
    } 
}
Community
  • 1
  • 1
John Farrelly
  • 7,289
  • 9
  • 42
  • 52
  • 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 Jan 01 '13 at 17:31
  • @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? – John Farrelly Jan 01 '13 at 17:35
  • No you are correct in that the @Value values are never set so they are null before they get to the System.setProperty calls. – ziggy Jan 01 '13 at 18:06
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

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • 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. – ziggy Jan 06 '13 at 01:35