I would start by clearing your configuration
it
<context:component-scan base-package="com.example" />
Includes it all
<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 you donβt need to explicitly identify all the beans (this would make scanning components quite useless if it were necessary).
Further these 2 beans
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
Already implies <mvc:annotation-driven />
You basically stay 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 this solves your problem, because the error is not related to the fact that the configuration is incomplete, but the configuration is in the wrong place. datasource
, sessionFactory
, transactionManager
and <tx:annotation-driven .. />
need to be migrated to applicationContext.xml
. Nex to the point that your applicationContext.xml should also include a scan component that scans everything but the controllers.
<context:component-scan base-package="com.example"> <context:exclude-filter type="annotation" value="org.springframework.stereotype.Controller" /> </context:component-scan">
Change <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 will prevent duplicate instances of your @Service
and @Repository
beans. This is what is happening now (both your ContextLoaderListener
and DispatcherServlet
load and create the same beans, which will lead you to other nice exceptions and next to this duplication of memory, since you have 2 instances of each bean in memory).