无法从javax.servlet.ServletContext中找到addListener方法
我正在尝试将SpringXML设置更改为纯基于代码的设置 所以我读了官方文件和博客上的一些帖子 e、 g 我做了一个代码像无法从javax.servlet.ServletContext中找到addListener方法,java,spring,spring-mvc,servlets,methods,Java,Spring,Spring Mvc,Servlets,Methods,我正在尝试将SpringXML设置更改为纯基于代码的设置 所以我读了官方文件和博客上的一些帖子 e、 g 我做了一个代码像 public class TestInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) throws ServletException { // TODO
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));
}
};
这里有个问题。在这些页面中,他们使用addListener(新ContextLoaderListener(ctx))
方法来设置上下文。但是,我的eclipse无法从容器
变量中找到该方法
我不知道为什么我的容器变量(javax.servlet.ServletContext实例)不能读取这个方法
谢谢你的回答:D
附言
我的spring版本是4.1.6.RELEASE
,我在pom.xml
上包括servlet3.0、spring上下文、spring webmvc
========================
也许我有一些沟通问题,所以我总结如下:D
- servlet.ServletContext文档清楚地声明它具有
>addListener
- 必须使用Spring
通过Java源代码而不是XML设置基本设置WebApplicationInitializer.onStartup(ServletContext)
- 无法从
类加载ServletContext
addListener
The method addListener(ContextLoaderListener) is undefined for the type ServletContext
下面的建议是将cast添加到“container”以跟进@juneyongoh的评论,结果发现问题是因为相互冲突的依赖关系。以下是解决这个问题的方法:
* 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.
在我的例子中,问题是因为Spring支持依赖于“javax.servlet”,我只是将其排除在外:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
<version>${spring-support.version}</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
org.springframework
弹簧支架
${spring support.version}
servlet api
javax.servlet
在我的案例中:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
javax.servlet
servlet api
2.5
假如
注意,artifactId是servlet api,而不是javax.servlet-api
我创建了一个遗留的MVC项目,这就是为什么我有这个包。当我尝试将.xml配置转换为Java时,我遇到了这个问题
当然,这与问题中的不同,但它显示为google搜索的第一个结果。这不是有效的Java。请编辑并修复代码。同时剪切并粘贴完整的实际错误消息,不要转述。@JimGarrison您的意思是
容器。
?我把它写进代码中,因为那一行是我的问题发生的地方。除此之外,它是非常有效的java。如果你不介意,请访问我在原始帖子中提供的链接。而RootConfig
只是另一个java文件,它在类定义上具有@Configuration
。谢谢。@JimGarrison这不是关于错误消息的。文档中说参数变量可以使用addListener
方法。然而,在代码中,这是不可能的。它只是一个错误,所以甚至不能compile@JuneyoungOh您好,我知道您不能编译,但您能显示编译器显示的消息吗?或者也可以显示错误的屏幕截图。感谢提供更多信息。我希望这会有很大帮助:-)谢谢。很好的洞察力