获取applicationcontext的方式

方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

方法二:通过Spring提供的工具类获取ApplicationContext对象
代码:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

 

 

在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中,
类似于:

public   class  AppContext {

  
private   static  AppContext instance;

  
private  AbstractApplicationContext appContext;

  
public   synchronized   static  AppContext getInstance() {
    
if  (instance  ==   null {
      instance 
=   new  AppContext();
    }
    
return  instance;
  }

  
private  AppContext() {
    
this .appContext  =   new  ClassPathXmlApplicationContext(
        
" /applicationContext.xml " );
  }

  
public  AbstractApplicationContext getAppContext() {
    
return  appContext;
  }
}

 

  1. public abstract class BaseAction extends Action {   
  2.   protected static WebApplicationContext ctx;   
  3.   
  4.   /**  
  5.    * setServlet方法,植入Spring's ApplicationContext  
  6.    * @param actionServlet  
  7.    * @return  
  8.    */  
  9.   public void setServlet(ActionServlet actionServlet); {   
  10.     super.setServlet(actionServlet);;   
  11.     ServletContext servletContext = actionServlet.getServletContext();;   
  12.     ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);;   
  13.   }   
  14.   
  15.   /**  
  16.    * getCtx方法,取得Spring's ApplicationContext  
  17.    * @return WebApplicationContext  
  18.    */  
  19.   public static WebApplicationContext getCtx();{   
  20.     return ctx;   
  21.   }  


  首先要看你的class是不是被spring管理。如果是的话一般没多大的必要去拿ApplicationContext。如果非要拿的话继承ApplicationContextAware就可了

如果不是被spring管理的话,就需要借助一下其他代码。总体思路是设计一个Class 让这个class拿一个ctx。保存在它的static 变量中

比如

public class GlobeContext implements ApplicationContextAware {
  private static ApplicationContext ac;

  
 public static void setSpringContext(ApplicationContext ac); {
  GlobeContext.ac = ac;  
 }

    /* (non-Javadoc);
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext);
     */
    public void setApplicationContext(ApplicationContext ac); throws BeansException {
        GlobeContext.ac=ac;
       
    }
static public ApplicationContext getApplicationContext();{

   return ac;
}
}

使用FileSystemXmlApplicationContext创建时,文件的路径应该是以project的根路径作为起始路径的,所以不能用"/WEB-INF/HibernateContext.xml"

使用ClassPathXmlApplicationContext创建时,要把配置文件放到你的classpath下面,对于web应用,classpath是WEB-INF/classes/

 

 

=============================================================================================

public class AppContext {

  private static AppContext instance;

  private AbstractApplicationContext appContext;

  public synchronized static AppContext getInstance() {
    if (instance == null) {
      instance = new AppContext();
    }
    return instance;
  }

  private AppContext() {
    this.appContext = new ClassPathXmlApplicationContext(
        "/applicationContext.xml");
  }

  public AbstractApplicationContext getAppContext() {
    return appContext;
  }
}

 

再servlet使用
package cn.wapall.yahoo.util;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.wapall.yahoo.business.IFeeCallbackService;

public class WebAppContext {
  private ApplicationContext appContext;
  /**
   * 初始化spring配置信息
   * @param context
   */
  public void init(ServletContext context) {
this.appContext = WebApplicationContextUtils.getWebApplicationContext(context);
  }
  /**
   * 获取返回信息服务
   * @return
   */
  public IFeeCallbackService getFeeCallbackService() {  
  return (IFeeCallbackService)this.appContext.getBean("feeCallbackService");
  }
 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值