在servlet中获取spring管理的bean

来源:互联网 发布:阿里云主机纪录怎么填 编辑:程序博客网 时间:2024/05/19 13:21
  1. public class Notify_Url extends HttpServlet {  
  2.     private static final long serialVersionUID = 201110091627L;  
  3.   
  4.     // 保存sysVarClient 引用,通过它获得 key  
  5.     private ISystemVar sysVarClient;  
  6.   
  7.     public Notify_Url() {  
  8.         super();  
  9.     }  
  10.   
  11.     // 调用 doPost 方法  
  12.     protected void doGet(HttpServletRequest request,  
  13.             HttpServletResponse response) throws ServletException, IOException {  
  14.         doPost(request, response);// 调用 doPost 方法  
  15.     }  
  16.   
  17.     // servlet初始化时从spring加载 sysVarClient 通过sysVarClient 获得 key  
  18.     @Override  
  19.     public void init() throws ServletException {  
  20.   
  21.         super.init();  
  22.         ServletContext servletContext = this.getServletContext();  
  23.   
  24.         WebApplicationContext ctx = WebApplicationContextUtils  
  25.                 .getWebApplicationContext(servletContext);  
  26.   
  27.         sysVarClient = (ISystemVar) ctx.getBean("sysVarClient");  
  28.   
  29.     }  
  30.   
  31.     protected void doPost(HttpServletRequest request,  
  32.             HttpServletResponse response) throws ServletException, IOException {  
  33.         //do Something ....   
  34.     }  
  35.   
  36. }  
原创粉丝点击