2014-10-10 81 views
1

這是錯誤: 產生的原因:的SessionFactory Hibernate的錯誤(Spring MVC的項目)

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of 
type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean 
which qualifies as autowire candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)} 

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

    <!-- Datasource to connection management (http://commons.apache.org/dbcp/configuration.html) 
    --> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     destroy-method="close"> 
     <property name="driverClass" value="${datasource.mysql.driverClassName}" /> 
     <property name="jdbcUrl" value="${datasource.mysql.url}" /> 
     <property name="user" value="${datasource.mysql.username}" /> 
     <property name="password" value="${datasource.mysql.password}" /> 
     <property name="maxIdleTime" value="${datasource.mysql.maxIdle}" /> 
    </bean> 

    <!-- Hibernate SessionFactory --> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="br.com.mirian.martins.core.model" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> 
       <prop key="hibernate.c3p0.timeout">${datasource.mysql.timeout}</prop> 
       <prop key="hibernate.connection.release_mode">after_transaction</prop> 
      </props> 
     </property> 
    </bean> 

    <!-- Transaction manager for a single Hibernate SessionFactory --> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
     scope="prototype"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
     <property name="defaultTimeout" value="120" /> 
    </bean> 

    <bean id="transactionTemplate" 
     class="org.springframework.transaction.support.TransactionTemplate"> 
     <property name="transactionManager" ref="transactionManager" /> 
    </bean> 
</beans> 

這是我DAOImpl (generic)

/** 
* Classe generica com metodos padroes utilizados na camada de persistencia 
* 
* @param <T> 
* @param <ID> 
*/ 
public class DAOImpl<T, ID extends Serializable>{ 

    private transient final Class<T> clazz; 

    @Autowired 
    private SessionFactory sessionFactory; 

    @SuppressWarnings("unchecked") 
    public DAOImpl() { 
    this.clazz = (Class<T>) ((ParameterizedType) 
    getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 
    } 

    /** 
    * SessionFactory injection 
    * 
    * @param sessionFactory 
    */ 
    public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
    } 

    /** 
    * Recupera a sessao corrente 
    * 
    * @return sessao corrente 
    */ 
    protected Session getSession() { 
    return sessionFactory.getCurrentSession(); 
    } 

    /** 
    * Utiliza a sessao corrente para criar uma nova instancia de criteria 
    * 
    * @return instancia de criteria 
    */ 
    protected Criteria createCriteria() { 
    return getSession().createCriteria(clazz); 
    } 

    /** 
    * Utiliza a sessao corrente para criar uma nova query 
    * 
    * @param hql 
    * @return query 
    */ 
    protected Query createQuery(String hql) { 
    return getSession().createQuery(hql); 
    } 
} 

這是我的controller

@Controller 
public class IndexController { 

    @Autowired 
    UserService userService; 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String index(HttpServletRequest request, HttpServletResponse response) { 


    return "index"; 

    } 

} 

請問,如果有人現在如何解決這個問題。 謝謝

+0

你的spring配置文件在哪裏?你應該把TransactionManager放在spring配置文件中。因此,Spring可以使Spring事務處於休眠狀態! – 2014-10-10 02:02:08

+0

請問您可以添加您的web.xml和servlet-context.xml文件的代碼 – 2014-10-10 05:55:10

+0

這是根本原因異常嗎?你能展示完整的堆棧跟蹤嗎? – pomkine 2014-10-10 07:41:40

回答

3

在這裏,你感到困惑的兩件事情

  1. JPA EntityManagerFactory的

    EntityManagerFactoryEntityManager。它們由JPA標準定義。

  2. 的SessionFactory

    SessionFactorySession和休眠是特定的。

都有優點有缺點&,因爲你使用SessionFactory(未JPA),你不應該使用的persistence.xml可言。

JPA EntityManagerFactory的

如果你想使用JPA與Spring,你需要把persistence.xmlMETA-INF文件夾中的源文件夾中,並在applicationContext.xml

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name = "persistenceUnitName" value = "MyJPA" /> 
</bean> 

聲明LocalContainerEntityManagerFactory然後你可以注入EntityManager使用@PersistenceContext

@PersistenceContext 
private EntityManager em; 

持久性。XML

this files usually contain details related to your database, such as connection strings and their respective user names and passwords including other ORM related information. These details can be placed in other locations so you need not explicitly have one, although having such a file usually makes all persistence related information available in one place which makes looking up certain settings and configurations easier.

SessionFactory的

如果你想使用SessionFactory那麼你已經申報sessionFactory豆到您applicationContext.xml如下

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     destroy-method="close"> 
     <property name="driverClass" value="${datasource.mysql.driverClassName}" /> 
     <property name="jdbcUrl" value="${datasource.mysql.url}" /> 
     <property name="user" value="${datasource.mysql.username}" /> 
     <property name="password" value="${datasource.mysql.password}" /> 
     <property name="maxIdleTime" value="${datasource.mysql.maxIdle}" /> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="packagesToScan" value="com.edgeowt.entities"/> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="show_sql">${hibernate.show_sql}</prop> 
        <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      </props> 
     </property> 
    </bean> 

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

然後你就可以能夠注入SessionFactory到您的春豆使用@Autowired

@Autowired 
private SessionFactory sessionFactory; 

希望這會幫助你解決你的問題...

1

您需要在DAO(或類似的Spring註釋)的頂部添加@Component。你也有你的web.xml中的<context:annotation-config/><context:component-scan base-package="whatever"/>?你必須告訴Spring出去嗅探註解。

+0

我們在''''''''''applicationContext.xml''文件中添加了''和''not int'web.xml' – 2014-10-10 05:08:05

1

您必須在DAOImpl類的頂部添加@Repository(Spring註釋)。此外,您必須插入<context:component-scan base-package="packageName"/>以掃描您的項目並在您的項目中找到mvn組件,並在彈簧配置中找到<mvc:annotation-driven />(以啓用彈簧註釋),以便它可以理解您的彈簧註釋。

相關問題