【问题标题】:Access property values inside Spring config file访问 Spring 配置文件中的属性值
【发布时间】:2015-03-22 19:25:10
【问题描述】:

我在WEB-INF/local.db.properties 有一个简单的属性文件:

db.driverClassName=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/db_name
db.username=postgres
db.password=password

我正在尝试访问我的 Spring 配置文件中的这些属性(接近尾声):

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util-3.1.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="controllers" />

 <mvc:annotation-driven/>
 <mvc:default-servlet-handler/>

 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
     <property name="definitions">
         <list>
             <value>/WEB-INF/tiles-config.xml</value>
         </list>
     </property>
 </bean>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
 </bean>

 <util:properties id="dbProperties" location="WEB-INF/local.db.properties" />
 <bean id="dbDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="${db.driverClassName}" /> <!--ERROR -->
     <property name="url" value="${db.url}" />
     <property name="username" value="${db.username}" />
     <property name="password" value="${db.password}" />
 </bean>
</beans>

错误是Could not load JDBC driver class [${db.driverClassName}]。这个错误对我来说很清楚 - 它不是试图查找它只是使用原始字符串的属性。

我该如何解决这个问题?

这是一个 Maven 3、Spring 4、Postgresql 项目。

【问题讨论】:

  • 你没有在 Spring 中加载配置文件。
  • @LuiggiMendoza 你能详细说明一下吗?我该怎么做?

标签: java spring properties-file


【解决方案1】:

您没有在 Spring 配置中加载属性文件。添加这一行:

<context:property-placeholder location="classpath:/WEB-INF/local.db.properties"/>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/local.db.properties"/>
</bean>

如果您有多个属性文件,请将上述配置更改为:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- locations, not location, check the S at the end -->
    <property name="locations">
        <list>
            <value>
                /WEB-INF/local.db.properties
            </value>
            <value>
                <!-- Path of another properties file -->
            </value>
        </list>
    </property>
</bean>

更多信息:PropertySourcesPlaceholderConfigurer

【讨论】:

  • 太棒了,谢谢!我确实必须将 bean 定义更改为 &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;(我认为因为我使用的是版本 4)并将属性更改为 &lt;property name="location" value="/WEB-INF/local.db.properties"/&gt;(取出 'classpath:')。
  • XML 元素的location 参数可以采用逗号分隔的文件列表进行加载。建议使用命名空间而不是 bean,尤其是在 Spring 4 上,因为它为您提供 PropertySourcesPlaceholderConfigurer 而不是 PropertyPlaceholderConfigurer
【解决方案2】:

如果您使用的是 Eclipse,请尝试右键单击项目 -> 属性 -> Java 构建路径 -> 源,然后将文件夹或链接源添加到您的属性文件。

【讨论】:

  • 问题不在于文件不在classpath下,问题出在Spring配置上。
猜你喜欢
  • 1970-01-01
  • 2020-03-05
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
相关资源
最近更新 更多