美文网首页
SpringBeanUtil

SpringBeanUtil

作者: LcChina_9527 | 来源:发表于2020-08-19 09:31 被阅读0次
    package com.flowable.test.utils;
    
    import org.flowable.engine.ProcessEngine;
    import org.flowable.engine.TaskService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    import javax.lang.model.element.VariableElement;
    
    /**
     * @program: flowable
     * @author: LcChina
     * @create: 2020-08-18 13:35
     */
    @Component
    public class SpringBeanUtil implements ApplicationContextAware {
    
        private static Logger logger = LoggerFactory.getLogger(ApplicationContextAware.class);
    
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (SpringBeanUtil.applicationContext == null) {
                SpringBeanUtil.applicationContext = applicationContext;
            }
            logger.info("\r\n----------加载applicationContext成功-----------------");
        }
    
        /**
         * 获取applicationContext
         * @return
         */
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        /**
         * 通过name获取 Bean.
         * @param name
         * @return
         */
        public static Object getBean(String name){
            return getApplicationContext().getBean(name);
        }
    
        /**
         * 通过name,以及Clazz返回指定的Bean
         * @param name
         * @param clazz
         * @param <T>
         * @return
         */
        public static <T> T getBean(String name,Class<T> clazz){
            return getApplicationContext().getBean(name, clazz);
        }
    
        /**
         * 通过class获取Bean.
         * @param <T>
         * @param tClass
         * @return
         */
        public static <T> T getBean(Class<T> tClass){
            return getApplicationContext().getBean(tClass);
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:SpringBeanUtil

          本文链接:https://www.haomeiwen.com/subject/bytkjktx.html