簡體   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