简体   繁体   English

如何使用 spring 从属性文件中检索值

[英]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.

相关问题 如何在春季使用自动装配从属性文件中检索键的值 - How to retrieve the value of a key from properties file in spring using Autowiring 如何使用属性文件中的值在Spring属性中进行算术运算? - How to do arithmetic in Spring properties, with values from a properties file? java.util.Properties:如何从spring config xml中的Properties检索值 - java.util.Properties: How do I retrieve values from Properties in spring config xml Spring:如何从Model类的属性文件中获取值 - Spring : how to get values from properties file in Model class Spring:如何将值从属性文件传递给构造函数 - Spring: how pass values to constructor from properties file 无法使用Spring的@Value,@ ConfigurationProperties注释从Properties文件中检索值 - Unable to retrieve value from Properties file using Spring's @Value, @ConfigurationProperties annotations Spring从属性文件获取枚举值 - Spring get enum values from properties file 如何使用Spring Boot应用程序中另一个属性文件中的值解析属性文件中的占位符 - How to resolve placeholder in properties file with values from another properties file in spring boot application 无法使用spring检索属性 - cannot retrieve properties using spring 如何在spring中的属性文件中设置占位符值 - How to set placeholder values in properties file in spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM