Spring 容器工具类

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Spring 工具类
 * @ClassName SpringUtil
 * @Author YH
 * @Date 2021/12/3
 * @Version 1.0
 */
@Component
public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextAware {
    
    

    /**
     * "@PostConstruct" 注解标记的类中,由于 ApplicationContext 还未加载,导致空指针<br>
     * 因此实现 BeanFactoryPostProcessor 注入 ConfigurableListableBeanFactory 实现 Bean 的操作
     */
    private static ConfigurableListableBeanFactory beanFactory;

    /**
     * Spring 容器
     */
    private static ApplicationContext applicationContext;

    /**
     * 私有化构造方法
     */
    private SpringUtil() {
    
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    
    
        SpringUtil.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        SpringUtil.applicationContext = applicationContext;
    }

    /**
     * 获取 ConfigurableListableBeanFactory
     * @return
     * @throws RuntimeException
     */
    public static ConfigurableListableBeanFactory getConfigurableBeanFactory() throws RuntimeException {
    
    
        final ConfigurableListableBeanFactory factory;
        if (null != beanFactory) {
    
    
            factory = beanFactory;
        } else if (applicationContext instanceof ConfigurableApplicationContext) {
    
    
            factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
        } else {
    
    
            throw new RuntimeException("No ConfigurableListableBeanFactory from context!");
        }
        return factory;
    }

    /**
     * 获取 Spring 容器
     * @return
     */
    public static ApplicationContext getApplicationContext() {
    
    
        return applicationContext;
    }

    public static ListableBeanFactory getBeanFactory() {
    
    
        return null == beanFactory ? applicationContext : beanFactory;
    }

    /**
     * 根据名称获取 Bean
     * @param name 名称
     * @param <T> 返回类型
     * @return
     */
    public static <T> T getBean(String name) {
    
    
        return (T) applicationContext.getBean(name);
    }

    /**
     * 根据类型获取 Bean
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
    
    
        return getBeanFactory().getBean(clazz);
    }

    /**
     * 根据名称和类型获取 Bean
     * @param name 名称
     * @param clazz 类型
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
    
    
        return getBeanFactory().getBean(name, clazz);
    }

    /**
     * 获取指定类型对应的所有 Bean,包括子类
     * @param type
     * @param <T>
     * @return
     */
    public static <T> Map<String, T> getBeansOfType(Class<T> type) {
    
    
        return getBeansOfType(type);
    }

    /**
     * 获取指定类型对应的 Bean 名称,包括子类
     * @param type
     * @return
     */
    public static String[] getBeanNamesForType(Class<?> type) {
    
    
        return getBeanFactory().getBeanNamesForType(type);
    }

    /**
     * 获取配置文件配置项的值
     * @param key 配置项 key
     * @return
     */
    public static String getProperty(String key) {
    
    
        return applicationContext.getEnvironment().getProperty(key);
    }

    /**
     * 获取应用程序名称
     * @return
     */
    public static String getApplicationName() {
    
    
        return getProperty("spring.application.name");
    }

    /**
     * 获取当前的环境配置,无配置返回 null
     * @return
     */
    public static String[] getActiveProfiles() {
    
    
        if (null == applicationContext) {
    
    
            return null;
        }

        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取当前的环境配置,当有多个环境配置时,只获取第一个
     * @return
     */
    public static String getActiveProfile() {
    
    
        final String[] activeProfiles = getActiveProfiles();

        if (activeProfiles == null || activeProfiles.length == 0) {
    
    
            return null;
        }

        return activeProfiles[0];
    }

    /**
     * 动态向 Spring 容器中注册 Bean
     * @param beanName 名称
     * @param bean 类型
     * @param <T>
     */
    public static <T> void registerBean(String beanName, T bean) {
    
    
        final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
        factory.autowireBean(bean);
        factory.registerSingleton(beanName, bean);
    }

    /**
     * 向容器中注销 Bean
     * @param beanName 名称
     * @param <T>
     */
    public static <T> void logoutBean(String beanName) {
    
    
        final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();

        if (factory instanceof DefaultSingletonBeanRegistry) {
    
    
            DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) factory;
            registry.destroySingleton(beanName);
        } else {
    
    
            throw new RuntimeException("Can not unregister bean, the factory is not a DefaultSingletonBeanRegistry!");
        }
    }

    /**
     * 发布事件
     * @param event
     */
    public static void publishEvent(ApplicationContext event) {
    
    
        if (null != applicationContext) {
    
    
            applicationContext.publishEvent(event);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_52462015/article/details/121704226
今日推荐