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?