有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用属性占位符位置的java

我有数据库。属性定义了某些属性的文件。 以及应用程序上下文。xml文件,我试图将这些值放入数据库存储库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>

但当我试图运行应用程序并读取数据库时。属性文件我收到以下错误:

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

我做错了什么


共 (2) 个答案

  1. # 1 楼答案

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

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

    您需要将文件database.properties放在resources/property_placeholder

  2. # 2 楼答案

    在应用程序上下文文件中使用此配置:将属性文件放入参考资料中

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