用Spring将Service注入到Servlet中

来源:互联网 发布:js自动获取input光标 编辑:程序博客网 时间:2024/05/21 14:03
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/45696707

解决方法有两种(推荐使用第二种)

方法一:
 
直接重写Servlet的Init()方法,代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void init(ServletConfig servletConfig) throws ServletException {  
  2.     ServletContext servletContext = servletConfig.getServletContext();  
  3.     WebApplicationContext webApplicationContext = WebApplicationContextUtils  
  4.             .getWebApplicationContext(servletContext);  
  5.     AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext  
  6.             .getAutowireCapableBeanFactory();  
  7.     autowireCapableBeanFactory.configureBean(this, BEAN_NAME);  
  8. }  

这里的BEAN_NAME即为我们需要注入到Spring容器中的服务,但这并不是一个好的方法,因为我们需要在每一个Servlet中都进行这样的操作。
 
方法二:
 
我们可以写一个类似于“org.springframework.web.struts.DelegatingRequestProcessor”的委托的Bean,然后通过配置的方法把我们的服务注入到servlet中,具体方法如下,
 
Step 1:编写委托类DelegatingServletProxy

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.telek.pba.base.util;  
  2.    
  3. import java.io.IOException;  
  4.    
  5. import javax.servlet.GenericServlet;  
  6. import javax.servlet.Servlet;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10.    
  11. import org.springframework.web.context.WebApplicationContext;  
  12. import org.springframework.web.context.support.WebApplicationContextUtils;  
  13.    
  14.    
  15. /** 
  16.  * 以下是类似org.springframework.web.struts.DelegatingRequestProcessor的一个委托 
  17.  * 用于通过配置的方法,在Servlet中注入Service 
  18.  * @author liyinwei 
  19.  *  
  20.  */  
  21. public class DelegatingServletProxy extends GenericServlet{  
  22.    
  23.     /** 
  24.      *  
  25.      */  
  26.     private static final long serialVersionUID = 1L;  
  27.     private String targetBean;  
  28.     private Servlet proxy;  
  29.    
  30.     @Override  
  31.     public void service(ServletRequest req, ServletResponse res)  
  32.             throws ServletException, IOException {  
  33.         proxy.service(req, res);  
  34.     }  
  35.    
  36.     /** 
  37.      * 初始化 
  38.      */  
  39.     public void init() throws ServletException {  
  40.         this.targetBean = getServletName();  
  41.         getServletBean();  
  42.         proxy.init(getServletConfig());  
  43.     }  
  44.    
  45.     /** 
  46.      * 获取Bean 
  47.      */  
  48.     private void getServletBean() {  
  49.         WebApplicationContext wac = WebApplicationContextUtils  
  50.                 .getRequiredWebApplicationContext(getServletContext());  
  51.         this.proxy = (Servlet) wac.getBean(targetBean);  
  52.     }  
  53.    
  54. }  

Step 2:修改Web.xml配置
 
在纯Servlet模式下,我们的配置方式如下

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <servlet>  
  2.   <description>活动发起模块活动查询分页Servlet</description>  
  3.   <display-name>launchActivityQueryServlet</display>  
  4.   <servlet-name>LaunchActivityQueryServlet</servlet-name>  
  5.   <servlet-class>com.telek.pba.launch.servlet.LaunchActivityQueryServlet</servlet-class>  
  6. <servlet>  
  7.    
  8. <servlet-mapping>  
  9.   <servlet-name>LaunchActivityQueryServlet</servlet-name>  
  10.   <url-pattern>/servlet/launch/LaunchActivityQueryServlet</url-pattern>  
  11. </servlet-mapping>  
  12. </servlet>  

如果采用我们这种代理的方法,则配置应该修改为:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <servlet>  
  2.   <description>活动发起模块活动查询分页Servlet</description>  
  3.   <display-name>launchActivityQueryServlet</display>  
  4.   <servlet-name>launchActivityQueryServlet</servlet-name>  
  5.   <servlet-class>com.telek.pba.base.util.DelegatingServletProxy</servlet-class>  
  6. <servlet>  
  7.    
  8. <servlet-mapping>  
  9.   <servlet-name>launchActivityQueryServlet</servlet-name>  
  10.   <url-pattern>/servlet/launch/LaunchActivityQueryServlet</url-pattern>  
  11. </servlet-mapping>  
  12. </servlet>   

注意:默认情况下,Servlet的配置中,LaunchActivityQuery的首字母一般为大写,而我们的标题中已注明,我们采用Spring的注解模式,如果是自动扫描注解的话,默认情况下,注解的value值为首字母小写,即:launchActivityQuery,因此,在我们新的配置中,要注意将首字母改为小写,否则会报无法找到Bean的错误。
 
Step 3:至此,我们就可以像SSH的注入方式一样,注入Servlet了,以下是个小示例:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.telek.pba.launch.servlet;  
  2.    
  3. import java.io.IOException;  
  4.    
  5. import javax.annotation.Resource;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10. import org.springframework.stereotype.Component;  
  11.    
  12. import com.telek.pba.base.model.PbaUserInfo;  
  13. import com.telek.pba.launch.dao.IPbaActivityInfoCurrentDAO;  
  14.    
  15. @Component  
  16. public class LaunchActivityQueryServlet extends HttpServlet {  
  17.    
  18.     private static final long serialVersionUID = 1L;  
  19.    
  20.     //注入IPbaActivityInfoCurrentDAO  
  21.     @Resource  
  22.     private IPbaActivityInfoCurrentDAO pbaActivityInfoCurrentDAO;  
  23.    
  24.    
  25.     /** 
  26.      * Constructor of the object. 
  27.      */  
  28.     public LaunchActivityQueryServlet() {  
  29.         super();  
  30.     }  
  31.    
  32.     /** 
  33.      * Destruction of the servlet. <br /> 
  34.      */  
  35.     public void destroy() {  
  36.         super.destroy(); // Just puts "destroy" string in log  
  37.         // Put your code here  
  38.     }  
  39.    
  40.     /** 
  41.      * The doGet method of the servlet. <br /> 
  42.      * 
  43.      * This method is called when a form has its tag value method equals to get. 
  44.      *  
  45.      * @param request the request send by the client to the server 
  46.      * @param response the response send by the server to the client 
  47.      * @throws ServletException if an error occurred 
  48.      * @throws IOException if an error occurred 
  49.      */  
  50.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  51.             throws ServletException, IOException {  
  52.         //sth to do  
  53.     }  
  54.    
  55.     /** 
  56.      * The doPost method of the servlet. <br /> 
  57.      * 
  58.      * This method is called when a form has its tag value method equals to post. 
  59.      *  
  60.      * @param request the request send by the client to the server 
  61.      * @param response the response send by the server to the client 
  62.      * @throws ServletException if an error occurred 
  63.      * @throws IOException if an error occurred 
  64.      */  
  65.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  66.             throws ServletException, IOException {  
  67.         //sth to do  
  68.     }  
  69.    
  70.     /** 
  71.      * Initialization of the servlet. <br /> 
  72.      * 
  73.      * @throws ServletException if an error occurs 
  74.      */  
  75.     public void init() throws ServletException {  
  76.         // Put your code here  
  77.     }  
  78.    
  79. }  

最后,请留心在Spring配置文件中,配置上自动扫描包的路径:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <context:component -scan base-package="com.telek.pba.*.dao.impl,  
  2.                     com.telek.pba.*.service.impl,  
  3.                     com.telek.pba.*.servlet"></context:component>  

大功告成!
0 0
原创粉丝点击