Java Servlets 注入spring bean

2017-01-13 15:07:00来源:csdn作者:doctor_who2004人点击


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")注解方法,属性
@Autowiredprivate 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.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;@Configurable
public 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);}
}
属性:
@Autowiredprivate TestManagerImpltestManagerImpl;
必须是@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/>
效果图:

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台