【问题标题】:Spring 3.2.2 property-placeholder not workingSpring 3.2.2 属性占位符不起作用
【发布时间】:2014-01-16 21:20:23
【问题描述】:

我使用 Spring 3.2.2 。我想使用 db.properties 文件作为数据库参数

db.properties

db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/test
db.user=test
db.password=test

弹簧配置

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

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

但我有错误

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${db.driver}]

谁能帮帮我?

【问题讨论】:

  • 您的属性文件位于类路径的顶层,对吧?
  • 你如何加载这个 xml 文件?请添加完整的堆栈跟踪。

标签: java spring properties configuration


【解决方案1】:

尝试在 spring 中将你的属性文件定义为:

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

然后以与您相同的方式访问它。

【讨论】:

  • 我使用 Mybatis,当我使用 mybatis-spring 1.0.1 时它可以工作,但是当我使用 1.1.1 版本时它不起作用
猜你喜欢
  • 2013-05-05
  • 2014-07-16
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
相关资源
最近更新 更多