Spring @WebListener不可用

时间:2014-09-04 06:55:31

标签: java spring spring-mvc java-ee

我使用Spring 4并希望使用ServletContextListener。当我向类eclipse中添加@WebListener注释时,IDE会显示编译错误。此外,实现还显示编译错误。

public class MyContextListener implements ServletContextListener { // this shows compiler

背后的

ServletContextListener错误

我也添加了spring-web。 spring版本是4.0.3.RELEASE

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>            
    </dependency>

任何人都可以告诉我问题出在哪里。

3 个答案:

答案 0 :(得分:2)

解决Eclipse中的编译问题:

将最新版本的servlet-api.jar添加到构建路径中。

在你的pom中添加servlet依赖项:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
  </dependency>

另见:

Maven compilation failes (but compilation under Eclipse is successful)

答案 1 :(得分:1)

@javax.servlet.annotation.WebListener注释是servlet-api not spring框架的一部分,如果您使用Tomcat,请转到右侧面板中的Project -> Properties -> Project Facets选择RunTimes,然后检查您正在使用的Tomcat服务器实例然后应用 之后,如果您转到java build path -> libraries,您会看到Apache Tomcat vXX已添加到图书馆 或者您可以下载servlet-api并将其添加到您的类路径

答案 2 :(得分:0)

@WebListener是来自javax.servlet包的注释,你是否在maven pom文件中添加了它?