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).