【发布时间】: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