Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Annotations

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
like image 594
mathlovingkitten Avatar asked Mar 06 '11 05:03

mathlovingkitten


People also ask

How many annotations are there in Spring mvc?

Basically, there are 6 types of annotation available in the whole spring framework.

What is @API annotation in Spring boot?

The @API annotations as per the documentation states “The annotation @Api is used to configure the whole API, and apply to all public methods of a class unless overridden by @APIMethod”.


1 Answers

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.

like image 84
Spencer Uresk Avatar answered Oct 10 '22 17:10

Spencer Uresk