@Component public class SpringUtil extends Object implements org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.context.ApplicationContextAware
Constructor and Description |
---|
SpringUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
getActiveProfile()
获取当前的环境配置,当有多个环境配置时,只获取第一个
|
static String[] |
getActiveProfiles()
获取当前的环境配置,无配置返回null
|
static org.springframework.context.ApplicationContext |
getApplicationContext()
获取
ApplicationContext |
static String |
getApplicationName()
获取应用程序名称
|
static <T> T |
getBean(Class<T> clazz)
通过class获取Bean
|
static <T> T |
getBean(String name)
通过name获取 Bean
|
static <T> T |
getBean(String name,
Class<T> clazz)
通过name,以及Clazz返回指定的Bean
|
static <T> T |
getBean(TypeReference<T> reference)
通过类型参考返回带泛型参数的Bean
|
static org.springframework.beans.factory.ListableBeanFactory |
getBeanFactory()
获取
ListableBeanFactory ,可能为ConfigurableListableBeanFactory 或 ApplicationContextAware |
static String[] |
getBeanNamesForType(Class<?> type)
获取指定类型对应的Bean名称,包括子类
|
static <T> Map<String,T> |
getBeansOfType(Class<T> type)
获取指定类型对应的所有Bean,包括子类
|
static org.springframework.beans.factory.config.ConfigurableListableBeanFactory |
getConfigurableBeanFactory()
获取
ConfigurableListableBeanFactory |
static String |
getProperty(String key)
获取配置文件配置项的值
|
static <T> T |
getProperty(String key,
Class<T> targetType,
T defaultValue)
获取配置文件配置项的值
|
static String |
getProperty(String key,
String defaultValue)
获取配置文件配置项的值
|
void |
postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) |
static void |
publishEvent(org.springframework.context.ApplicationEvent event)
发布事件
|
static void |
publishEvent(Object event)
发布事件
Spring 4.2+ 版本事件可以不再是
ApplicationEvent 的子类 |
static <T> void |
registerBean(String beanName,
T bean)
动态向Spring注册Bean
由
BeanFactory 实现,通过工具开放API
更新: shadow 2021-07-29 17:20:44 增加自动注入,修复注册bean无法反向注入的问题 |
void |
setApplicationContext(org.springframework.context.ApplicationContext applicationContext) |
static void |
unregisterBean(String beanName)
注销bean
将Spring中的bean注销,请谨慎使用
|
public void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) throws org.springframework.beans.BeansException
postProcessBeanFactory
in interface org.springframework.beans.factory.config.BeanFactoryPostProcessor
org.springframework.beans.BeansException
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
setApplicationContext
in interface org.springframework.context.ApplicationContextAware
public static org.springframework.context.ApplicationContext getApplicationContext()
ApplicationContext
ApplicationContext
public static org.springframework.beans.factory.ListableBeanFactory getBeanFactory()
ListableBeanFactory
,可能为ConfigurableListableBeanFactory
或 ApplicationContextAware
ListableBeanFactory
public static org.springframework.beans.factory.config.ConfigurableListableBeanFactory getConfigurableBeanFactory() throws UtilException
ConfigurableListableBeanFactory
ConfigurableListableBeanFactory
UtilException
- 当上下文非ConfigurableListableBeanFactory抛出异常public static <T> T getBean(String name)
T
- Bean类型name
- Bean名称public static <T> T getBean(Class<T> clazz)
T
- Bean类型clazz
- Bean类public static <T> T getBean(String name, Class<T> clazz)
T
- bean类型name
- Bean名称clazz
- bean类型public static <T> T getBean(TypeReference<T> reference)
T
- Bean类型reference
- 类型参考,用于持有转换后的泛型类型public static <T> Map<String,T> getBeansOfType(Class<T> type)
T
- Bean类型type
- 类、接口,null表示获取所有beanpublic static String[] getBeanNamesForType(Class<?> type)
type
- 类、接口,null表示获取所有bean名称public static String getProperty(String key)
key
- 配置项keypublic static String getProperty(String key, String defaultValue)
key
- 配置项keydefaultValue
- 默认值public static <T> T getProperty(String key, Class<T> targetType, T defaultValue)
T
- 属性值类型key
- 配置项keytargetType
- 配置项类型defaultValue
- 默认值public static String getApplicationName()
public static String[] getActiveProfiles()
public static String getActiveProfile()
public static <T> void registerBean(String beanName, T bean)
由BeanFactory
实现,通过工具开放API
更新: shadow 2021-07-29 17:20:44 增加自动注入,修复注册bean无法反向注入的问题
T
- Bean类型beanName
- 名称bean
- beanpublic static void unregisterBean(String beanName)
将Spring中的bean注销,请谨慎使用
beanName
- bean名称public static void publishEvent(org.springframework.context.ApplicationEvent event)
event
- 待发布的事件,事件必须是ApplicationEvent
的子类public static void publishEvent(Object event)
ApplicationEvent
的子类event
- 待发布的事件Copyright © 2024. All rights reserved.