这是错误: 引起:
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 :(得分:3)
在这里你会对两件事情感到困惑
JPA EntityManagerFactory
EntityManagerFactory
和EntityManager
。它们由JPA标准定义。
<强>的SessionFactory 强>
SessionFactory
和Session
是特定于休眠的。
两者都有Pros&amp;缺点,因为你使用SessionFactory
(而不是JPA),你根本不应该使用persistence.xml。
JPA EntityManagerFactory
如果您想在JP中使用JPA,则需要将persistence.xml
放入源文件夹中的META-INF
文件夹中,并在LocalContainerEntityManagerFactory
applicationContext.xml
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name = "persistenceUnitName" value = "MyJPA" />
</bean>
然后,您可以使用EntityManager
:
@PersistenceContext
注入Spring bean
@PersistenceContext
private EntityManager em;
<强>的persistence.xml 强>
此文件通常包含与您的数据库相关的详细信息,例如 连接字符串及其各自的用户名和密码 包括其他ORM相关信息。可以放置这些细节 在其他地方,所以你不需要明确地拥有一个,但是 拥有这样的文件通常会产生所有与持久性相关的信息 在一个地方提供查找某些设置和 配置更容易。
<强>的SessionFactory 强>
如果您想使用SessionFactory
,则必须将sessionFactory
bean声明为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
注入到您的spring bean中
@Autowired
private SessionFactory sessionFactory;
希望这能帮助您解决问题...
答案 1 :(得分:1)
您需要在DAO的顶部添加@Component
(或类似的Spring注释)。您的web.xml中还有<context:annotation-config/>
和<context:component-scan base-package="whatever"/>
吗?你必须告诉Spring出去嗅探注释。
答案 2 :(得分:1)
您必须在DAOImpl类的顶部添加@Repository
(Spring注释)。此外,您必须插入<context:component-scan base-package="packageName"/>
来扫描所有项目并在项目中找到mvn组件,并在spring配置中<mvc:annotation-driven />
(以便使用spring注释),以便它可以理解您的spring注释。 / p>