【问题标题】:Properties aren't resolved in Spring context XML属性未在 Spring 上下文 XML 中解析
【发布时间】:2012-01-11 15:22:22
【问题描述】:

我正在使用 static main 方法创建我的 Spring 上下文

return new ClassPathXmlApplicationContext("applicationContext.xml");

applicationContext.xml 内部,我正在用我的一个豆子接线

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
    <property name="driverClassName">
        <value>${db.driverclassname}</value>
    </property>
    ...
</bean>

我的类路径中有一个 .properties 文件,其中包含值 db.driverclassname

不幸的是,我收到以下错误:

Property 'driverClassName' threw exception; 
    nested exception is java.lang.IllegalStateException: 
        Could not load JDBC driver class [${db.driverclassname}]

我做错了什么?我正在使用 Spring 2.5.5

【问题讨论】:

    标签: java spring properties javabeans


    【解决方案1】:

    您不需要PropertyPlaceholderConfigurer 吗?

    例如

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

    This article 详细使用情况。

    【讨论】:

      【解决方案2】:
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                  <list>
                      <value>classpath:your.properties</value>
                  </list>
              </property>
              <property name="ignoreUnresolvablePlaceholders" value="true"/>
          </bean>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-04
        • 2014-12-11
        • 2017-06-06
        • 2012-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-18
        相关资源
        最近更新 更多