使用$ {db.datasource}无法使用hibernate.properties值在应用程序上下文中配置数据源

时间:2014-10-16 18:30:07

标签: java spring hibernate

类路径中的hibernate.properties按如下所示加载

org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {db.password=cFZlRDExZVNhQms=, db.url=jdbc:oracle:thin:@******, datasource.driverClassName=com.mysql.jdbc.Driver, db.username=base, hibernate.bytecode.use_reflection_optimizer=false}

applicationcontext.xml中的datasource和propertyplaceholder如下:

<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
            <property name="ignoreResourceNotFound" value="true"/>
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>


         <bean id="dataSource" class="emp.MyCustomBasicDataSource" destroy-method="close">

        <property name="driverClassName" value="${datasource.driverClassName}"/>
        <property name="url" value="jdbc:mysql://*****/TestDB"/>
        <property name="username" value="root"/>
        <property name="password" value="hello"/>
        </bean>

hibernate.properties如下:

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:oracle:thin:@****
db.username=base
db.password=cFZlRDExZVNhQms=

即使没有给出propertyplaceholder,属性文件也会被加载。

当我尝试运行Main类时,exeception发生如下:

  processing foreign key constraints
    23:53:56.380 [main] INFO  o.h.c.ConnectionProviderFactory - Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
    23:53:56.386 [main] INFO  o.h.e.c.InjectedDataSourceConnectionProvider - Using provided datasource
    23:53:56.399 [main] WARN  org.hibernate.cfg.SettingsFactory - Could not obtain connection metadata
    org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${datasource.driverClassName}'
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136) [commons-dbcp.jar:1.2.2]
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880) [commons-dbcp.jar:1.2.2]
        at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46) [hibernate-entitymanager.jar:3.4.0.GA]
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:88) [hibernate-core.jar:3.3.0.CR1]
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2078) [hibernate-core.jar:3.3.0.CR1]
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1302) [hibernate-core.jar:3.3.0.CR1]
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) [hibernate-annotations.jar:3.3.0.CR1]
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) [hibernate-entitymanager.jar:3.4.0.GA]
        at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132) [hibernate-entitymanager.jar:3.4.0.GA]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:224) [spring-orm.jar:2.5.6.SEC01]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:291) [spring-orm.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) [spring-beans.jar:2.5.6.SEC01]
        at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_67]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByName(AbstractAutowireCapableBeanFactory.java:1029) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:977) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) [spring-beans.jar:2.5.6.SEC01]
        at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_67]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) [spring-beans.jar:2.5.6.SEC01]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) [spring-beans.jar:2.5.6.SEC01]
        at emp.InsertTest.main(InsertTest.java:37) [bin/:na]
    Caused by: java.lang.ClassNotFoundException: ${datasource.driverClassName}
        at java.net.URLClassLoader$1.run(Unknown Source) [na:1.7.0_67]
        at java.net.URLClassLoader$1.run(Unknown Source) [na:1.7.0_67]
        at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_67]
        at java.net.URLClassLoader.findClass(Unknown Source) [na:1.7.0_67]
        at java.lang.ClassLoader.loadClass(Unknown Source) [na:1.7.0_67]
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) [na:1.7.0_67]
        at java.lang.ClassLoader.loadClass(Unknown Source) [na:1.7.0_67]
        at java.lang.Class.forName0(Native Method) [na:1.7.0_67]
        at java.lang.Class.forName(Unknown Source) [na:1.7.0_67]
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130) [commons-dbcp.jar:1.2.2]`enter code here`
        ... 33 common frames omitted




I have tried to retrieve the datasource value alone first but I'm unable to get it. Kindly help me resolve this issue.

0 个答案:

没有答案