Search code examples
springspring-mvcspring-annotations

org.springframework.web.WebAbblicationInitializer.onStartup won't Override interface method


I am attempting a pure java (no web.xml) Spring WebMVC project and I am running into a peculiar issue with the configuration file. I am using exactly the code from this example, where it says "A 100% code-based approach to configuration." However, eclipse says that I must remove the @Override annotation because onStartup does not "implement a superclass method."

This seems like it might be a problem with the versions of Servlet or Spring that I am using, but I don't really know what I might change either of them to which would fix that.

This is a Maven project. I'd like to solve this by adjusting my maven dependencies, rather than using Eclipse configurations.

Here is the configuration file

package com.peak15.jumpgate;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override // TODO figure out why this doesn't override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context
            = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.peak15.jumpgate");
        return context;
    }
 }

here is pom.xml

<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>jumpgate</groupId>
  <artifactId>jumpgate2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>JumpGateII</name>
  <properties>
        <jdk.version>1.8</jdk.version>
        <spring.version>3.2.13.RELEASE</spring.version>
  </properties>
  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
        </dependency> 
    </dependencies>
</project>

Solution

  • Check if you have installed jdk 1.8 -> if not, Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override, but in Java 1.5 can only be applied to methods overriding a superclass method).

    You can also add Maven compiler plugin to your pom to make sure it is working with latest Java version:

    <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>                
            </plugin>
    </plugins>
    

    Hope that causes an issue.