2014-04-20 1 views
0

전쟁 이외의 속성 파일에 액세스해야합니다. 나는 외부 재산에 대한 경로를 유지하고있다.war-Spring 2.5.1 버전 외의 속성 파일에 액세스

파일을 클래스 경로 특성 파일에 보관하고있다. 프로젝트 버전은 입니다. 스프링 2.5.1

@Configuration 및 Environemnt 클래스는 사용할 수 없습니다.

System.properties은 클래스 경로에 있으며 외부 속성에 대한 키 입력이 있습니다. 파일이 ID를

 keyValue= 444; 

external.properties에서

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

있다 \ 임시 \의 external.properties

나는 아래의 방법으로 시도 :

key.externalFile = C 나는이 값을 아래와 같이 빈에 주입 할 필요가있다.

 <bean id="helloWorldBean" class="com.java.snippets.enterprise.services.HelloWorld"> 
<property name="key" value="${keyValue} /> 

내가

나는 또한

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

<util:properties id="props" location="file:${key.supportiveFile}"/> 

함께 노력 "$ {키 값} '자리'키 값 '문자열 값을 확인할 수 없습니다 오류 무엇입니까 그러나 그것은 같은 결과로 끝났다.

<bean id="helloWorldBean" 
    class="com.java.snippets.enterprise.services.HelloWorld"> 
    <property name="key" value="${props.keypath}" /> 
</bean> 

도와주세요.

전체 코드 1. 상황에 맞는 파일

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

<context:property-placeholder location="classpath:constants.properties"/> 
<context:property-placeholder location="file:${key.supportiveFile}"/> 
    <bean id="helloWorldBean" 
    class="com.java.snippets.enterprise.services.HelloWorld"> 
    <property name="key" value="${keypath}" /> 
</bean> 
    </beans> 

2. System property file - constants.properties 
key.supportiveFile=C\:\\Users\\Kasun\\AppData\\Local\\Temp\\key.properties 

3. Test Class 

    public static void main(String[] args) { 

ApplicationContext context = new 
    ClassPathXmlApplicationContext("applicationContext.xml");   
     HelloWorld hello = (HelloWorld) context.getBean("helloWorldBean");   
     hello.sayHello(); 


} 

감사합니다. 불행히도 여전히 해결할 수 없습니다. 스레드 "main"의 예외 org.springframework.beans.factory.BeanInitializationException : 속성을로드 할 수 없습니다. 중첩 예외는 java.io.FileNotFoundException입니다 : $ {key.externalFile} (지정된 파일을 찾을 수 없습니다.) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory (PropertyResourceConfigurer.java:78) at org. springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors org.springframework.context.support.AbstractApplicationContexeBeanFactoryPostProcessors에서 (AbstractApplicationContext.java:542) (AbstractApplicationContext.java:516) org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext에서 . java : 348) at org.springframework.context.support.ClassPathXmlApplicationContext (ClassPathXmlApplicationContext.java:123) at org.springframework.context.support.ClassPathXmlApplicationContext.t .

+0

이 난 그냥 대답의 단계를 언급, 혼란에 대해 무엇을 참조하십시오. – Prasad

답변

0

컨텍스트를 제거 invok PropertyPlaceholderConfigurer와의 속성 정의와로 교체 :

<bean id="placeholderConfig1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>classpath:System.properties</value> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders"> 
      <value>true</value> 
     </property> 
     <property name="systemPropertiesModeName"> 
      <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value> 
     </property> 
    </bean> 

    <bean id="placeholderConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>file:${key.externalFile}</value> 
     </property> 
    </bean> 
+0

감사합니다. 위의 예외가 발생합니다. 스프링 2.5에서 작동합니까? – Kasun

+0

https://jira.shpring.io/browse/SPR-6428 이것이 봄 문제입니까? – Kasun

+0

org.springframework.beans.factory.BeanInitializationException : 속성을로드 할 수 없습니다. 중첩 예외는 java.io.FileNotFoundException : $ {key.externalFile} (시스템에서 지정된 파일을 찾을 수 없음) – Kasun