2012-03-08 18 views
16

私はApplicationContextを使用してJavaアプリケーションにロードされるXML構成ファイルを使用しています。Spring設定ファイルでオプションのプロパティファイルを使用するにはどうすればよいですか?

XML設定ファイルは、PropertyPlaceholderConfigurerを使用していくつかのプロパティファイルから読み取ることによってプロパティを解決します。

各プロパティファイルをオプションにしたいと考えています。私は(db-default.propertiesが存在しますが、db.propertiesにはない)私は、アプリケーションを実行するときしかし、私は次の例外を取得しています、これをtrueにignoreUnresolsvablePlaceholdersを設定することによって行われることを考えた:

Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [db.properties] cannot be opened because it does not exist 

これは何私でありますXML構成は次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <bean id="placeholder-configurer-1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="order" value="1"/> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    </bean> 

    <bean id="placeholder-configurer-2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="order" value="2"/> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
     <property name="location" value="classpath:/db-default.properties"/> 
    </bean> 

    <bean id="placeholder-configurer-3" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="order" value="3"/> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
     <property name="locations"> 
      <list> 
       <value>classpath:/db.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="${jdbc.driver}"/> 
     <property name="url" value="${jdbc.url}"/> 
     <property name="username" value="${jdbc.username}"/> 
     <property name="password" value="${jdbc.password}"/> 
    </bean> 

</beans> 

db.propertiesをオプションのプロパティファイルにするために必要なことは何ですか。

答えて

17
<property name="ignoreResourceNotFound" value="true"/> 
+0

ありがとうございました。それがトリックでした。 –

+0

実際、私が書いたものを見ると、私たちは両方とも古いxmlスタイルを使用していることに気付きました。あなたは 'p:ignoreResourceNotFound =" true "'構文を知っていますか?そうでない場合は、http://blog.springsource.org/2006/11/25/xml-syntax-sugar-in-spring-20/をチェックしてください。 –

4

これはあまりにも動作するはずです:

<context:property-placeholder ignore-resource-not-found="true" location="classpath:your.properties" ... /> 
関連する問題