Web开发中获取Spring的ApplicationContext的几种方式



Web开发中获取Spring的ApplicationContext的几种方式          

文章来自:点击打开链接

在 WEB 开发中,获取到由 Spring 进行管理的某些 Bean,通常采用下面方式来获取

1、通过set注入方式

Java代码 复制代码  收藏代码
  1. <span>private ProjectStageHandler projectStageHandler;  
  2.   
  3. public void setProjectStageHandler(ProjectStageHandler projectStageHandler) {  
  4.   this.projectStageHandler = projectStageHandler;  
  5.  }  
  6.   
  7. </span>  
<span>private ProjectStageHandler projectStageHandler;

public void setProjectStageHandler(ProjectStageHandler projectStageHandler) {
  this.projectStageHandler = projectStageHandler;
 }

</span>

2、通过注解方式

Java代码 复制代码  收藏代码
  1. <span>@Resource  
  2.   
  3. private ProjectStageHandler projectStageHandler;  
  4.   
  5. </span>  
<span>@Resource

private ProjectStageHandler projectStageHandler;

</span>

今天遇到 需要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean,在这里和大家分享一下,WEB 开发中,怎么获取 ApplicationContext ( 如有不正确之处还请指点)

查找工程有没有这种实现,功夫没有白费还真找到了大致实现方式如下:

3、首先web.xml配置

Xml代码 复制代码  收藏代码
  1. <span><listener>  
  2.         <listener-class>org.common.xxxServletContextListener</listener-class>  
  3.     </listener></span>  

接着xxxServletContextListener.java

Java代码 复制代码  收藏代码
  1. <span>public class xxxServletContextListener implements ServletContextListener {      
  2.     //ServletContext创建时调用该方法   
  3.     public void contextInitialized(ServletContextEvent arg0){  
  4.         ServletContext context= arg0.getServletContext();     
  5.         System.out.println("监听器启动。。。。。。。。。。。。。。。。。。。。。。。。。。。。");  
  6.         ApplicationCtx.init(context);  
  7.     }    
  8.     //ServletContext销毁时调用该方法      
  9.     public void contextDestroyed(ServletContextEvent sce){      
  10.          System.out.println("ServletContext销毁");      
  11.      }  
  12.         
  13. }   </span>  
