调用init方法失败; '的sessionFactory'或者' hibernateTemplate'是必须的

时间:2016-02-27 21:12:14

标签: java spring hibernate spring-mvc

我收到了问题中的错误。我的Dao实施班如下:

    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

请告诉我这样做的正确方法。

1 个答案:

答案 0 :(得分: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 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>

数据源配置datasource.xml:

Uncaught ReferenceError: Unable to parse bindings.
Bindings value: visible: expanded, click: toggle
Message: toggle is not defined