2016-02-27 85 views
0

我收到錯誤,如在問題中。吾道實現類定義如下:調用init方法失敗; 'sessionFactory'或'hibernateTemplate'是必需的

package dao.classes; 

import java.util.List; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 

import model.Compte; 
import dao.interfaces.*; 

@Repository 
public class CompteDao extends HibernateDaoSupport implements CompteDaoInterface{ 
    public CompteDao(){ 

    } 
    @Autowired 
    SessionFactory sessionfactory; 

    @Transactional(readOnly= false) 
     public void add(Compte compte) { 
     getHibernateTemplate().save(compte) ; 
     } 
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED) 
     public void edit(Compte compte) { 
     getHibernateTemplate().update(compte); 
     } 
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED) 
     public void delete (Compte compte) { 
     getHibernateTemplate().delete(compte); 
     } 
    public Compte getbyIDCompte(String matricule){ 
     return (Compte) (getHibernateTemplate().find("from Compte where matricule = ? ", matricule)).get(0) ; 

    } 

} 

這裏是CompteService實現:

package services.classes; 

import org.springframework.transaction.annotation.Transactional; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

import org.springframework.stereotype.Service; 

import dao.classes.CompteDao; 
import model.Compte; 
import services.interfaces.CompteServiceInterface; 

@Service 
@ManagedBean(name="compteService") 
@SessionScoped 
public class CompteService implements CompteServiceInterface{ 

    CompteDao compteDao; 
     public void setCompteDao(CompteDao compteDao) { 
      this.compteDao= compteDao; 

     } 

    @Transactional 
     public void add(Compte compte) { 
      compteDao.add(compte); 


     } 
     public void edit(Compte compte) { 
      compteDao.edit(compte); 

     } 
     public void delete (Compte compte){ 
      compteDao.delete(compte); 
     } 
     public Compte getbyIDCompte(String matricule){ 
      return compteDao.getbyIDCompte(matricule); 
     } 
} 

這裏是compte.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 


    <bean id="compteDao"  class="dao.classes.CompteDao"> 

    </bean> 
    <bean id="compteService" class="services.classes.CompteService"> 
     <property name="compteDao" ref="compteDao"></property> 
    </bean> 


     </beans> 

而產生的錯誤:

GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDao' defined in class path resource [spring/beans/Compte.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701) 
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204) 
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required 
    at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:118) 
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) 
    ... 19 more 

請告訴我什麼是正確的方法來做到這一點。

+0

是你的所有配置的?應該有一些額外的Hibernate bean配置,包括SessionFactory。看看有關Hibernate的Spring文檔:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html#orm-hibernate – woemler

回答

0

這裏是Hibernate.xml的配置:

<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.xsd"> 



    <bean id ="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" > 
    <property name="sessionFactory" ref="sessionFactory"/> 
    <property name="checkWriteOperations" value="false"/> 
    </bean> 


     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

     <property name="dataSource"> 
     <ref bean= "dataSource"/> 
     </property> 
     <property name="hibernateProperties"> 
     <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     </props> 
     </property> 

     <property name="mappingResources"> 
     <list> 
     <value>/hibernate/Compte.hbm.xml</value> 
     <value>/hibernate/Groupe.hbm.xml</value> 
     <value>/hibernate/Utilisateur.hbm.xml</value> 

     </list> 
     </property> 
     </bean> 





     </beans> 

數據源配置datasource.xml:

<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.xsd"> 


    <!-- <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> --> 
    <!-- <property name="location"> --> 
    <!-- <value>WEB-INF/classes/properties/DataBaseConnection.properties </value> WEB-INF/classes/properties/DataBaseConnection.properties --> 
    <!-- </property> --> 
    <!-- </bean> --> 

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/> 
     <property name="username" value="datacenter"/> 
     <property name="password" value="datacenter"/> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
     </bean> 

     </beans> 
and beanslocation.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:security="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd "> 


     <import resource="../beans/Compte.xml"/> 







     </beans> 
相關問題