SpringUtil

----------------------spring----------------------------

  1. public class SpringUtil {  
  2.   
  3.     private static Log logger = LogFactory.getLog(SpringUtil.class);  
  4.     /** Spring框架应用上下文对象 */  
  5.     private static ApplicationContext factory = getApplicationContext();  
  6.       
  7.     static{  
  8.         getApplicationContext();  
  9.     }  
  10.       
  11.     public static void setFactoryBean(ApplicationContext factory){  
  12.         SpringUtil.factory = factory;  
  13.     }  
  14.     /** 
  15.      * 获得Spring框架应用上下文对象  
  16.      * @return ApplicationContext 
  17.      */  
  18.     public static ApplicationContext getApplicationContext()  
  19.     {  
  20.         //判断如果 ApplicationContext 的对象 == NULL  
  21.         if ( factory == null )  
  22.         {  
  23.             if(logger.isDebugEnabled()) logger.debug("Init Spring's ApplicationContext========");  
  24.             try  
  25.             {  
  26.                 ConsolePrinter.println("******init spring application context");  
  27.                 factory = new ClassPathXmlApplicationContext(new String[]{"applicationContext-faengine.xml","applicationContext-common.xml"  
  28.                         //,"applicationContext-daoSupport-test.xml"  
  29.                         });  
  30.             }  
  31.             catch ( Exception e1 )  
  32.             {  
  33.                 if(logger.isDebugEnabled()) logger.debug("err = " + e1.getMessage());  
  34.                 e1.printStackTrace();  
  35.             }  
  36.         }  
  37.         //返回ApplicationContext  
  38.         return factory;  
  39.     }  

-----------------------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;

扫描二维码关注公众号,回复: 2348130 查看本文章


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!)



猜你喜欢

转载自blog.csdn.net/hhb910/article/details/80069157
今日推荐