----------------------spring----------------------------
- public class SpringUtil {
- private static Log logger = LogFactory.getLog(SpringUtil.class);
- /** Spring框架应用上下文对象 */
- private static ApplicationContext factory = getApplicationContext();
- static{
- getApplicationContext();
- }
- public static void setFactoryBean(ApplicationContext factory){
- SpringUtil.factory = factory;
- }
- /**
- * 获得Spring框架应用上下文对象
- * @return ApplicationContext
- */
- public static ApplicationContext getApplicationContext()
- {
- //判断如果 ApplicationContext 的对象 == NULL
- if ( factory == null )
- {
- if(logger.isDebugEnabled()) logger.debug("Init Spring's ApplicationContext========");
- try
- {
- ConsolePrinter.println("******init spring application context");
- factory = new ClassPathXmlApplicationContext(new String[]{"applicationContext-faengine.xml","applicationContext-common.xml"
- //,"applicationContext-daoSupport-test.xml"
- });
- }
- catch ( Exception e1 )
- {
- if(logger.isDebugEnabled()) logger.debug("err = " + e1.getMessage());
- e1.printStackTrace();
- }
- }
- //返回ApplicationContext
- return factory;
- }
-----------------------springboot-----------------------------------
public class SpringUtil implements ApplicationContextAware {/**
* 说明:定义后能再同一类中使用static常量log
*/
private static final Logger logger = LoggerFactory.getLogger(SpringUtil.class);
/**
* 说明:定义一个只能在该类使用的静态变量applicationContext
*/
private static ApplicationContext applicationContext;
/**
* @Title: setApplicationContext
* @See: 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
public final void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
/**
* 说明:通过Spring配置文件中定义的bean名称,从IOC容器中取得实例
* @Title: getBean
* @param beanName Bean名
* @param <T> 范型
* @return Bean名称对应实例Object,使用时需要强制类型转换
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String beanName) {
try {
T bean = (T) applicationContext.getBean(beanName);
return bean;
} catch (Exception e){
if (logger.isDebugEnabled()) {
logger.debug("bean's name of Spring config's file don't exist,error message :",e);
}
}
return null;
}
/**
* 说明:通过Spring配置文件中定义的bean名称,从IOC容器中取得bean类型
* @Title: getType
* @param beanName Bean名
* @return Bean名称对应bean类型
*/
public static Class<?> getType(String beanName) {
try {
Class<?> beanType = applicationContext.getType(beanName);
return beanType;
} catch (Exception e){
if (logger.isDebugEnabled()) {
logger.debug("bean's name of Spring config's file don't exist,error message :",e);
}
}
return null;
}
/**
* 说明:获取指定类型的Bean
* @Title: getBeansByType
* @param beanType Bean类名
* @return 指定类型的Bean
*/
@SuppressWarnings({"rawtypes" ,"unchecked"})
public static <T> Map<String, T> getBeansByType(Class beanType) {
try {
Map<String, T> beans = applicationContext.getBeansOfType(beanType);
return beans;
} catch (Exception e){
if (logger.isDebugEnabled()) {
logger.debug("bean of Obtained type don't exist, error message:",e);
}
}
return null;
}
/**
* 说明:获取指定类型的Bean映射到MAP
* @Title: getBeanByType
* @param beanType bean类
* @param <T> 范型
* @return 映射结果
*/
public static <T> T getBeanByType(Class<T> beanType) {
try {
Map<String, T> beans = SpringUtil.applicationContext.getBeansOfType(beanType);
if (beans != null && beans.size() > 0) {
return beans.values().iterator().next();
}
} catch (Exception e){
if (logger.isDebugEnabled()) {
logger.debug("bean of Obtained type don't exist in Map, error message:",e);
}
}
return null;
}
代码中引入
一,一般方式
/* 注入获取业务需求数据 */
MultiTaskService asynctaskservice = (MultiTaskService) SpringUtils.getBean("multitaskservice");
二,多线程
如果是多线程,无法直接获取web容器中的bean,需要定义静态变量
private static IsStaticService isstaticservice;
Method(){...
isstaticservice = (IsStaticService) SpringUtils.getBean("isStaticService");
if (null == isstaticservice) {
logger.info("MultiTaskService:isstaticservice bean is null");
} else {
... = isstaticservice.findCount().intValue();
}
}
参考 https://blog.csdn.net/jueshengtianya/article/details/47439983
1. 使用static声明变量
2. 把线程设置为主程序的内部类
3. 使用静态方法直接取的容器中的spring对象(但一定要记住,你定义这个工具类也要配置成spring中的bean!)