<span>public class xxxServletContextListener implements ServletContextListener {    
    //ServletContext创建时调用该方法 
	public void contextInitialized(ServletContextEvent arg0){
		ServletContext context= arg0.getServletContext();   
		System.out.println("监听器启动。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
		ApplicationCtx.init(context);
	}  
    //ServletContext销毁时调用该方法    
    public void contextDestroyed(ServletContextEvent sce){    
         System.out.println("ServletContext销毁");    
     }
	  
}   </span>

ApplicationCtx .java

Java代码 复制代码  收藏代码
  1. <span>public class ApplicationCtx {  
  2.   
  3.     public static ApplicationContext ctx;  
  4.       
  5.     public static ApplicationContext getCtx(){  
  6.         return ctx;  
  7.     }  
  8.     public static void init(ServletContext sc){  
  9.         if(ctx==null){  
  10.             ctx =  WebApplicationContextUtils.getRequiredWebApplicationContext(sc);  
  11.         }  
  12.     }  
  13. }</span>  
<span>public class ApplicationCtx {

	public static ApplicationContext ctx;
	
	public static ApplicationContext getCtx(){
		return ctx;
	}
	public static void init(ServletContext sc){
		if(ctx==null){
			ctx =  WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		}
	}
}</span>

通过 ApplicationCtx.getCtx().getBean("xxx"); 就可以获到想要的bean啦

想想这种方式有点太罗嗦和麻烦,还有没有更简单的方式哪?有当然有。

4、首先要在spring.xml中加上

Xml代码 复制代码  收藏代码
  1. <span><bean id="applicationContext" class="org.wy.util.AppliactionContextHelper"/></span>  

当对AppliactionContextHelper实例时就自动设置applicationContext,以便后来可直接用applicationContext

Java代码 复制代码  收藏代码
  1. <span>   
  2. public class AppliactionContextHelper implements ApplicationContextAware {  
  3.   private static ApplicationContext applicationContext;     //Spring应用上下文环境  
  4.    
  5.   /** 
  6.   * 实现ApplicationContextAware接口的回调方法,设置上下文环境   
  7.   * @param applicationContext 
  8.   * @throws BeansException 
  9.   */  
  10.   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  11.     AppliactionContextHelper.applicationContext = applicationContext;  
  12.   }  
  13.    
  14.   /** 
  15.   * @return ApplicationContext 
  16.   */  
  17.   public static ApplicationContext getApplicationContext() {  
  18.     return applicationContext;  
  19.   }  
  20.    
  21.   /** 
  22.   * 获取对象   
  23.   * @param name 
  24.   * @return Object 一个以所给名字注册的bean的实例 
  25.   * @throws BeansException 
  26.   */  
  27.   public static Object getBean(String name) throws BeansException {  
  28.     return applicationContext.getBean(name);  
  29.   }  
  30.    
  31.   /** 
  32.   * 获取类型为requiredType的对象 
  33.   * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) 
  34.   * @param name       bean注册名 
  35.   * @param requiredType 返回对象类型 
  36.   * @return Object 返回requiredType类型对象 
  37.   * @throws BeansException 
  38.   */  
  39.   public static Object getBean(String name, Class requiredType) throws BeansException {  
  40.     return applicationContext.getBean(name, requiredType);  
  41.   }  
  42.    
  43.   /** 
  44.   * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true 
  45.   * @param name 
  46.   * @return boolean 
  47.   */  
  48.   public static boolean containsBean(String name) {  
  49.     return applicationContext.containsBean(name);  
  50.   }  
  51.    
  52.   /** 
  53.   * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 
  54.   * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)   
  55.   * @param name 
  56.   * @return boolean 
  57.   * @throws NoSuchBeanDefinitionException 
  58.   */  
  59.   public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {  
  60.     return applicationContext.isSingleton(name);  
  61.   }  
  62.    
  63.   /** 
  64.   * @param name 
  65.   * @return Class 注册对象的类型 
  66.   * @throws NoSuchBeanDefinitionException 
  67.   */  
  68.   public static Class getType(String name) throws NoSuchBeanDefinitionException {  
  69.     return applicationContext.getType(name);  
  70.   }  
  71.    
  72.   /** 
  73.   * 如果给定的bean名字在bean定义中有别名,则返回这些别名   
  74.   * @param name 
  75.   * @return 
  76.   * @throws NoSuchBeanDefinitionException 
  77.   */  
  78.   public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {  
  79.     return applicationContext.getAliases(name);  
  80.   }  
  81. }  
  82. </span>  
<span> 
public class AppliactionContextHelper implements ApplicationContextAware {
  private static ApplicationContext applicationContext;     //Spring应用上下文环境
 
  /**
  * 实现ApplicationContextAware接口的回调方法,设置上下文环境  
  * @param applicationContext
  * @throws BeansException
  */
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    AppliactionContextHelper.applicationContext = applicationContext;
  }
 
  /**
  * @return ApplicationContext
  */
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }
 
  /**
  * 获取对象  
  * @param name
  * @return Object 一个以所给名字注册的bean的实例
  * @throws BeansException
  */
  public static Object getBean(String name) throws BeansException {
    return applicationContext.getBean(name);
  }
 
  /**
  * 获取类型为requiredType的对象
  * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
  * @param name       bean注册名
  * @param requiredType 返回对象类型
  * @return Object 返回requiredType类型对象
  * @throws BeansException
  */
  public static Object getBean(String name, Class requiredType) throws BeansException {
    return applicationContext.getBean(name, requiredType);
  }
 
  /**
  * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  * @param name
  * @return boolean
  */
  public static boolean containsBean(String name) {
    return applicationContext.containsBean(name);
  }
 
  /**
  * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
  * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)  
  * @param name
  * @return boolean
  * @throws NoSuchBeanDefinitionException
  */
  public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.isSingleton(name);
  }
 
  /**
  * @param name
  * @return Class 注册对象的类型
  * @throws NoSuchBeanDefinitionException
  */
  public static Class getType(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getType(name);
  }
 
  /**
  * 如果给定的bean名字在bean定义中有别名,则返回这些别名  
  * @param name
  * @return
  * @throws NoSuchBeanDefinitionException
  */
  public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getAliases(name);
  }
}
</span>

通过AppliactionContextHelper.getBean("xxx");就可以获到想要的bean啦

5、

利用ClassPathXmlApplicationContext

可以从classpath中读取XML文件

(1) ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao)context.getBean("userDao");

(2) ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(new String[]{"applicationContext-ibatis-oracle.xml","applicationContext.xml","applicationContext-data-oracle.xml"});

BeanFactory factory = resource;

factory.getBean("xxx");

这种方式只适合非web中的应用程序中。

--------------------------------------------------------------------------------------------------------------------------------

