NoSuchBeanDefinitionException How to initialise SessionFactory bean?

佐手、 提交于 2019-12-01 08:02:14

I would start with cleaning your configuration

This

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

Includes all this

<context:component-scan base-package="com.example" />
<context:component-scan base-package="com.example.service" />
<context:component-scan base-package="com.example.service.impl" />
<context:component-scan base-package="com.example.dao" />
<context:component-scan base-package="com.example.dao.impl.hibernate" />
<context:component-scan base-package="com.example.web.controller" />
<context:component-scan base-package="com.example.entity" />

You are using component scanning so no need to explicitly define all the beans (it would make component scanning pretty useless if that was still needed).

Next these 2 beans

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

Are already implied by <mvc:annotation-driven />

Basically leaves you with this

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

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

<mvc:annotation-driven />
<tx:annotation-driven />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:~/userdb" />
    <property name="username" value="user" />
    <property name="password" value="pswd" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.example.entity.User</value>
            <value>com.example.entity.Role</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.H2Dialect
            hibernate.show_sql=true
        </value>
  </property>
</bean>

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

Not that it will solve your problem because the error is not due to the fact that the configuration is incomplete, but the configuration is in the wrong place. The datasource, sessionFactory, transactionManager and <tx:annotation-driven .. /> must be moved to the applicationContext.xml. Nex to that your applicationContext.xml should also include a component-scan which scans for everything but controllers.

    <context:component-scan base-package="com.example">
    <context:exclude-filter type="annotation" value="org.springframework.stereotype.Controller" />
</context:component-scan">

Change the <component-scan .. /> in your servlet-context.xml to

<context:component-scan base-package="com.example" use-default-filters="false">
    <context:include-filter type="annotation" value="org.springframework.stereotype.Controller" />
</context:component-scan">

This to prevent duplicate instantiation of your @Service and @Repository beans. Which is what is happening now (both your ContextLoaderListener and DispatcherServlet are loading and creating the same beans, which would lead you to other nice exceptions and next to that duplication of memory as you have 2 instances of each bean in memory).

Somtimes what Spring actually trying to say is:

There are multiple bean definitions...

I had this problem:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'metadataGeneratorFilter' is defined

once where I had an xml file like this:

<security:custom-filter ref="metadataGeneratorFilter" before="CHANNEL_FILTER"/>

<beans:bean id="metadataGeneratorFilter"
            class="org.springframework.security.saml.metadata.  MetadataGeneratorFilter">
    <beans:constructor-arg ref="metadataGenerator"/>
</beans:bean>
ZaoTaoBao

If you use @Autowired you don't need to declare like a property at the context. But it must be at the context--->NoSuchBeanDefinition--> don't find it.

<bean id="userService" class="com.example.service.impl.UserServiceImpl">
    <property name="userDAO" ref="userDAO" /> 
</bean>

try to remove userDAO from property context.--if you autowired it, you don't need it at context.

but the problem seems to be at this bean:

<bean id="userDAO" class="com.example.dao.impl.hibernate.HibernateUserDAO" />

can't inject the dependency of sessionFactory, but everything seems to be correct :( why don't you try to remove @autowired of session factory, put get/set an add property ref at context in hibernateUserDao, just for testing what happens?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!