1

いくつかのプロパティが定義されたdatabase.propertiesファイルがあります。また、これらの値をdatabaseRepository Bean プロパティに設定しようとしているapplication-context.xmlファイル:

    <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>

しかし、アプリを実行してdatabase.propertiesファイルを読み取ろうとすると、次のエラーが発生します。

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

ファイル構造は次のようになります。

ここで何が間違っていますか?

4

2 に答える 2

2

エラーは実際にはFileNotFoundException: property_placeholder\database.propertiesであるため、プロパティ ファイルへの正しいパスを指定していることを確認してください。

ここで、正しいパスはclasspath:property_placeholder/database.properties構造に応じたものになる可能性があります。

database.propertiesファイルを下に置く必要がありますresources/property_placeholder

于 2016-08-27T13:02:58.410 に答える