Spring 3.2.2 property-placeholder无法正常工作

时间:2014-01-16 21:20:24

标签: java spring properties configuration

我使用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}]

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

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

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

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