繁体   English   中英

Spring-使用property-placeholder加载war文件或类路径之外但在文件系统中的属性文件

[英]Spring - Using property-placeholder to load properties file that is outside the war file or the classpath but is in the filesystem

我已经配置了属性文件的加载,如下所示(Spring 3.1)

我的弹簧的XML

<context:component-scan base-package="com.mypackage"/>
<context:property-placeholder location="file:///C:/temp/application.properties"/>

web.xml中

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>       
        /WEB-INF/my-spring-xml
        </param-value>
    </context-param>

application.properties

expirationOffset = 777

在我的java类中,我声明了以下属性:

private @Value("${expirationOffset}") String propertyValue;

由于某种原因,该值未初始化。 当我运行以下语句时:

System.out.println("VALUE - " + propertyValue);

输出总是

16:03:02,355 INFO  [stdout] (http--127.0.0.1-8080-1) VALUE - ${expirationOffset}

我试图将属性文件移到war文件中,以尝试在类路径中访问它,但仍然存在相同的问题。 这是我从类路径访问它的方式。

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

理想情况下,我希望将属性文件保留在war文件之外,因为由于简单的属性更改,我不想重建war文件。

我错过了什么吗?

编辑

我从此上下文中删除了msm-spring.xml初始化参数:

<context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>       
            /WEB-INF/my-spring-xml
            </param-value>
        </context-param>

并将其移动到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>

上述更改似乎已修复,因为我现在获取了正确的值。 我认为本来的方式是将它放在应用程序上下文中,这意味着它将在整个应用程序/ webapp中可用。

msm-spring在上面列出的3个中的任何一个中有什么区别?

根据您提供的信息,这有点猜测:

您的根Web应用程序上下文中可能没有<context:property-placeholder.. ..-由ContextLoaderListener加载,而您可能是在Web上下文中(由Dispatcher servlet加载)。 能否请您确认一下。

更新:根据评论,问题似乎在于<context:propert-placeholder..是在Root Web应用程序上下文中定义的,但在Web Context的组件中被引用。

解决方法是在这种情况下将propertyplaceholder移至Web上下文(通过MessageDispatcherServlet定义的一个)。

编辑:

您是否尝试通过#{expirationOffset}使用setter方法?

即:

private String propertyValue;
@Value("#{expirationOffset}")
    public void setPropertyValue(String property) {
        propertyValue = property;
    }

另外一个选项 :

添加Properties bean而不是PropertyPlaceConfigurer,如下所示:

<util:properties id="myProps" location="file:///C:/temp/application.properties" />

要么

<util:properties id="myProps" location="classpath:application.properties" />

并将Setter稍作修改即可

private String propertyValue;
@Value("#{myProps.expirationOffset}")
    public void setPropertyValue(String property) {
        propertyValue = property;
    }

您必须将xmlns:util="http://www.springframework.org/schema/util"xmlns缩小和相应的http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd为上下文配置xml中的xsi:schemalocation

这绝对应该工作。

希望能帮助到你。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM