Package cn.sinozg.applet.common.utils
Class SpringUtil
java.lang.Object
cn.sinozg.applet.common.utils.SpringUtil
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.beans.factory.config.BeanFactoryPostProcessor,org.springframework.context.ApplicationContextAware
@Component
public final class SpringUtil
extends Object
implements org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.context.ApplicationContextAware
spring工具类 方便在非spring管理环境中获取bean
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static org.springframework.beans.factory.config.ConfigurableListableBeanFactorySpring应用上下文环境private static org.springframework.context.ApplicationContext -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TbeanOfNmTp(String name, Class<T> clazz) 获取beanstatic <T,R> R beanOfType(T type, Class<?> clazz, Map<T, R> map, Function<R, T> fun) 从多个实现类获取到beanstatic <T,R> R beanOfType(T type, Class<?> clazz, Map<T, R> map, Function<R, T> fun, Predicate<T> predicate) 从多个实现类获取到beanbeansOfType(Class<T> clazz) 获取beanstatic booleancontainsBean(String name) 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回truestatic String获取当前的环境配置,当有多个环境配置时,只获取第一个static String[]获取当前的环境配置,无配置返回nullstatic String[]getAliases(String name) 如果给定的bean名字在bean定义中有别名,则返回这些别名static <T> TgetAopProxy(T invoker) 获取aop代理对象static org.springframework.context.ApplicationContext获取上下文static <T> T获取类型为requiredType的对象static <T> T获取对象static Class<?>static booleanisSingleton(String name) 判断以给定名字注册的bean定义是一个singleton还是一个prototype。voidpostProcessBeanFactory(@NotNull org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) static <T> TregisterBean(String beanName, Class<T> clazz, Object... args) 动态注册beanvoidsetApplicationContext(@NotNull org.springframework.context.ApplicationContext applicationContext)
-
Field Details
-
beanFactory
private static org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactorySpring应用上下文环境 -
context
private static org.springframework.context.ApplicationContext context
-
-
Constructor Details
-
SpringUtil
public SpringUtil()
-
-
Method Details
-
postProcessBeanFactory
public void postProcessBeanFactory(@NotNull @NotNull org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) throws org.springframework.beans.BeansException - Specified by:
postProcessBeanFactoryin interfaceorg.springframework.beans.factory.config.BeanFactoryPostProcessor- Throws:
org.springframework.beans.BeansException
-
setApplicationContext
public void setApplicationContext(@NotNull @NotNull org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException - Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Throws:
org.springframework.beans.BeansException
-
getApplicationContext
public static org.springframework.context.ApplicationContext getApplicationContext()获取上下文- Returns:
- 上下文
-
getBean
获取对象- Parameters:
name- bean名称- Returns:
- Object 一个以所给名字注册的bean的实例
- Throws:
org.springframework.beans.BeansException- Exception
-
getBean
获取类型为requiredType的对象- Parameters:
clazz- bean类型- Returns:
- 对象
- Throws:
org.springframework.beans.BeansException- Exception
-
containsBean
如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true- Parameters:
name- bean名称- Returns:
- boolean
-
isSingleton
public static boolean isSingleton(String name) throws org.springframework.beans.factory.NoSuchBeanDefinitionException 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)- Parameters:
name- bean名称- Returns:
- boolean
- Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException- Exception
-
getType
public static Class<?> getType(String name) throws org.springframework.beans.factory.NoSuchBeanDefinitionException - Parameters:
name- bean名称- Returns:
- Class 注册对象的类型
- Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException- Exception
-
getAliases
public static String[] getAliases(String name) throws org.springframework.beans.factory.NoSuchBeanDefinitionException 如果给定的bean名字在bean定义中有别名,则返回这些别名- Parameters:
name- bean名称- Returns:
- 结果
- Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException- Exception
-
getAopProxy
public static <T> T getAopProxy(T invoker) 获取aop代理对象- Parameters:
invoker- invoker- Returns:
- 对象
-
getActiveProfiles
获取当前的环境配置,无配置返回null- Returns:
- 当前的环境配置
-
getActiveProfile
获取当前的环境配置,当有多个环境配置时,只获取第一个- Returns:
- 当前的环境配置
-
beansOfType
获取bean- Type Parameters:
T- 类型- Parameters:
clazz- 类型- Returns:
- 实例
-
beanOfType
从多个实现类获取到bean- Type Parameters:
T- 类型R- 实例- Parameters:
type- 类型clazz- 类map- 缓存mapfun- 获取类型的函数- Returns:
- 实例
-
beanOfType
public static <T,R> R beanOfType(T type, Class<?> clazz, Map<T, R> map, Function<R, T> fun, Predicate<T> predicate) 从多个实现类获取到bean- Type Parameters:
T- 类型R- 实例- Parameters:
type- 类型clazz- 类map- 缓存mapfun- 获取类型的函数predicate- 判断相等的函数- Returns:
- 实例
-
beanOfNmTp
获取bean- Type Parameters:
T- 类型- Parameters:
name- 名称clazz- 类型- Returns:
- 实例
-
registerBean
动态注册bean- Type Parameters:
T- 对象- Parameters:
beanName- bean的名称clazz- 对象类型args- 参数- Returns:
- bean
-