在servlet中如何使用被Spring管理的service

  首先真的很感谢这篇文章的作者,我在这次的项目中遇到了这个问题,看了很多网上的资料都没解决,直到看到这篇文章才解决,我用的是方法2.2。

 刚开始我把service设置成servlet的成员变量,不知道为什么不行,这种方式让这个servlet都无法使用,希望知道原因的朋友能告知。以至于我以为这篇文章也是不行,感到灰心丧气,两天都没解决这个问题。刚刚我再次尝试了一次,把service放到方法里,设置成局部变量,没想到竟然成功初始化了,而且成功调用后台取到值返回,让我差点“喜极而泣”,真的很开心,明天就可以接着把项目往下做了,具体内容参照以下。再次感谢作者!

-------------分割线------------------------

原文

我的使用场景是SpringMvc+MyBatis,我总结了以下两种方式,三种方法。两种方式指的是采用注入方式和获取spring管理的bean。三种方法指的是,代理注入、硬编码获取bean和实现ApplicationContextAware接口获取bean。

第一种方式:采用注入方式。

编写一个代理类,代码如下:

Java代码   收藏代码
  1. @SuppressWarnings("serial")  
  2. public class ProxyServlet extends HttpServlet {  
  3.   
  4.     @Override  
  5.     public void service(ServletRequest req, ServletResponse res)  
  6.             throws ServletException, IOException {  
  7.         proxyServlet.service(req, res);  
  8.     }  
  9.   
  10.     @Override  
  11.     public void init() throws ServletException {  
  12.         this.targetBean = getServletName();  
  13.         getServletBean();  
  14.         proxyServlet.init(getServletConfig());  
  15.     }  
  16.   
  17.     private String targetBean;  
  18.       
  19.     private Servlet proxyServlet;  
  20.       
  21.     private void getServletBean(){  
  22.         WebApplicationContext wac = WebApplicationContextUtils  
  23.                 .getRequiredWebApplicationContext(getServletContext());  
  24.         this.proxyServlet = (Servlet) wac.getBean(targetBean);  
  25.     }  
  26. }  

    然后编写需要注入service的servlet,代码如下:

 

 

 

Java代码   收藏代码
  1. @Component  
  2. public class MemcacheServlet extends HttpServlet {  
  3.     private static final long serialVersionUID = 1L;  
  4.       
  5.     @Autowired  
  6.     private GlobalCacheService globalCacheService;  
  7.          
  8.     /** 
  9.      * @see HttpServlet#HttpServlet() 
  10.      */  
  11.     public MemcacheServlet() {  
  12.         super();  
  13.           
  14.     }  
  15.   
  16.     /** 
  17.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  18.      */  
  19.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  20.         String flag = request.getParameter("flag");  
  21.         globalCacheService.test();  
  22.         if("q".equals(flag)){  
  23.             //取缓存  
  24.             String name = (String)globalCacheService.getCacheValue("_name1", Object.class);  
  25.             System.out.println("执行取缓存操作: " + name);  
  26.         }else if("f".equals(flag)){  
  27.             //放缓存  
  28.             String username = request.getParameter("username");  
  29.             globalCacheService.deleteCacheValue("_name1");  
  30.             if(!StringUtil.isBlank(username)){  
  31.                 System.out.println("执行存缓存操作: " + username);  
  32.                 globalCacheService.setCacheValue("_name1", username, 28800);  
  33.             }else{  
  34.                 System.out.println("执行存缓存操作: " + username);  
  35.                 globalCacheService.setCacheValue("_name1""lzx"28800);  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  42.      */  
  43.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  44.         response.setContentType("text/html");  
  45.         this.doGet(request, response);  
  46.     }  
  47.   
  48. }  

    最后在web.xml中配置如下:

Xml代码   收藏代码
  1. <servlet>  
  2.     <servlet-name>memcacheServlet</servlet-name>  
  3.     <servlet-class>com.hsis.core.servlet.web.ProxyServlet</servlet-class>  
  4.   </servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>memcacheServlet</servlet-name>  
  7.     <url-pattern>*.to</url-pattern>  
  8.   </servlet-mapping>  

    第二种方式获取spring管理的service,采用硬编码或者实现AplicationContextAware接口。

      2.1 采用硬编码方法如下:

     

Java代码   收藏代码
  1. ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);  
  2. 或者  
  3. WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);  
  4. LzxService lzxService = (LzxService)wac.getBean("lzxService");  

    注:WebApplicationContext继承的ApplicationContext。

      2.2 采用实现AplicationContextAware接口方法,首先创建一个类SpringContextUtil,代码如下:

Java代码   收藏代码
  1. import org.springframework.beans.BeansException;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.ApplicationContextAware;  
  4.   
  5. public class SpringContextUtil implements ApplicationContextAware {  
  6.       
  7.     private static ApplicationContext applicationContext;  
  8.   
  9.     @Override  
  10.     public void setApplicationContext(ApplicationContext applicationContext)  
  11.             throws BeansException {  
  12.         applicationContext = applicationContext;  
  13.     }  
  14.       
  15.     public static ApplicationContext getApplicationContext(){  
  16.         return applicationContext;  
  17.     }  
  18.       
  19.     public static Object getBean(String name){  
  20.         return applicationContext.getBean(name);  
  21.     }  
  22.       
  23.     public static <T> T getBean(String name, Class<T>  requiredClass){  
  24.         return applicationContext.getBean(name, requiredClass);  
  25.     }  
  26.   
  27. }  

 applicationContext.xml中配置一下:

 

 

 

Java代码   收藏代码
  1. <bean class=”SpringContextUtil” />  

 

     在servlet中使用即可:

Java代码   收藏代码
  1. globalCacheService = (GlobalCacheService) SpringContextUtil.getBean("globalCacheService", GlobalCacheService.class);  

     注:实现Aware接口的类,初始化之后可以获取对应的资源,实现ApplicationContextAware接口的bean,初始化后被注入applicationContext实例。

      如果使用ClassPathXmlApplicationContext、FileSystemClassPathXmlApplicationContext和FileSystemXmlApplicationContext等对象去加载Spring配置文件,会生成一个新的application对象,这样会产生冗余。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值