SpringBeanUtil

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 
     * @return
     */
    public static  T getBean(String name,Class clazz){
        return getApplicationContext().getBean(name, clazz);
    }

    /**
     * 通过class获取Bean.
     * @param 
     * @param tClass
     * @return
     */
    public static  T getBean(Class tClass){
        return getApplicationContext().getBean(tClass);
    }


}

你可能感兴趣的:(SpringBeanUtil)