Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not find addListener method from javax.servlet.ServletContext

I am trying to change spring xml settings to pure code based setting.

So I read official documents and some posts from blogs.

e.g. http://docs.spring.io/spring-framework/docs/4.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html

An I made a code like ...

public class TestInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container)
            throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("on Startup method has called.");
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(RootConfig.class);
        container.


        //container.addListener(new ContextLoaderListener(ctx));
    }
};

A problem here. In those pages, they use addListener(new ContextLoaderListener(ctx)) method to set context. However my eclipse can not find that method from container variable.

I do not know any clue why my container variable(javax.servlet.ServletContext instance) can not read this method.

Thanks for your answer:D

P.S.

My spring version is 4.1.6.RELEASE and I include servlet3.0, spring-context, spring-webmvc on pom.xml.

========================

Maybe I got some communication problem, So I summarize this :D

  • javax.servlet.ServletContext doc clearly state that it has method addListener >> http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html
  • have to use Spring WebApplicationInitializer.onStartup(ServletContext) to set basic setting via Java source code, not XML
  • Can not load addListener from ServletContext class.

=================================

Edit. This is not error on console. However it is the only message I got. It is from eclipse toolkit.

The method addListener(ContextLoaderListener) is undefined for the type ServletContext

than recommendation is Add cast to 'container'

like image 241
Juneyoung Oh Avatar asked May 14 '15 02:05

Juneyoung Oh


1 Answers

To follow up on what @JuneyoungOh has commented, turns out that the problem is because of conflicting dependency. And these are the ways to solve this problem :

* make version 3.0.1 and artifactId 'javax.servlet-api' or
* add tomcat(in my case 7.0) to project build path and remove servlet dependency.
like image 96
kucing_terbang Avatar answered Sep 28 '22 20:09

kucing_terbang