所谓spring上相文辅助类,就这这个类可以有封装的getBean()方法,根据spring的BEAN ID找到指定的BEAN实例
package com.jugiven.core.utils;
import org.springframework.context.ApplicationContext;
/**
* spring上下文工具
* <br>创建日期:2012-2-15
* <br><b>Copyright 2012 银联商务有限公司 All Rights Reserved</b>
* @since 1.0
* @version 1.0
*/
public class ContextUtil {
/**
* spring上下文句柄
*/
private static ApplicationContext applicationContext = null;
/**
* 设置spring上下文句柄
* <br>创建时间:2012-2-15 下午6:14:59
* @since 1.0
* @param cxt spring上下文句柄
*/
public static void setContext(ApplicationContext cxt) {
applicationContext = cxt;
}
/**
* 根据spring的BEAN ID找到指定的BEAN实例
* <br>创建时间:2012-2-15 下午6:16:57
* @since 1.0
* @param beanId spring配置的bean编号
* @return spring管理的BEAN实例
* @throws Exception 如果根据指定的BEAN编号没有找到BEAN信息则抛出该异常
*/
public static Object getBean(String beanId) throws Exception {
Object object = applicationContext.getBean(beanId);
if(object == null) {
throw new Exception("没有找到指定的BEAN ID [ " + beanId + " ]");
}
return object;
}
}