使用Spring PropertyPlaceholderConfigurer获取外部属性失败

时间:2014-01-16 13:47:08

标签: jdbc properties datasource spring-jdbc

我在使用Spring PropertyPlaceholderConfigurer获取外部属性值时遇到问题。 这是Spring中的JDBC配置示例

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:spring/jdbc.properties</value>
    </property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="${driverClass}"></property>                                                    
        <property name="url" value="${driverUrl}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>   
        <property name="initialSize" value="${initialSize}"></property>
        <property name="maxActive" value="${maxActive}"></property>
    </bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg><ref bean="dataSource"/></constructor-arg>
    </bean>
</beans>

然后我运行一个JUnit测试用例,不幸的是得到了错误..

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '"oracle.jdbc.driver.OracleDriver"'
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
    at main.java.ds.EmployeeDS.getAllUsers(EmployeeDS.java:60)
    at main.java.ds.EmployeeDSTest.testGetAllUsers(EmployeeDSTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    ......
Caused by: java.lang.ClassNotFoundException: "oracle.jdbc.driver.OracleDriver"
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
    ... 33 more

我很确定ojdbc.jar已经参与其中,如果我将实际值放入“dataSource”bean而不是属性文件中,它可以正常运行...

我认为问题应该是“PropertyPlaceholderConfigurer” 但我担心我可能错了。 我创建了一个自定义属性configurer来检查PropertyPlaceholderConfigurer

是否有问题
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
    private static Map<String, Object> ctxPropertiesMap;
    @Override  
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,  
            Properties props)throws BeansException {  

        super.processProperties(beanFactory, props);  
        //load properties to ctxPropertiesMap  
        ctxPropertiesMap = new HashMap<String, Object>();  
        for (Object key : props.keySet()) {  
            String keyStr = key.toString();  
            String value = props.getProperty(keyStr);  
            ctxPropertiesMap.put(keyStr, value);  
        }  
    }  

    //static method for accessing context properties  
    public static Object getContextProperty(String name) {  
        return ctxPropertiesMap.get(name);  
    }

结果还行!!!! 所以我错过了什么?

<!-- -------------------------update my question -------------------------------- -->

我确信在执行测试时ojdbc.jar在我的类路径中 我试图使用真实的配置值(抱歉,我无法向您显示真实的网址,用户名和密码)

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@xxxx:xxxx:xxxx"></property>
        <property name="username" value="xxxxx"></property>
        <property name="password" value="xxxxx"></property>     
        <property name="initialSize" value="5"></property>
        <property name="maxActive" value="10"></property>
    </bean>

它有效...所以我认为这不是问题.. 任何建议?

0 个答案:

没有答案