添加获取当前的环境配置方法
This commit is contained in:
parent
1ac323d68c
commit
2d6bd72f14
@ -5,7 +5,10 @@ import org.springframework.beans.BeansException;
|
|||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* spring工具类 方便在非spring管理环境中获取bean
|
* spring工具类 方便在非spring管理环境中获取bean
|
||||||
@ -13,17 +16,25 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public final class SpringUtils implements BeanFactoryPostProcessor
|
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
|
||||||
{
|
{
|
||||||
/** Spring应用上下文环境 */
|
/** Spring应用上下文环境 */
|
||||||
private static ConfigurableListableBeanFactory beanFactory;
|
private static ConfigurableListableBeanFactory beanFactory;
|
||||||
|
|
||||||
|
private static ApplicationContext applicationContext;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
|
||||||
{
|
{
|
||||||
SpringUtils.beanFactory = beanFactory;
|
SpringUtils.beanFactory = beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
|
||||||
|
{
|
||||||
|
SpringUtils.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取对象
|
* 获取对象
|
||||||
*
|
*
|
||||||
@ -111,4 +122,25 @@ public final class SpringUtils implements BeanFactoryPostProcessor
|
|||||||
{
|
{
|
||||||
return (T) AopContext.currentProxy();
|
return (T) AopContext.currentProxy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前的环境配置,无配置返回null
|
||||||
|
*
|
||||||
|
* @return 当前的环境配置
|
||||||
|
*/
|
||||||
|
public static String[] getActiveProfiles()
|
||||||
|
{
|
||||||
|
return applicationContext.getEnvironment().getActiveProfiles();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前的环境配置,当有多个环境配置时,只获取第一个
|
||||||
|
*
|
||||||
|
* @return 当前的环境配置
|
||||||
|
*/
|
||||||
|
public static String getActiveProfile()
|
||||||
|
{
|
||||||
|
final String[] activeProfiles = getActiveProfiles();
|
||||||
|
return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user