[英]How to retrieve values from a properties file using spring
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?有人可以建议我如何使用 Spring 来检索我的 Java 代码中的值,以从 applicationContext.xml 中的 context:property-placeholder 标记指定的任何属性文件中获取值吗?
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.我是 Spring 的新手。尝试使用它来检索我的应用程序的可配置属性以连接到(可配置的)JMS 队列。
I have a Java/J2EE web application, using Spring.我有一个 Java/J2EE web 应用程序,使用 Spring。
In the src/main/resources/applicationContext.xml I have the following line:在 src/main/resources/applicationContext.xml 我有以下行:
<context:property-placeholder location="classpath:myapp.properties" />
Then in the src/main/resources/myapp.properties file I have the following lines:然后在 src/main/resources/myapp.properties 文件中,我有以下几行:
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.我遇到的问题是,对于我的一生,我无法弄清楚如何将 myapp.properties 中定义的 myservice.url 的值获取到我正在运行的 java 代码中。
I have tried a static function [to be called around the application]:我试过 static function [在应用程序周围调用]:
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.但是,虽然它返回填充的环境 object“env”,但 env.getProperty(propName) 方法返回 null。
Ok - many, many thanks to Rustam who gave me the clues I needed.好的 - 非常非常感谢Rustam ,他给了我所需的线索。 The way that I resolved this was thus:
我解决这个问题的方法是:
In the src/main/resources/applicationContext.xml I have the following line:在 src/main/resources/applicationContext.xml 我有以下行:
<context:property-placeholder location="classpath:myapp.properties" />
Then in the src/main/resources/myapp.properties file I have the following lines:然后在 src/main/resources/myapp.properties 文件中,我有以下几行:
myservice.url=tcp://someservice:4002
myservice.queue=myqueue.service.txt.v1.q
Then I have a class as follows:然后我有一个 class 如下:
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:然后,我的 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 object 填充了分配给“myservice.url”和“myservice.queue”的值,可以通过方法调用检索这些值:
configInfo.getMyServiceUrl()
and和
configInfo.getMyServiceQueue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.