java Spring - 使用属性占位符加载位于 war 文件或类路径之外但位于文件系统中的属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11141193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring - Using property-placeholder to load properties file that is outside the war file or the classpath but is in the filesystem
提问by ziggy
I have configured the loading of the properties file as shown below (Spring 3.1)
我已经配置了如下所示的属性文件的加载(Spring 3.1)
my-spring-xml
我的弹簧-xml
<context:component-scan base-package="com.mypackage"/>
<context:property-placeholder location="file:///C:/temp/application.properties"/>
web.xml
网页.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/my-spring-xml
</param-value>
</context-param>
application.properties
应用程序属性
expirationOffset = 777
In my java class i have declared the property as follows:
在我的 java 类中,我已经声明了如下属性:
private @Value("${expirationOffset}") String propertyValue;
For some reason the value is not being initialised. When i run the following statement:
由于某种原因,该值没有被初始化。当我运行以下语句时:
System.out.println("VALUE - " + propertyValue);
The output is always
输出总是
16:03:02,355 INFO [stdout] (http--127.0.0.1-8080-1) VALUE - ${expirationOffset}
I have tried to move the properties file in to the war file to try and access it while it is in the classpath but still the same problem. Here is how i access it from the classpath.
我试图将属性文件移动到 war 文件中,以尝试在类路径中访问它,但仍然存在同样的问题。这是我从类路径访问它的方法。
<context:property-placeholder location="classpath:/conf/application.properties"/>
Ideally i would like to keep the properties file outside the war file as i would not want to have to rebuild the war file because of a simple property change.
理想情况下,我希望将属性文件保留在 war 文件之外,因为我不想因为简单的属性更改而重建 war 文件。
Have i missed something?
我错过了什么吗?
Edit
编辑
I removed the msm-spring.xml initialisation paramters from this context:
我从此上下文中删除了 msm-spring.xml 初始化参数:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/my-spring-xml
</param-value>
</context-param>
and moved it to the servlet context as shown below.
并将其移动到 servlet 上下文,如下所示。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>myservice</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/my-spring-xml
</param-value>
</init-param>
</servlet>
The above change seems to have fixed as i am now getting the correct value. I thought the way i had originally was that i had it on the application context meaning that it will be available throughout the application/webapp.
上述更改似乎已修复,因为我现在获得了正确的值。我认为我最初的方式是将它放在应用程序上下文中,这意味着它将在整个应用程序/webapp 中可用。
What is the difference in having the msm-spring in the either of 3 i have listed above?
在我上面列出的 3 个中的任何一个中使用 msm-spring 有什么区别?
回答by Biju Kunjummen
This is a bit of speculation based on the information you have provided:
这是根据您提供的信息进行的一些推测:
You probably don't have the <context:property-placeholder..
in your Root Web Application context - the one loaded by ContextLoaderListener, instead you may be having it in the web context(loaded by Dispatcher servlet). Can you please confirm this.
您<context:property-placeholder..
的根 Web 应用程序上下文中可能没有ContextLoaderListener 加载的上下文,而您可能将其放在 Web 上下文中(由 Dispatcher servlet 加载)。你能确认一下吗。
Update:Based on the comment, the issue seems to have been that the <context:propert-placeholder..
was defined in the Root Web application context, but being referred to in a component from Web Context.
更新:根据评论,问题似乎<context:propert-placeholder..
是在根 Web 应用程序上下文中定义的,但在来自 Web 上下文的组件中被引用。
The fix is to move the propertyplaceholder to the web context (one defined through MessageDispatcherServlet) in this case.
在这种情况下,修复方法是将属性占位符移动到 Web 上下文(通过 MessageDispatcherServlet 定义的上下文)。
回答by Anuj Patel
EDIT :
编辑 :
Did you try using setter method with #{expirationOffset}
??
您是否尝试将 setter 方法与#{expirationOffset}
??
i.e. :
IE :
private String propertyValue;
@Value("#{expirationOffset}")
public void setPropertyValue(String property) {
propertyValue = property;
}
Another Option :
另外一个选择 :
Add Properties bean instead of PropertyPlaceConfigurer like this :
添加 Properties bean 而不是 PropertyPlaceConfigurer 像这样:
<util:properties id="myProps" location="file:///C:/temp/application.properties" />
OR
或者
<util:properties id="myProps" location="classpath:application.properties" />
And Replace Setter with a slight modification as
并稍微修改一下替换 Setter 为
private String propertyValue;
@Value("#{myProps.expirationOffset}")
public void setPropertyValue(String property) {
propertyValue = property;
}
You'll have to add xmlns:util="http://www.springframework.org/schema/util"
to xmlns
decalrations and correcsponding http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
to xsi:schemalocation
in your context configuration xml.
你必须添加xmlns:util="http://www.springframework.org/schema/util"
到xmlns
decalrations和correcspondinghttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
到xsi:schemalocation
你的上下文配置XML。
This should definitely work.!
这绝对有效。!
Hope it Helps. :)
希望能帮助到你。:)