2013-08-23 2 views
4

저는 스프링 웹 MVC 프로젝트를 사용하고 ormlite.xmljdbc.properties을 포함하여 모든 봄 관련 파일을 WEB-INF\spring 아래에 배치했습니다.봄 컨텍스트 구성 파일에서 속성 파일을 찾는 방법

지금 나는 ormlite.xml에서 jdbc.properties 파일, Like this 찾으려 :

<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/> 

을하지만 응용 프로그램을 실행할 때, 그것은 나에게 그런 말 것 :

Could not load properties

은 할 수 없습니다 특성 파일을 찾으십시오.

무엇이 문제입니까?

<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/> 

사용 :

<bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:location="/WEB-INF/spring/jdbc.properties"/> 

그리고 당신의 속성은 봄 파일에서 사용할 수 및 네임 스페이스 추가하는 것을 잊지 마세요 :

답변

9

:

문제는 루트 경로의 에 아니므로 WEB-INF는 당신이 사용할 때 동일한 경로를 사용해야합니다, 액세스 할 수없는/것을 당신의 테스트 케이스 (src/main/webapp 부분을 포함하지만 응용 프로그램이 작동하지 않게됩니다. ). 속성을로드하는 접두사 :

나는 당신이 SRC/메인/자원 디렉토리에 jdbc.properties를 이동하고 단순히 클래스 경로를 사용하는 것이 좋습니다.

코드 :

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

위의 코드는 그들이 (그들은 src/main/resources에있을 때 그들이 입니다) 클래스 패스의 루트에 있다고 가정합니다.

다른 사람에게 도움이되기를 바랍니다.

0

대신 내가 생각 xmlns:p="http://www.springframework.org/schema/p"

+0

작동하지 않습니다. 다음을보십시오 : [http://i.imgur.com/QmpQv6K.png] – hguser

+0

어떤 버전의 스프링을 사용하고 있습니까? –

+0

나는 3.2.3.RELEASE – hguser

0

을 Spring에 프로퍼티를로드하는 방법을 지시하는 접두어가 없습니다. 나는 당신의 정의를 할 필요가 생각 :

<context:property-placeholder location="file:/WEB-INF/spring/jdbc.properties"/> 

참고 파일의 추가 : 접두사. 봄 포럼에서

+0

'file :'접두사를 추가하려고 시도했지만 작동하지 않습니다. – hguser

+0

파일을 시도 했습니까? WEB-INF/spring/jdbc.properties? 즉 시작 슬래시가 누락 되었습니까? –

+0

예, 둘 다 시도했습니다. – hguser

1

클래스 패스 외부에 동일한 문제가 발생했습니다.

내 솔루션 :

먼저 등록 bean을 정의

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="location"> 
      <value>/WEB-INF/your.properties</value> 
     </property> 
    </bean> 

다음 속성-자리에서 그것을 참조 : 나를 위해 완벽하게

<context:property-placeholder properties-ref="configProperties" /> 

작품!

관련 문제