转载:

获得spring里注册Bean的四种方法,特别是第三种方法,简单:


一:方法一(多在struts框架中)继承BaseDispatchAction

Java代码 复制代码 收藏代码
Java代码 复制代码  收藏代码
  1. <span>   
  2. import com.mas.wawacommunity.wap.service.UserManager;  
  3.    
  4. public class BaseDispatchAction extends DispatchAction {  
  5.     /** 
  6.     * web应用上下文环境变量 
  7.     */  
  8.     protected WebApplicationContext ctx;  
  9.    
  10.     protected UserManager userMgr;  
  11.    
  12.     /** 
  13.     * 获得注册Bean     
  14.     * @param beanName String 注册Bean的名称 
  15.     * @return 
  16.     */  
  17.     protected final Object getBean(String beanName) {  
  18.         return ctx.getBean(beanName);  
  19.     }  
  20.    
  21.     protected ActionForward unspecified(ActionMapping mapping, ActionForm form,  
  22.               javax.servlet.http.HttpServletRequest request,  
  23.               javax.servlet.http.HttpServletResponse response) {      
  24.         return mapping.findForward("index");  
  25.     }  
  26.    
  27.     public void setServlet(ActionServlet servlet) {  
  28.         this.servlet = servlet;  
  29.         this.ctx = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());  
  30.         this.userMgr = (UserManager) getBean("userManager");  
  31.     }          
  32. }  
  33. </span>  
<span> 
import com.mas.wawacommunity.wap.service.UserManager;
 
public class BaseDispatchAction extends DispatchAction {
    /**
    * web应用上下文环境变量
    */
    protected WebApplicationContext ctx;
 
    protected UserManager userMgr;
 
    /**
    * 获得注册Bean    
    * @param beanName String 注册Bean的名称
    * @return
    */
    protected final Object getBean(String beanName) {
        return ctx.getBean(beanName);
    }
 
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
              javax.servlet.http.HttpServletRequest request,
              javax.servlet.http.HttpServletResponse response) {    
        return mapping.findForward("index");
    }
 
    public void setServlet(ActionServlet servlet) {
        this.servlet = servlet;
        this.ctx = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
        this.userMgr = (UserManager) getBean("userManager");
    }        
}
</span>


二:方法二实现BeanFactoryAware
一定要在spring.xml中加上:
Xml代码 复制代码 收藏代码

Xml代码 复制代码  收藏代码
  1. <span><bean id="serviceLocator" class="com.am.oa.commons.service.ServiceLocator" singleton="true" />  
  2. </span>  

当对serviceLocator实例时就自动设置BeanFactory,以便后来可直接用 beanFactory
Java代码 复制代码 收藏代码
Java代码 复制代码  收藏代码
  1. <span>public class UserAction extends BaseAction implements Action,ModelDriven{  
  2.      
  3.     private Users user = new Users();  
  4.     protected ServiceLocator service = ServiceLocator.getInstance();  
  5.     UserService userService = (UserService)service.getService("userService");  
  6.    
  7.     public String execute() throws Exception {          
  8.         return SUCCESS;  
  9.     }  
  10.    
  11.   public Object getModel() {  
  12.         return user;  
  13.     }      
  14.      
  15.     public String getAllUser(){  
  16.         HttpServletRequest request = ServletActionContext.getRequest();          
  17.         List ls=userService.LoadAllObject( Users.class );  
  18.         request.setAttribute("user",ls);      
  19.         this.setUrl("/yonghu.jsp");  
  20.         return SUCCESS;  
  21.     }  
  22. }  
  23. </span>  
<span>public class UserAction extends BaseAction implements Action,ModelDriven{
   
    private Users user = new Users();
    protected ServiceLocator service = ServiceLocator.getInstance();
    UserService userService = (UserService)service.getService("userService");
 
    public String execute() throws Exception {        
        return SUCCESS;
    }
 
  public Object getModel() {
        return user;
    }    
   
    public String getAllUser(){
        HttpServletRequest request = ServletActionContext.getRequest();        
        List ls=userService.LoadAllObject( Users.class );
        request.setAttribute("user",ls);    
        this.setUrl("/yonghu.jsp");
        return SUCCESS;
    }
}
</span>



三:方法三实现ApplicationContextAware,一定要在spring.xml中加上:
Xml代码 复制代码 收藏代码
Xml代码 复制代码  收藏代码
  1. <span><bean id="SpringContextUtil " class="com.am.oa.commons.service.SpringContextUtil " singleton="true" />  
  2. </span>  

