没有找到类型为[org.hibernate.SessionFactory]的匹配bean用于依赖

时间:2013-03-29 08:46:24

标签: spring hibernate maven sessionfactory

我是Spring MVC的初学者。我遇到一个问题“找不到类型为[org.hibernate.SessionFactory]的匹配bean依赖:预计至少有一个bean有资格作为此依赖项的autowire候选者。”并花费很多时间。< / p>

我使用了maven构建管理。我的项目有一个项目,包括2个模块:springcore(jar)和springmvc(web app)。而springmvc(网络应用程序)一直是依赖弹簧的核心。在模块spring核心中,我定义了所有包,例如dom,dao,service及其实现,我还在src / main / resources中定义了applicationContext.xml:

的applicationContext.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"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Activates scanning of @Autowired -->
    <context:annotation-config />

    <context:component-scan base-package="com.netvn" /> 

    <bean id="propertyConfigurer"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
         p:location="classpath:jdbc" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
            p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"                    p:password="${jdbc.password}" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
                    <list>
                         <value>com.netvn.dom.Contact</value>
                    </list>
            </property>
            <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

ContactDaoImpl.java

@Repository
public class ContactDaoImpl implements ContactDao {
    private Log log = LogFactory.getLog(ContactDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    public List<Contact> listContact() {
        Session session = sessionFactory.getCurrentSession();
        Query q = session.createQuery("select g from Contact g order by id desc");
        List<Contact> contactList= q.list(); 
        return contactList; 
    }
}

ContactServiceImpl.java

@Service
@Transactional
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactDao contactDao;

    @Transactional(propagation=Propagation.REQUIRED)
    public List<Contact> listContact(){
        return contactDao.listContact();
    }
}

在springmvc(web app)中,我定义了web.xml和dispatcher-servlet.xml

调度-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc 
                 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                 http://www.springframework.org/schema/context
                 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config />

    <mvc:annotation-driven />
    <context:component-scan base-package="com.netvn.dao" />
    <context:component-scan base-package="com.netvn.service" />
    <context:component-scan base-package="com.netvn.controller" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

 </beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                 classpath*:*.xml
             </param-value>
    </context-param>
    <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
</web-app>

当我尝试在Tomcat 6.0服务器上运行它时,这是我的问题

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.netvn.dao.impl.ContactDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    ... 86 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)

有人可以告诉我,当我配置SessionFactory时,Web应用程序上下文无法创建SessionFactory或者出现任何问题的原因是什么。

任何支持将不胜感激。

3 个答案:

答案 0 :(得分:1)

一般问题是类路径中的配置/类是否缺少您自己的库。再次检查导入和配置是否正确:

  1. 使用类路径扫描来实现实体类的自动检测:
    S1-IN

  2. 或者最重要的是:AnnotationSessionFactoryBean类需要Hibernate 3.2或更高版本,并且存在Java Persistence API和Hibernate Annotations附加组件。

  3. 参考:https://docs.spring.io/spring/docs/3.2.8.RELEASE/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

答案 1 :(得分:0)

尝试直接将应用程序上下文导入dispatcher-servlet.xml(正确加载),如下所示。

<import resource="classpath:WEB-INF/applicationContext.xml" />

答案 2 :(得分:0)

您的sessionfactory未正确初始化。

在错误的位置和内容中读取您的错误。

  

无法自动装配字段:private org.hibernate.SessionFactory

您需要正确初始化SessionFactory

现在如何测试这个非常简单。首先删除代码中添加的sessionfactory。测试数据源是否正确地进行了初始化

将数据源添加为autowire。 @Autowired DataSource dataSource;

在您的方法中,您可以轻松地执行dataSource.getConnection。如果你能够使用普通的jdbc进行测试,那么你就更近了一步。

如果您的数据源配置正确,您可以测试您的sessionfactory作为第二步。

我不认为conext扫描会给你任何问题,因为它说autowired失败了。因此,您的数据源和sessionfactory bean未在xml中正确设置