2014-04-16 82 views
1

我遇到需要在我的系統中使用兩個屬性文件的情況。正在加載上下文時加載屬性文件,將文件路徑作爲另一個屬性文件的屬性

  1. System.properties

    key.supportiveFile=C:\keypath\supportive.properties 
    

    關鍵路徑其可以是類路徑之外。我想維護支持.properties作爲System.properties中的鍵的路徑。 supportive.properties文件有

  2. supportive.properties。

    Supportive.properties具有密鑰key1,key2。 屬性supportive.properties文件必須使用上下文加載。

理想情況下,我的屬性文件將顯示如下。

supportive.properties 
key1=value1 
key2=value2 

在spring.xml我試圖加載supportive.properties和注入KEY1和KEY2值在spring.xml定義像的下方。

i。加載屬性文件

<context:property-placeholder location="classpath:System.properties,file:${key.supportiveFile}"/> 

ii。這是一個示例bean,我想如何將密鑰注入到bean中。

<bean id="helloWorldBean" class="com.sample.snippets.enterprise.services.HelloWorld"> 
     <property name="prefixProp" value="${key1}" /> 
     <property name="suffixProp" value="${key2}" /> 
    </bean> 

服務器日誌說Could not resolve placeholder 'key.supportiveFile' in [file:${key.supportiveFile}]但失敗並不會加載supportive.property文件。

請告知如何解決這個技術問題。

+0

請使用正確的文本格式! – Baby

回答

0

你可以這樣說:

<context:component-scan base-package="com.foo.bar.config"/> 

<context:property-placeholder location="file:${key.supportiveFile}" /> 

<bean id="helloWorldBean" class="com.sample.snippets.enterprise.services.HelloWorld"> 
    <property name="prefixProp" value="${key1}" /> 
    <property name="suffixProp" value="${key2}" /> 
</bean> 

和內部com.foo.bar.config你有下面的類:

package com.foo.bar.config; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 

@Configuration 
@PropertySource("classpath:/System.properties") 
public class SpringConfig{ 

} 

說明這種方法來自this JIRA issue

+0

謝謝我試過你的方法,但不幸的是,現在我在加載文件時出現以下錯誤:$ {key.supportiveFile} resource.http://stackoverflow.com/questions/10627856/spring-3-0-could-not -load-properties – Kasun

+0

你究竟做了什麼?向我們展示您的代碼和堆棧跟蹤! –

+0

對不起,這是一個Windows路徑問題。這對Spring3.x很有效。我希望將實際的實現應用於Spring 2.5.1版本的項目,其中沒有org.springframework.context.annotation.Configuration類。有沒有辦法在這種情況下做。 ? – Kasun