java - Hibernate 占位符被忽略

标签 java spring hibernate

我正在使用 Spring 和 Hibernate 开发一个简单的独立 Java 应用程序来尝试学习它们。

我在 Spring 和 Hibernate 的配置中遇到了一些问题,以便真正从代码中创建一些“有用”的结果...

我尝试遵循“Beginning Hibernate 2nd Edition”和“Pro Spring 3”书中报告的示例,但是关于 Hibernate,我遇到了这个问题(我正在使用 log4j 进行日志记录) :

1824 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory  - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38638273: defining beans [dataSource,sessionFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,personaDao,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [app-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driverClassName}]

配置文件的解析中似乎忽略了占位符

这里我复制了其中的摘录:

app-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

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

 <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" >
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven/>




<bean id="personaDao" class="org.bladela.dataaccess.persona.PersonaDaoImpl">
       <property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

        <session-factory>
            <property name="show_sql">true</property>

            <mapping class="org.bladela.dataaccess.persona.Persona"/>



        </session-factory>
</hibernate-configuration>

jdbc.properties:

jdbc.driverClassName=org.postgresql.Driver
jdbc.dialect=org.hibernate.dialect.PostgreSQLDialect
jdbc.databaseurl=jdbc:postgresql://localhost:5432/employeemanagementdb
jdbc.username=bladela
jdbc.password=password  

如果我用它们的值替换所有占位符,程序将继续并连接到数据库(如果我只替换一个......错误“转到”下一个)

然后它返回一个不正确的结果(当它应该返回一个包含一个元素的列表时返回一个空列表),但如果我无法解决它,也许我稍后会询问它。

有什么建议吗?

最佳答案

我没有看到在您的上下文中声明任何 PropertyPlaceholderConfigurer。属性占位符应该如何解析?

一种解决方案是添加

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

根据您的上下文。

关于java - Hibernate 占位符被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204114/

相关文章:

java - @Order 与 @Bean 注释的方法行为

java - 如何修复 hibernate 上的 JndiException?

java - 了解 Hibernate 文档中可选的事务定义

mysql - 如何在Eclipse IDE中组合GWT+Hibernate+MySQL?

java - 如何检查 RXJava 是否完成或从队列中清空

java - 使这个HashMap更高效的方法

java - 如何从多个文件中轮询最后修改的文件并发送到 apache camel 中的目标端点?

JAVA Swing Gui 窗口挂起

java - 如果当前类是spring bean,如何使用抽象类中的参数调用 super 构造函数?

java - Spring Boot 控制台应用程序不记录任何内容