!384 【轻量级PR】新增SpringUtils.getRequiredProperty,以静态方式获取配置文件中的值

Merge pull request !384 from 阿超/master
master
若依 2022-05-20 02:48:48 +00:00 committed by Gitee
commit 7506ac77cb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 159 additions and 146 deletions

View File

@ -1,146 +1,159 @@
package com.ruoyi.common.utils.spring; package com.ruoyi.common.utils.spring;
import org.springframework.aop.framework.AopContext; import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException; 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.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
/** /**
* spring 便springbean * spring 便springbean
* *
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{ {
/** Spring应用上下文环境 */ /** Spring应用上下文环境 */
private static ConfigurableListableBeanFactory beanFactory; private static ConfigurableListableBeanFactory beanFactory;
private static ApplicationContext applicationContext; 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 @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{ {
SpringUtils.applicationContext = applicationContext; SpringUtils.applicationContext = applicationContext;
} }
/** /**
* *
* *
* @param name * @param name
* @return Object bean * @return Object bean
* @throws org.springframework.beans.BeansException * @throws org.springframework.beans.BeansException
* *
*/ */
@SuppressWarnings("unchecked") @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); return (T) beanFactory.getBean(name);
} }
/** /**
* requiredType * requiredType
* *
* @param clz * @param clz
* @return * @return
* @throws org.springframework.beans.BeansException * @throws org.springframework.beans.BeansException
* *
*/ */
public static <T> T getBean(Class<T> clz) throws BeansException public static <T> T getBean(Class<T> clz) throws BeansException
{ {
T result = (T) beanFactory.getBean(clz); T result = (T) beanFactory.getBean(clz);
return result; return result;
} }
/** /**
* BeanFactorybeantrue * BeanFactorybeantrue
* *
* @param name * @param name
* @return boolean * @return boolean
*/ */
public static boolean containsBean(String name) public static boolean containsBean(String name)
{ {
return beanFactory.containsBean(name); return beanFactory.containsBean(name);
} }
/** /**
* beansingletonprototype beanNoSuchBeanDefinitionException * beansingletonprototype beanNoSuchBeanDefinitionException
* *
* @param name * @param name
* @return boolean * @return boolean
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* *
*/ */
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
{ {
return beanFactory.isSingleton(name); return beanFactory.isSingleton(name);
} }
/** /**
* @param name * @param name
* @return Class * @return Class
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* *
*/ */
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
{ {
return beanFactory.getType(name); return beanFactory.getType(name);
} }
/** /**
* beanbean * beanbean
* *
* @param name * @param name
* @return * @return
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* *
*/ */
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
{ {
return beanFactory.getAliases(name); return beanFactory.getAliases(name);
} }
/** /**
* aop * aop
* *
* @param invoker * @param invoker
* @return * @return
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker) public static <T> T getAopProxy(T invoker)
{ {
return (T) AopContext.currentProxy(); return (T) AopContext.currentProxy();
} }
/** /**
* null * null
* *
* @return * @return
*/ */
public static String[] getActiveProfiles() public static String[] getActiveProfiles()
{ {
return applicationContext.getEnvironment().getActiveProfiles(); return applicationContext.getEnvironment().getActiveProfiles();
} }
/** /**
* *
* *
* @return * @return
*/ */
public static String getActiveProfile() public static String getActiveProfile()
{ {
final String[] activeProfiles = getActiveProfiles(); final String[] activeProfiles = getActiveProfiles();
return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
} }
}
/**
*
*
* @param key key
* @return
*
*/
public static String getRequiredProperty(String key)
{
return applicationContext.getEnvironment().getRequiredProperty(key);
}
}