You have a couple of problems (the following are apparent from your post).
- Mixing jars of different versions of Spring
- Wrong location of annotations
- Duplication in your applicationcontext.
- Using versioned XSD files.
- Conflicting versioning in web.xml
1. Mixing Jars of different versions of Spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
Your pom is littered with jars from different spring versions (3.0.5, 4.0.1, 4.0.3 and 4.0.3). If you want trouble then that is the way to go. Always stick to a single version of a framework and don't mix jars from different versions. This isn't only for Spring but goes for every (multi jar) framework out-there.
Use the power of maven to help you, specify a property to hold the version of Spring you want to use and use that property.
<properties>
<spring.version>4.0.4.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
You might need to add a couple more depending on which dependencies Spring Data JPA is pulling in, if that has different versions.
2. Wrong location of annotations
@Service
public interface Store {
boolean get();
}
Annotations aren't inherited from interfaces so putting the annotation on the interface is pretty much useless. Spring has some hacks in place for specific annotations like @Transactional
but not for this. Move the annotation to the concrete implementation and don't put it on the interface.
@Service
public class Controller {
@Autowired
private Store storeImpl;
public void use() {
storeImpl.get();
}
}
I don't see anything here no @Controller
or what so ever also no @RequestMapping
so I'm not sure how you think that Spring detects and handles this class.
3. Duplication in your applicationcontext.
<context:annotation-config />
remove it from your context as that is already implied by the use of <context:component-scan />
.
<mvc:annotation-driven />
should be only in the xml file loaded by the DispatcherServlet
in your case (if you have posted actual code) that should be the dispatcher-servlet.xml
.
You have the same <context:component-scan .. />
element in both your xml files, this will ead to bean duplication. The files loaded by the ContextLoaderListener
should load anything but @Controllers
and the DispatcherServlet
should load only @Controllers
and ignore anything else. To accomplish that use include/exclude filters when component scanning.
Use this for the ContextLoaderListener
.
<context:component-scan base-package="com.mycompany.app">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype. Controller" />
<context:component-scan>
Use this for the DispatcherServlet
.
<context:component-scan base-package="com.mycompany.app" use-default-filters="false >
<context:include-filter type="annotation" expression="org.springframework.stereotype. Controller" />
<context:component-scan>
Also don't import the configuration of the DispatcherServlet
into the ContextLoaderListener
as this will again lead to bean duplication.
4. Using versioned XSD files.
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
Your schema locations use versioned XSD files, it is recommended to use versionless xsd files. This will ensure that the xsd version belonging to your current spring version will be loaded.
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
5. Conflicting versioning in web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
What is it you want version 2.3 of 2.4? You including servlet-api 2.5 in your pom and I would expect that you want version 3.0. Change your header (and maven dependency) accordingly.
Change your web.xml
header to the following.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Update your pom.xml to include the servlet 3.0 spec.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>