当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
Java代码 复制代码 收藏代码
Java代码 复制代码  收藏代码
  1. <span>   
  2. public class SpringContextUtil implements ApplicationContextAware {  
  3.   private static ApplicationContext applicationContext;     //Spring应用上下文环境  
  4.    
  5.   /** 
  6.   * 实现ApplicationContextAware接口的回调方法,设置上下文环境   
  7.   * @param applicationContext 
  8.   * @throws BeansException 
  9.   */  
  10.   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  11.     SpringContextUtil.applicationContext = applicationContext;  
  12.   }  
  13.    
  14.   /** 
  15.   * @return ApplicationContext 
  16.   */  
  17.   public static ApplicationContext getApplicationContext() {  
  18.     return applicationContext;  
  19.   }  
  20.    
  21.   /** 
  22.   * 获取对象   
  23.   * @param name 
  24.   * @return Object 一个以所给名字注册的bean的实例 
  25.   * @throws BeansException 
  26.   */  
  27.   public static Object getBean(String name) throws BeansException {  
  28.     return applicationContext.getBean(name);  
  29.   }  
  30.    
  31.   /** 
  32.   * 获取类型为requiredType的对象 
  33.   * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) 
  34.   * @param name       bean注册名 
  35.   * @param requiredType 返回对象类型 
  36.   * @return Object 返回requiredType类型对象 
  37.   * @throws BeansException 
  38.   */  
  39.   public static Object getBean(String name, Class requiredType) throws BeansException {  
  40.     return applicationContext.getBean(name, requiredType);  
  41.   }  
  42.    
  43.   /** 
  44.   * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true 
  45.   * @param name 
  46.   * @return boolean 
  47.   */  
  48.   public static boolean containsBean(String name) {  
  49.     return applicationContext.containsBean(name);  
  50.   }  
  51.    
  52.   /** 
  53.   * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 
  54.   * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)   
  55.   * @param name 
  56.   * @return boolean 
  57.   * @throws NoSuchBeanDefinitionException 
  58.   */  
  59.   public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {  
  60.     return applicationContext.isSingleton(name);  
  61.   }  
  62.    
  63.   /** 
  64.   * @param name 
  65.   * @return Class 注册对象的类型 
  66.   * @throws NoSuchBeanDefinitionException 
  67.   */  
  68.   public static Class getType(String name) throws NoSuchBeanDefinitionException {  
  69.     return applicationContext.getType(name);  
  70.   }  
  71.    
  72.   /** 
  73.   * 如果给定的bean名字在bean定义中有别名,则返回这些别名   
  74.   * @param name 
  75.   * @return 
  76.   * @throws NoSuchBeanDefinitionException 
  77.   */  
  78.   public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {  
  79.     return applicationContext.getAliases(name);  
  80.   }  
  81. }  
  82. </span>  
<span> 
public class SpringContextUtil implements ApplicationContextAware {
  private static ApplicationContext applicationContext;     //Spring应用上下文环境
 
  /**
  * 实现ApplicationContextAware接口的回调方法,设置上下文环境  
  * @param applicationContext
  * @throws BeansException
  */
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContextUtil.applicationContext = applicationContext;
  }
 
  /**
  * @return ApplicationContext
  */
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }
 
  /**
  * 获取对象  
  * @param name
  * @return Object 一个以所给名字注册的bean的实例
  * @throws BeansException
  */
  public static Object getBean(String name) throws BeansException {
    return applicationContext.getBean(name);
  }
 
  /**
  * 获取类型为requiredType的对象
  * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
  * @param name       bean注册名
  * @param requiredType 返回对象类型
  * @return Object 返回requiredType类型对象
  * @throws BeansException
  */
  public static Object getBean(String name, Class requiredType) throws BeansException {
    return applicationContext.getBean(name, requiredType);
  }
 
  /**
  * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  * @param name
  * @return boolean
  */
  public static boolean containsBean(String name) {
    return applicationContext.containsBean(name);
  }
 
  /**
  * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
  * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)  
  * @param name
  * @return boolean
  * @throws NoSuchBeanDefinitionException
  */
  public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.isSingleton(name);
  }
 
  /**
  * @param name
  * @return Class 注册对象的类型
  * @throws NoSuchBeanDefinitionException
  */
  public static Class getType(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getType(name);
  }
 
  /**
  * 如果给定的bean名字在bean定义中有别名,则返回这些别名  
  * @param name
  * @return
  * @throws NoSuchBeanDefinitionException
  */
  public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getAliases(name);
  }
}
</span>


