2016-08-27 4 views
1

나는 database.properties 일부 속성이 정의 된 파일이 있습니다. 또한 응용 프로그램의 context.xml 내가 databaseRepository 빈 속성에이 값을 넣을려고 파일 :속성 자리 표시 자 위치 사용

<context:property-placeholder location="file:property_placeholder/database.properties"/> 
<bean id="databaseRepository" class="property_placeholder.DatabaseRepository"> 
    <property name="host" value="${host}"/> 
    <property name="port" value="${port}"/> 
    <property name="user" value="${user}"/> 
    <property name="password" value="${password}"/> 
</bean> 

는하지만 응용 프로그램을 실행하려고하고있을 때 데이터베이스를 참조하십시오. : 파일 구조가 어떻게 보이는지

다음
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: property_placeholder\database.properties (The system cannot find the path specified) 
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151) 
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) 
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) 
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) 
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
at property_placeholder.MainSpring.main(MainSpring.java:9) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
Caused by: java.io.FileNotFoundException: property_placeholder\database.properties (The system cannot find the path specified) 
at java.io.FileInputStream.open0(Native Method) 
at java.io.FileInputStream.open(FileInputStream.java:195) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at java.io.FileInputStream.<init>(FileInputStream.java:93) 
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) 
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) 
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:168) 
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143) 
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) 
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175) 
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156) 
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:142) 
... 12 more 

이있다 : 속성을 나는 다음과 같은 오류가 파일을 가져

내가 뭘 잘못하고있어?

+0

파일이 클래스 경로에 있고 폴더 외부가 아닌 것 같기 때문에'file :'대신'classpath :'를 사용하는 것이 좋습니다. –

답변

1

오류는 실제로는 FileNotFoundException: property_placeholder\database.properties이므로 속성 파일에 올바른 경로를 제공했는지 확인하십시오.

구조에 따라 올바른 경로는 classpath:property_placeholder/database.properties 일 수 있습니다.

당신은 당신의 애플리케이션 컨텍스트 파일에 resources/property_placeholder

+0

고마워요! 도움이되었지만 database.properties 파일을 적절한 대상 하위 폴더에 복사해야했습니다. –

1

사용이 설정에 따라 파일 database.properties을 둘 필요가 : 당신의 재산 자원에 파일을 넣어.

<bean 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="location"> 
<value>classpath:database.properties</value> 
</property> 
</bean> 
+0

감사합니다. 속성 파일을 resources 폴더로 옮겼을 때 적절한 대상 하위 폴더에 수동으로 복사 할 필요가 없었습니다. –