5

I've been over Spring's documentation a couple times, but I don't seem to be able to get @Controller, etc annotations to work.

I am loading the dependencies and repositories in my POM (... are my specific values):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>...</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>org.springframework.context</artifactId>
      <version>${org.springframework.version}</version>
      <scope>runtime</scope>
   </dependency>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>org.springframework.web.servlet</artifactId>
      <version>${org.springframework.version}</version>
      <scope>runtime</scope>
   </dependency>
  </dependencies>

  <repositories>
    <repository>
        <id>com.springsource.repository.bundles.release</id>
        <url>http://repository.springsource.com/maven/bundles/release/</url>
    </repository>
    <repository>
        <id>com.springsource.repository.bundles.external</id>
        <url>http://respoitory.springsource.com/maven/bundles/external</url>
    </repository>
  </repositories>
</project>

In my web.xml, I am setting up the dispatcher servlet:

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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"
         version="2.4">

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

And under WEB-INF I have servlet-context.xml (... is my controller package):

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                        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">

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

    <mvc:annotation-driven />

    <!-- Adds prefix and suffix to returned views -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

I think I must be missing or forgetting something simple. Eclipse doesn't recognize the annotations and maven fails to build the project. Can anyone help me out here?

EDIT

For further clarification, in eclipse, adding the annotation results in:

The import org.springframework cannot be resolved

And running mvn clean install results in:

package org.springframework.stereotype does not exist

And removing the runtime scope results in the following Maven error:

error reading ...\.m2\repository\org\aopalliance\com.springsource.org.aopalliance\1.0.0\com.springsource.org.aopalliance-1.9.9.jar; error in opening zip file
mathlovingkitten
  • 168
  • 1
  • 3
  • 9

4 Answers4

6

It may have something to do with what you've set as the scope for the Spring dependencies. You've set them to "runtime", which means they aren't used for compilation and are only needed at runtime. This would explain why it won't compile in Eclipse (and is probably the problem in your Maven build as well, although you didn't specify the error).

Just remove the scope to use the default scope (the default is compile, which is most likely what you want in this case) and see if that gets you any further.

Update:

Thanks for adding the specific error you are getting.

This means Maven is trying to use a local dependency, but can't open the jar file. I've seen this happen sometimes where the jar gets corrupted during the download or only downloads partially.

Try deleting the ~/.m2/repository/org/aopalliance/com.springsource.org.aopalliance/ directory and re-run the build. This will cause Maven to re-download it and hopefully get it in a good state.

Spencer Uresk
  • 3,730
  • 26
  • 23
  • If I remove the scope, Maven fails to build, even w/o the annotations (see edit). – mathlovingkitten Mar 06 '11 at 17:12
  • Well, I deleted some of the folders and reran, and Maven seems to be okay. But now when I deploy to Tomcat I get a "dispatcherServlet is not available" error. In addition, Eclipse is still not recognizing the annotation imports. – mathlovingkitten Mar 06 '11 at 21:15
4

I found the answer to my question in a later post.

Basically, I needed to add the m2eclipse plugin to Eclipse. You can find instructions at http://m2eclipse.sonatype.org/installing-m2eclipse.html if needed. Once this is installed, you can right click on your project in Eclipse and choose Maven > Enable Dependency Management. Then eclipse will be able to recognize imports from your POM dependencies.

So in the end, I had the following. Eclipse recognized everything, Maven correct built and packaged the project, and Tomcat deployed the project successfully.

Here is my web.xml hooking up the dispatcher servlet:

<?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" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         id="CoreProject" version="3.0">

    <!-- SERVLET FOR HANDLING ALL INCOMING REQUESTS -->
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- SERVLET MAPPING FOR HANDLING ALL INCOMING REQUESTS -->
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Here's my servlet-context.xml enabling the annotations and view resolver:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                           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">

    <!-- SCANS ALL MATCHING PACKAGES FOR COMPONENTS -->
    <context:component-scan base-package="com.myBasePackage" />

    <!-- ALLOWS FOR ANNOTATION MAPPING FOR CONTROLLERS -->
    <mvc:annotation-driven />

    <!-- ADDS PREFIX AND SUFFIX TO RETURNED VIEWS -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Here's my POM file:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.myBasePackage</groupId>
  <artifactId>CoreProject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>CoreProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <!-- SERVLET DEPENDENCIES -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- SPRING DEPENDENCIES -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- TESTING DEPENDENCIES -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Hope this helps someone else.

mathlovingkitten
  • 168
  • 1
  • 3
  • 9
0

Be sure your eclipse cached version of the repo index is up to date, you can even manually update the index by using maven repo view and update it from there.

I always try the vanilla version (command line) of my maven builds, if I get an error from some IDE.

Murkantilism
  • 1,060
  • 1
  • 16
  • 31
Loki
  • 659
  • 1
  • 8
  • 18
0

use below and try, annotation handler should be used as below in your application context .xml

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0"/>
    </bean>

this will resolved stereotype errors

if not worked add below line for xsi:schemaLocation

  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
kapil das
  • 2,061
  • 1
  • 28
  • 29