2013-11-25 76 views
0

我正在開發一個簡單的獨立Java應用程序,使用Spring和Hibernate嘗試學習它們。忽略Hibernate佔位符

我有在Spring和Hibernate的配置一些問題,才能真正從代碼中創建一些「有用」的結果......

我試着下面的例子作爲報道書「開始Hibernate的第二版」和「臨春3」,但我有,就休眠,這個問題(我使用log4j來進行記錄):

1824 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]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 

如果我用他們的值替換所有的佔位符,程序繼續,它連接到分貝(如果我只替換一個...錯誤「去」到下一個)

然後它返回一個不正確的結果(一個空列表,當它應該返回一個元素列表),但也許我會問以後,如果我不能解決它。

有什麼建議嗎?

回答

1

我沒有在您的上下文中看到任何PropertyPlaceholderConfigurer聲明。財產佔位符應該如何解決?

一種解決方案是

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

添加到您的環境。

+0

感謝...它的工作(對不起,打擾你了) 現在我已經明白,爲什麼返回的查詢列表是空的......工作,爲明天...... 再次感謝 – bladela

+0

@bladela不客氣。這就是我們在這裏。考慮加註並接受有幫助的答案(和問題)。 –

+0

我試過了...但我沒有要求的聲望,以upvote你 我會接受你的答案,只要網站將允許我:) – bladela