【问题标题】:Using property placeholder location使用属性占位符位置
【发布时间】:2016-08-27 12:57:51
【问题描述】:

我有定义了一些属性的 database.properties 文件。 还有 application-context.xml 文件,我试图将这些值放入 databaseRepository bean 属性:

    <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

文件结构如下所示: structure

我在这里做错了什么?

【问题讨论】:

  • 我建议尝试classpath: 而不是file:,因为您的文件似乎在类路径中,而不是在文件夹之外。

标签: java spring intellij-idea property-placeholder


【解决方案1】:

在您的应用程序上下文文件中使用此配置:将您的属性文件放入资源中。

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

【讨论】:

  • 谢谢。当我将属性文件移动到资源文件夹时,我不需要手动将其复制到正确的目标子文件夹。
【解决方案2】:

错误实际上是FileNotFoundException: property_placeholder\database.properties,因此请确保提供正确的属性文件路径。

根据您的结构,这里的正确路径可能是classpath:property_placeholder/database.properties

你需要把你的文件database.properties放在resources/property_placeholder下面

【讨论】:

  • 谢谢!这很有帮助,但我还必须将 database.properties 文件复制到正确的目标子文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 2013-01-21
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多