0

Main question:

Could someone advise how I use Spring to retrieve a value in my Java code to get values out of whatever properties file is specified by the context:property-placeholder tag in the applicationContext.xml?

Detailed description:

I am very new to Spring. Trying to use it to retreive configurable properties for my application to connect to a (configurable) JMS queue.

I have a Java/J2EE web application, using Spring.

In the src/main/resources/applicationContext.xml I have the following line:

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

Then in the src/main/resources/myapp.properties file I have the following lines:

myservice.url=tcp://someservice:4002
myservice.queue=myqueue.service.txt.v1.q

The problem that I am having, is that, for the life of me, I cannot figure out how to get the value of myservice.url that is defined in myapp.properties into my running java code.

I have tried a static function [to be called around the application]:

public static String getProperty(String propName)
{
  WebApplicationContext ctx = 
      FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
  Environment env = ctx.getEnvironment();
  retVal = env.getProperty(propName);
  return retVal;
}

However, while it returns a populated Environment object "env", the env.getProperty(propName) method returns null.

1 Answers1

0

Ok - many, many thanks to Rustam who gave me the clues I needed. The way that I resolved this was thus:

In the src/main/resources/applicationContext.xml I have the following line:

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

Then in the src/main/resources/myapp.properties file I have the following lines:

myservice.url=tcp://someservice:4002
myservice.queue=myqueue.service.txt.v1.q

Then I have a class as follows:

package my.app.util;
import org.springframework.beans.factory.annotation.Value;

public class ConfigInformation 
{
    public ConfigInformation()
    {
       // Empty constructor needed to instantiate beans
    }

    @Value("${myservice.url}")
    private String _myServiceUrl;
    public String getMyServiceUrl()
    {
        return _myServiceUrl;
    }

    @Value("${myservice.queue}")
    private String _myServiceQueue;
    public String getMyServiceQueue()
    {
        return _myServiceQueue;
    }
}

Then, I have the following declaration in my applicationContext.xml:

<bean name="configInformation" class="my.app.util.ConfigInformation">
</bean>

After I have done this, I am able to use the following lines in my code:

WebApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
ConfigInformation configInfo = (ConfigInformation) ctx.getBean("configInformation");

And consequently, the configInfo object, is populated with the values assigned to "myservice.url" and "myservice.queue" which can be retrieved with the method calls:

configInfo.getMyServiceUrl()

and

configInfo.getMyServiceQueue()