lidongdong
2022-09-23 afb088b35b01ceab763081810a6e964c23d0cef8
flower_city/src/main/java/com/dg/core/exception/file/SpringUtils.java
File was renamed from flower_city/src/main/java/com/dg/core/spring/SpringUtils.java
@@ -1,5 +1,6 @@
package com.dg.core.spring;
package com.dg.core.exception.file;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -8,31 +9,20 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
/**
 * spring工具类 方便在非spring管理环境中获取bean
 * 
 * @author ruoyi
 * @author huanghongfa
 */
@Slf4j
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;
    private static ApplicationContext applicationContext;
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringUtils.applicationContext = applicationContext;
    }
    /**
     * 获取对象
@@ -43,8 +33,7 @@
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
    public static <T> T getBean(String name) throws BeansException {
        return (T) beanFactory.getBean(name);
    }
@@ -56,9 +45,8 @@
     * @throws BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException
    {
        T result = (T) beanFactory.getBean(clz);
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = beanFactory.getBean(clz);
        return result;
    }
@@ -68,8 +56,7 @@
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name)
    {
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }
@@ -81,8 +68,7 @@
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
    {
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(name);
    }
@@ -92,8 +78,7 @@
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
    {
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }
@@ -105,8 +90,7 @@
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
    {
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }
@@ -117,8 +101,7 @@
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker)
    {
    public static <T> T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }
@@ -127,8 +110,7 @@
     *
     * @return 当前的环境配置
     */
    public static String[] getActiveProfiles()
    {
    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }
@@ -137,9 +119,18 @@
     *
     * @return 当前的环境配置
     */
    public static String getActiveProfile()
    {
    public static String getActiveProfile() {
        final String[] activeProfiles = getActiveProfiles();
        return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
        return !ObjectUtils.isEmpty(activeProfiles) ? activeProfiles[0] : null;
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }
}