首页>代码>大嘴首发J2EE框架,Bigmouth-framework你值得拥有!>/bigmouth/bigmouth-framework/trunk/src/main/java/org/bigmouth/framework/core/SpringContextHolder.java
package org.bigmouth.framework.core; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.log4j.Logger; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. * */ public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = org.apache.log4j.Logger.getLogger(SpringContextHolder.class); /** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ public void setApplicationContext(ApplicationContext applicationContext) { logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext); if (SpringContextHolder.applicationContext != null) { logger.warn("SpringContextHolder中的ApplicationContext被覆盖,原有Context为:" + SpringContextHolder.applicationContext); } SpringContextHolder.applicationContext = applicationContext; //NOSONAR } /** * 实现DisposableBean接口,在Context关闭时清理静态变量. * @Override */ public void destroy() throws Exception { SpringContextHolder.cleanApplicationContext(); } /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } /** * 清除applicationContext静态变量. */ public static void cleanApplicationContext() { logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); applicationContext = null; } /** * 检查ApplicationContext不为空. */ private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } } /** * 从 Spring 上下文获取Bean实例, BeanID 默认为类简单名称或首字母小写的类简单名称。 * * @param <T> 泛型类型 * @param beanClass * @return Spring Bean 实例 * @author Allen.Hu / 2012-6-29 * @since SkyMarket 1.0 */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> beanClass) { Object obj = null; String simpleName = beanClass.getSimpleName(); obj = getBean(simpleName); if (null == obj) { String beanId = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1); obj = getBean(beanId); } return (T) obj; } /** * 获得当前配置的数据源对象。 * * @return * @author Allen.Hu / 2012-6-29 * @since SkyMarket 1.0 */ public static DataSource getCurrentDataSource() { return ((BasicDataSource)getBean("dataSource")); } }
最近下载更多
ssmss951 LV3
2023年2月27日
微信网友_5970838873509888 LV2
2022年5月24日
hwshws LV9
2021年5月24日
gyd二蛋不是扁的 LV2
2019年12月11日
huangjiarui LV11
2019年11月28日
luohaipeng LV23
2019年11月19日
w2lijing2 LV10
2019年9月16日
等待or进取 LV1
2019年8月24日
低调人 LV38
2019年8月4日
1763157114 LV15
2019年7月8日
最近浏览更多
KWAN112234 LV1
2023年8月18日
ssmss951 LV3
2023年2月27日
微信网友_6248713511227392 LV11
2022年12月5日
微信网友_5970838873509888 LV2
2022年5月24日
你曾是少年啊
2022年2月16日
暂无贡献等级
刘亦菲9527 LV15
2021年12月1日
543666826 LV33
2021年11月18日
好的好的 LV8
2021年6月21日
zxfzxf LV5
2021年6月21日
jackychen1012 LV2
2021年6月18日