繁体   English   中英

Spring 3.1和Servlet API 3.0。 如何使用@WebServlet注释

[英]Spring 3.1 and Servlet API 3.0. How to use @WebServlet annotation

最近的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。

根据评论:

Jetty 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配置中,以便让它扫描注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM