/******************************************************************************
|
* Copyright (C) 2015 Shenzhen Penguin Network Technology Co., Ltd
|
* All Rights Reserved.
|
* 本软件为深圳企鹅网络科技有限公司开发研制。未经本公司正式书面同意,其他任何个人、团体
|
* 不得使用、复制、修改或发布本软件.
|
*****************************************************************************/
|
|
package com.qxueyou.scc.base.util;
|
|
import org.springframework.beans.BeansException;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.stereotype.Component;
|
|
/**
|
*
|
* Spring 工具类,提供获取Sping上下文环境及获取Bean的接口
|
* @history 2010-09-12 夏德虎 新建
|
*
|
*/
|
@Component
|
public class SpringUtil implements ApplicationContextAware {
|
|
private static ApplicationContext applicationContext;
|
|
private SpringUtil(){}
|
|
/**
|
* 获取bean
|
* @param <T> 泛型类型
|
* @param name bean name
|
* @param clz bean 类型
|
* @param context servlet上下文
|
* @return bean实例
|
*/
|
public static <T> T getBean(String name,Class<T> clz){
|
return applicationContext.getBean(name,clz);
|
}
|
|
/**
|
* 获取bean
|
* @param <T> 泛型类型
|
* @param clz bean 类型
|
* @param context servlet上下文
|
* @return bean实例
|
*/
|
public static <T> T getBean(Class<T> clz){
|
return applicationContext.getBean(clz);
|
}
|
|
@SuppressWarnings("unchecked")
|
public static <T> T getBean(String name) throws BeansException {
|
return (T) applicationContext.getBean(name);
|
}
|
|
|
@Override
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
SpringUtil.applicationContext= applicationContext;
|
}
|
}
|