【问题标题】:Spring 3.1 and Servlet API 3.0. How to use @WebServlet annotationSpring 3.1 和 Servlet API 3.0。如何使用@WebServlet 注解
【发布时间】:2011-12-16 14:22:18
【问题描述】:

recent Spring 3.1 Release 中有一个@WebServlet 注释支持。但似乎我不能使用它。有没有一些教程。这是我的 Web.xml

<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0" >
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/beans.xml</param-value>
   </context-param>
   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
</web-app>

...还有我的 beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


    <context:annotation-config />
    <context:component-scan base-package="xxx.xxxxxxxx.xxx" />

    <!-- aop:aspectj-autoproxy aspectj-weaving="on" / -->

    <bean
        class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

    <!-- bean class="xxx.xxxx.xxx.xxx.MessageProfileLogger" 
        / -->
</beans>

servlet 源...

@WebServlet(urlPatterns = { "/service/*" })
public class RequestServlet extends HttpServlet {
    {
        System.out.println("RequestServlet is initialized");
    }
...
}

所需的行为 - 带注释的 servlet 已初始化并开始侦听请求。

真正发生了什么 - 什么都没有,就像 Servlet 没有注释一样。

附加信息 - servlet 的路径在组件扫描中,其他 DI 内容也可以。

我没有企业容器,我用这个教程为Spring+Embeded Jetty 工作到现在。

编辑:似乎我使用的是旧版本的 Jetty (7.0.2)。似乎 Jetty 从版本 8 开始就支持 @WebServlet 3.0。

【问题讨论】:

  • “我不能使用它”到底是什么意思?究竟会发生什么?
  • @BalusC 你说得对,我会编辑我的问题。
  • 嗯,我不做 Spring,所以我不确定 AbstractServlet 是否正确,这在标准 Servlet API 中通常是 HttpServlet(尽管我相信 AbstractServlet 扩展自它在封面下)。但是您是否正在运行 Servlet 3.0 兼容的容器,例如 Tomcat 7、Glassfish 3 等?
  • AbstractServlet 是我编写的 HttpServlet 的子类。这没什么特别的。
  • 您使用的是哪个 Jetty 版本?只有 8 或更新版本支持 Servlet 3.0 API。

标签: java spring jakarta-ee


【解决方案1】:

根据 cmets:

码头 7.0.2

为了能够使用 Servlet 3.0 API,您需要一个支持 Servlet 3.0 API 的容器。例如至少 Tomcat 7、Glassfish 3、JBoss AS 6、WebSphere AS 8、Jetty 8 等。单独更改 web.xml 以符合 Servlet 3.0 不会更改容器的内部类以神奇地支持 Servlet 3.0 API。当web.xml 中提到不支持的版本时,大多数容器甚至会退回到最低兼容性模式。

如果您使用的是嵌入式 Jetty 8,请不要忘记将 AnnotationConfiguration 添加到 Jetty 配置中,以便让它扫描注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2012-01-05
    • 2016-02-27
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多