2011-12-16 42 views
1

recent Spring 3.1 Release中有一個@WebServlet註釋支持。但似乎我無法使用它。有沒有一些教程。這是我的web.xmlSpring 3.1和Servlet API 3.0。如何使用@WebServlet註釋

<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)。看來,碼頭支持@WebServlet 3.0版本以來8.

+0

你對「我無法使用」有什麼意思?究竟發生了什麼? – BalusC 2011-12-16 14:25:15

回答

4

按照評論:

碼頭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配置中,以便讓它掃描註釋。

相關問題