【问题标题】:spring DIC read properties file inside maven structurespring DIC读取maven结构内的属性文件
【发布时间】:2013-02-07 09:49:20
【问题描述】:

我有一个 spring XML 文件,其中包含为休眠创建数据源的 bean:

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

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

这是这个文件在使用 Maven 之前的样子。现在我已将此文件放在 src/main/resources/properties/database.properties 中,我得到java.io.FileNotFoundException - 找不到属性文件。我将&lt;value&gt;properties/database.properties&lt;/value&gt; 更改为&lt;value&gt;/properties/database.properties&lt;/value&gt; 仍然无法正常工作。属性文件位于/target/classes/properties/database.properties,所以我想它应该可以从properties/database.properties 中读取,因为src/main/resources 包含在类路径中。我应该在配置 XML 中更正什么?

【问题讨论】:

    标签: jakarta-ee maven filenotfoundexception


    【解决方案1】:

    答案是在属性值上加上classpath:,如下图:

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

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 2020-05-10
      • 1970-01-01
      • 2011-10-31
      相关资源
      最近更新 更多