gpt4 book ai didi

java - Hibernate 占位符被忽略

转载 作者:行者123 更新时间:2023-12-01 13:47:58 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com