001package run.iget.framework.common.context;
002
003import org.springframework.beans.BeansException;
004import org.springframework.context.ApplicationContext;
005import org.springframework.context.ApplicationContextAware;
006import org.springframework.stereotype.Component;
007
008/**
009 * 代码千万行,注释第一行,注释不规范,迭代两行泪
010 * ---------------类描述-----------------
011 * Spring上下文对象获取工具类
012 * ---------------类描述-----------------
013 * @author 大周
014 * @since 2021/7/15 23:12:07
015 */
016@Component
017public final class SpringContextHolder implements ApplicationContextAware {
018
019    /**
020     * spring的上下文对象
021     */
022    private static ApplicationContext APPLICATION_CONTEXT;
023
024    /**
025     * 功能描述: 通过类类型获取bean
026     * @param tClass -- <描述>
027     * @return
028     */
029    public static <T> T getBean(Class<T> tClass) {
030        return APPLICATION_CONTEXT.getBean(tClass);
031    }
032
033    /**
034     * 功能描述: 通过类类型获取bean
035     * @param tClass -- <描述>
036     * @param params -- 构造参数
037     * @return
038     */
039    public static <T> T getBean(Class<T> tClass, Object... params) {
040        return APPLICATION_CONTEXT.getBean(tClass, params);
041    }
042
043    /**
044     * 功能描述: 通过类名称
045     * @param beanName
046     * @return
047     */
048    public static Object getBean(String beanName) {
049        return APPLICATION_CONTEXT.getBean(beanName);
050    }
051
052    /**
053     * 功能描述: 通过类名称
054     * @param beanName -- 类名称
055     * @param params -- 构造参数
056     * @return
057     */
058    public static Object getBean(String beanName, Object... params) {
059        return APPLICATION_CONTEXT.getBean(beanName, params);
060    }
061
062    @Override
063    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
064        APPLICATION_CONTEXT = applicationContext;
065    }
066}