找不到javax.servlet.ServletContext中的addListener方法

Jun*_* Oh 5 java methods spring servlets spring-mvc

我试图将spring xml设置更改为纯代码设置.

所以我阅读了官方文档和博客上的一些帖子.

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

我做了一个像......的代码

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));
    }
};
Run Code Online (Sandbox Code Playgroud)

这是一个问题.在这些页面中,他们使用addListener(new ContextLoaderListener(ctx))方法来设置上下文.但是我的日食无法从container变量中找到该方法.

我不知道为什么我的容器变量(javax.servlet.ServletContext实例)无法读取此方法.

谢谢你的回答:D

PS

我的春天的版本是4.1.6.RELEASE,我包括servlet3.0, spring-context, spring-webmvcpom.xml.

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

也许我遇到了一些沟通问题,所以我总结一下:D

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

编辑.这不是控制台上的错误.然而,这是我得到的唯一信息.它来自eclipse工具包.

The method addListener(ContextLoaderListener) is undefined for the type ServletContext
Run Code Online (Sandbox Code Playgroud)

比推荐的 Add cast to 'container'

kuc*_*ang 7

为了跟进@JuneyoungOh评论的内容,结果发现问题是由于依赖性冲突造成的.这些是解决这个问题的方法:

* 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.
Run Code Online (Sandbox Code Playgroud)