action调用:
Java代码 复制代码 收藏代码
Java代码 复制代码  收藏代码
  1. <span>package com.anymusic.oa.webwork;  
  2.    
  3. import java.util.List;  
  4. import java.util.Map;  
  5.    
  6. import javax.servlet.http.HttpServletRequest;  
  7.    
  8. import com.anymusic.oa.commons.service.ServiceLocator;  
  9. import com.anymusic.oa.hibernate.pojo.Role;  
  10. import com.anymusic.oa.hibernate.pojo.Users;  
  11. import com.anymusic.oa.spring.IUserService;  
  12. import com.opensymphony.webwork.ServletActionContext;  
  13. import com.opensymphony.xwork.Action;  
  14. import com.opensymphony.xwork.ActionContext;  
  15. import com.opensymphony.xwork.ModelDriven;  
  16.    
  17. public class UserAction extends BaseAction implements Action,ModelDriven{  
  18.      
  19.     private Users user = new Users();  
  20.  //不用再加载springContext.xml 文件,因为在web.xml中配置了,在程序中启动是就有了.     
  21.     UserService userService = (UserService) SpringContextUtil.getBean("userService");  
  22.      
  23.     public String execute() throws Exception {          
  24.         return SUCCESS;  
  25.     }  
  26.    
  27.   public Object getModel() {  
  28.         return user;  
  29.     }      
  30.      
  31.     public String getAllUser(){  
  32.         HttpServletRequest request = ServletActionContext.getRequest();          
  33.         List ls=userService.LoadAllObject( Users.class );  
  34.         request.setAttribute("user",ls);      
  35.         this.setUrl("/yonghu.jsp");  
  36.         return SUCCESS;  
  37.     }  
  38. }  
  39. </span>  
<span>package com.anymusic.oa.webwork;
 
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import com.anymusic.oa.commons.service.ServiceLocator;
import com.anymusic.oa.hibernate.pojo.Role;
import com.anymusic.oa.hibernate.pojo.Users;
import com.anymusic.oa.spring.IUserService;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ModelDriven;
 
public class UserAction extends BaseAction implements Action,ModelDriven{
   
    private Users user = new Users();
 //不用再加载springContext.xml 文件,因为在web.xml中配置了,在程序中启动是就有了.   
    UserService userService = (UserService) SpringContextUtil.getBean("userService");
   
    public String execute() throws Exception {        
        return SUCCESS;
    }
 
  public Object getModel() {
        return user;
    }    
   
    public String getAllUser(){
        HttpServletRequest request = ServletActionContext.getRequest();        
        List ls=userService.LoadAllObject( Users.class );
        request.setAttribute("user",ls);    
        this.setUrl("/yonghu.jsp");
        return SUCCESS;
    }
}
</span>

四.通过servlet 或listener设置spring的 ApplicationContext,以便后来引用.
注意分别extends ContextLoaderListener和ContextLoaderServlet .然后就可用SpringContext 来getBean.
覆盖原来在web.xml中配置的listener或servlet.
Java代码 复制代码  收藏代码
  1. <span>public class SpringContext  {     
  2.   private static ApplicationContext applicationContext;     //Spring应用上下文环境     
  3.       
  4.     
  5.   */     
  6.   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {     
  7.     SpringContextUtil.applicationContext = applicationContext;     
  8.   }     
  9.       
  10.   /**   
  11.   * @return ApplicationContext   
  12.   */    
  13.   public static ApplicationContext getApplicationContext() {     
  14.     return applicationContext;     
  15.   }     
  16.       
  17.     
  18.   */     
  19.   public static Object getBean(String name) throws BeansException {     
  20.     return applicationContext.getBean(name);     
  21.   }     
  22.     
  23. }     
  24.     
  25. public class SpringContextLoaderListener extends ContextLoaderListener{  //     
  26.  public void contextInitialized(ServletContextEvent event) {       
  27.   super.contextInitialized(event);     
  28.   SpringContext.setApplicationContext(     
  29.     WebApplicationContextUtils.getWebApplicationContext(event.getServletContext())     
  30.     );     
  31.  }     
  32. }     
  33.     
  34. public class SpringContextLoaderServlet extends ContextLoaderServlet {     
  35.  private ContextLoader contextLoader;     
  36.     public void init() throws ServletException {     
  37.         this.contextLoader = createContextLoader();     
  38.         SpringContext.setApplicationContext(this.contextLoader.initWebApplicationContext(getServletContext()));     
  39.     }     
  40. }    
  41. </span> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值