Java Servlets 注入spring bean
来源:互联网 发布:软件测试的目的 编辑:程序博客网 时间:2024/04/19 07:50
Java Servlets 注入spring bean
方法一:HttpRequestHandler
此方法要实现HttpServlet,我们不能直接实现HttpServlet接口,而去实现HttpRequestHandler接口:
package com.xx.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.HttpRequestHandler;import com.googlecode.jsonrpc4j.JsonRpcServer;@Component("myServlet")public class MyServlet implements HttpRequestHandler { @Autowired private TestManagerImpl testManagerImpl; @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); requestURI = requestURI.substring(requestURI.lastIndexOf("/") + 1); JsonRpcServer server = testManagerImpl.getServer(requestURI); server.handle(request, response); }}
此类被spring托管,我们用@Component("myServlet")注解方法,属性
@Autowired
private TestManagerImpl testManagerImpl;
private TestManagerImpl testManagerImpl;
是我们需要注入的对象。
我们还需要在web.xml中配置此伪servlet:
<servlet> <display-name>myServlet</display-name> <servlet-name>myServlet</servlet-name> <servlet-class> org.springframework.web.context.support.HttpRequestHandlerServlet </servlet-class></servlet><servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/myurl/*</url-pattern></servlet-mapping>
注意:servlet-class的配置类是:
<servlet-class>
org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
方法二:利用org.springframework.beans.factory.annotation.Configurable 注解与 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this) 实现
package com.xxx.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.web.context.support.SpringBeanAutowiringSupport;import com.googlecode.jsonrpc4j.JsonRpcServer;@Configurablepublic class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Autowired private TestManagerImpl testManagerImpl; @Override public void init() throws ServletException { SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); requestURI = requestURI.substring(requestURI.lastIndexOf("/") + 1); JsonRpcServer server = testManagerImpl.getServer(requestURI); server.handle(request, response); }}
属性:
@Autowired
private TestManagerImpl testManagerImpl;
private TestManagerImpl testManagerImpl;
必须是@Autowired注解,此属性是我们需要注入的对象。
web.xml的配置如下:
<servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>com.xxx.controller.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet/*</url-pattern> </servlet-mapping>
标准配置Servlet方式。
还需一下配置spring xml:
<context:component-scan base-package="com.xxx"/>
<context:annotation-config/>
<context:spring-configured/>
<context:annotation-config/>
<context:spring-configured/>
效果图:
1 0
- Java Servlets 注入spring bean
- Java Spring Tutorial -- 注入Bean
- Spring Bean java显式配置类注入Bean
- Spring的bean注入
- Spring自动注入Bean
- Spring注入内部Bean
- servlet spring bean注入
- SPring Bean注入 job
- Spring注入Bean
- Spring自动注入Bean
- Spring bean 集合注入
- 【Spring】注解注入bean
- Spring Bean注入Propertites
- [Spring] 注入Bean属性
- Spring容器注入bean
- Spring注解注入bean
- spring bean注入异常
- Spring bean注入方式
- Sys::AlarmCall
- 【BZOJ 2002】 [Hnoi2010]Bounce 弹飞绵羊 LCT
- linux C语言之called object ‘maze’ is not a function or function pointer printf("%d\t", maze(i, j))
- Shell 脚本实践
- Android多屏幕适配-平板
- Java Servlets 注入spring bean
- 【Linux】进度条与缓冲区
- 使用mock进行数据拦截实战感悟
- POP3、IMAP和SMTP
- 汇编(指令cpuid)获取cpu信息
- Exporting Your App for Testing
- 用foreach来遍历数组
- 地形平滑算法
- 快速排序