|
|
package com.utils;
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
|
* Spring Context 工具类
|
|
|
*
|
|
|
* 核心职责:在非Spring管理类中获取Spring容器中的Bean
|
|
|
*
|
|
|
* 设计理念:
|
|
|
* 1. 实现ApplicationContextAware接口,获取Spring应用上下文
|
|
|
* 2. 提供静态方法,方便在任何地方获取Bean
|
|
|
* 3. 支持按名称和类型获取Bean
|
|
|
* 4. 提供Bean相关信息的查询方法
|
|
|
*
|
|
|
* 使用场景:
|
|
|
* - 在工具类、静态方法中获取Spring Bean
|
|
|
* - 在非Spring管理的类中使用Spring Bean
|
|
|
* - 动态获取Bean实例
|
|
|
*
|
|
|
* 注意事项:
|
|
|
* - 需要被Spring管理(使用@Component注解)
|
|
|
* - 在Spring容器初始化完成后才能使用
|
|
|
*/
|
|
|
@Component // 声明为Spring组件,由Spring容器管理
|
|
|
public class SpringContextUtils implements ApplicationContextAware {
|
|
|
|
|
|
/**
|
|
|
* Spring应用上下文对象
|
|
|
* 通过setApplicationContext方法由Spring容器注入
|
|
|
*/
|
|
|
public static ApplicationContext applicationContext;
|
|
|
|
|
|
/**
|
|
|
* 设置应用上下文
|
|
|
* 实现ApplicationContextAware接口的方法,由Spring容器自动调用
|
|
|
*
|
|
|
* @param applicationContext Spring应用上下文
|
|
|
* @throws BeansException 如果设置上下文失败
|
|
|
*/
|
|
|
@Override
|
|
|
public void setApplicationContext(ApplicationContext applicationContext)
|
|
|
throws BeansException {
|
|
|
// 将Spring容器传入的应用上下文保存到静态变量中
|
|
|
SpringContextUtils.applicationContext = applicationContext;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过Bean名称获取Bean实例
|
|
|
*
|
|
|
* @param name Bean的名称
|
|
|
* @return Bean实例
|
|
|
* @throws BeansException 如果获取Bean失败
|
|
|
*/
|
|
|
public static Object getBean(String name) {
|
|
|
return applicationContext.getBean(name);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过Bean名称和类型获取Bean实例
|
|
|
*
|
|
|
* @param name Bean的名称
|
|
|
* @param requiredType Bean的类型
|
|
|
* @param <T> Bean的泛型类型
|
|
|
* @return 指定类型的Bean实例
|
|
|
* @throws BeansException 如果获取Bean失败或类型不匹配
|
|
|
*/
|
|
|
public static <T> T getBean(String name, Class<T> requiredType) {
|
|
|
return applicationContext.getBean(name, requiredType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过类型获取Bean实例
|
|
|
*
|
|
|
* @param requiredType Bean的类型
|
|
|
* @param <T> Bean的泛型类型
|
|
|
* @return 指定类型的Bean实例
|
|
|
* @throws BeansException 如果获取Bean失败
|
|
|
*/
|
|
|
public static <T> T getBean(Class<T> requiredType) {
|
|
|
return applicationContext.getBean(requiredType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查Spring容器中是否包含指定名称的Bean
|
|
|
*
|
|
|
* @param name Bean的名称
|
|
|
* @return 如果包含返回true,否则返回false
|
|
|
*/
|
|
|
public static boolean containsBean(String name) {
|
|
|
return applicationContext.containsBean(name);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查指定名称的Bean是否为单例
|
|
|
*
|
|
|
* @param name Bean的名称
|
|
|
* @return 如果是单例返回true,否则返回false
|
|
|
*/
|
|
|
public static boolean isSingleton(String name) {
|
|
|
return applicationContext.isSingleton(name);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取指定名称Bean的类型
|
|
|
*
|
|
|
* @param name Bean的名称
|
|
|
* @return Bean的类型,如果Bean不存在返回null
|
|
|
*/
|
|
|
public static Class<? extends Object> getType(String name) {
|
|
|
return applicationContext.getType(name);
|
|
|
}
|
|
|
|
|
|
// 可以添加的增强方法:
|
|
|
|
|
|
/**
|
|
|
* 获取应用上下文本身
|
|
|
*
|
|
|
* @return Spring应用上下文
|
|
|
*/
|
|
|
public static ApplicationContext getApplicationContext() {
|
|
|
return applicationContext;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过类型获取所有匹配的Bean(包括子类)
|
|
|
*
|
|
|
* @param type Bean的类型
|
|
|
* @param <T> Bean的泛型类型
|
|
|
* @return 匹配的Bean Map,key为Bean名称,value为Bean实例
|
|
|
*/
|
|
|
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
|
|
|
return applicationContext.getBeansOfType(type);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取所有Bean的名称
|
|
|
*
|
|
|
* @return Bean名称数组
|
|
|
*/
|
|
|
public static String[] getBeanDefinitionNames() {
|
|
|
return applicationContext.getBeanDefinitionNames();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取环境变量中的属性值
|
|
|
*
|
|
|
* @param key 属性键
|
|
|
* @return 属性值
|
|
|
*/
|
|
|
public static String getProperty(String key) {
|
|
|
return applicationContext.getEnvironment().getProperty(key);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取环境变量中的属性值,带默认值
|
|
|
*
|
|
|
* @param key 属性键
|
|
|
* @param defaultValue 默认值
|
|
|
* @return 属性值,如果不存在返回默认值
|
|
|
*/
|
|
|
public static String getProperty(String key, String defaultValue) {
|
|
|
return applicationContext.getEnvironment().getProperty(key, defaultValue);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取环境变量中的属性值,指定类型
|
|
|
*
|
|
|
* @param key 属性键
|
|
|
* @param targetType 目标类型
|
|
|
* @param <T> 泛型类型
|
|
|
* @return 属性值
|
|
|
*/
|
|
|
public static <T> T getProperty(String key, Class<T> targetType) {
|
|
|
return applicationContext.getEnvironment().getProperty(key, targetType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发布应用事件
|
|
|
*
|
|
|
* @param event 应用事件
|
|
|
*/
|
|
|
public static void publishEvent(Object event) {
|
|
|
applicationContext.publishEvent(event);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查Spring上下文是否已初始化
|
|
|
*
|
|
|
* @return 如果已初始化返回true,否则返回false
|
|
|
*/
|
|
|
public static boolean isInitialized() {
|
|
|
return applicationContext != null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前激活的Profile
|
|
|
*
|
|
|
* @return 激活的Profile数组
|
|
|
*/
|
|
|
public static String[] getActiveProfiles() {
|
|
|
return applicationContext.getEnvironment().getActiveProfiles();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查当前是否激活了指定的Profile
|
|
|
*
|
|
|
* @param profile 要检查的Profile
|
|
|
* @return 如果激活返回true,否则返回false
|
|
|
*/
|
|
|
public static boolean isProfileActive(String profile) {
|
|
|
String[] activeProfiles = getActiveProfiles();
|
|
|
for (String activeProfile : activeProfiles) {
|
|
|
if (activeProfile.equals(profile)) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
} |