這是錯誤: 產生的原因:的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";
}
}
請問,如果有人現在如何解決這個問題。 謝謝
你的spring配置文件在哪裏?你應該把TransactionManager放在spring配置文件中。因此,Spring可以使Spring事務處於休眠狀態! – 2014-10-10 02:02:08
請問您可以添加您的web.xml和servlet-context.xml文件的代碼 – 2014-10-10 05:55:10
這是根本原因異常嗎?你能展示完整的堆棧跟蹤嗎? – pomkine 2014-10-10 07:41:40