All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mobaijun.core.spring.SpringUtil Maven / Gradle / Ivy

There is a newer version: 3.0.8
Show newest version
/*
 * Copyright (C) 2022 [www.mobaijun.com]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mobaijun.core.spring;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ArrayUtil;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.Map;
import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.autoconfigure.thread.Threading;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;

/**
 * software:IntelliJ IDEA 2022.2.3
* class name: SpringUtil
* class description: spring 工具类
* * @author MoBaiJun 2022/12/7 14:23 */ public class SpringUtil implements ApplicationContextAware, BeanFactoryPostProcessor { /** * ConfigurableListableBeanFactory */ private static ConfigurableListableBeanFactory beanFactory; /** * ApplicationContext * -- GETTER -- * 获取应用程序上下文 */ @Getter private static ApplicationContext applicationContext; /** * 发布自定义事件 *

* Spring 4.2+ 版本起,发布的事件不再局限于 {@link ApplicationEvent} 子类。 * 此方法将事件发布到当前 Spring 应用程序上下文。 * * @param event 待发布的事件 */ public static void publishEvent(Object event) { if (null != applicationContext) { applicationContext.publishEvent(event); } } /** * 检查 Bean 是否存在 * * @param name Bean 的名称 * @return 如果 BeanFactory 中存在该 Bean,则返回 true;否则返回 false */ public static boolean containsBean(String name) { return getBeanFactory().containsBean(name); } /** * 获取{@link org.springframework.beans.factory.ListableBeanFactory},可能为{@link ConfigurableListableBeanFactory} 或 {@link ApplicationContextAware} * * @return {@link org.springframework.beans.factory.ListableBeanFactory} */ public static ListableBeanFactory getBeanFactory() { final ListableBeanFactory factory = null == beanFactory ? applicationContext : beanFactory; if (null == factory) { throw new RuntimeException("No ConfigurableListableBeanFactory or ApplicationContext injected, maybe not in the Spring environment?"); } return factory; } /** * 获取{@link ConfigurableListableBeanFactory} * * @return {@link ConfigurableListableBeanFactory} */ public static ConfigurableListableBeanFactory getConfigurableBeanFactory() { final ConfigurableListableBeanFactory factory; if (null != beanFactory) { factory = beanFactory; } else if (applicationContext instanceof ConfigurableApplicationContext) { factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); } else { throw new RuntimeException("No ConfigurableListableBeanFactory from context!"); } return factory; } /** * 通过name获取 Bean * * @param Bean类型 * @param name Bean名称 * @return Bean */ @SuppressWarnings("unchecked") public static T getBean(String name) { return (T) getBeanFactory().getBean(name); } /** * 通过class获取Bean * * @param Bean类型 * @param clazz Bean类 * @return Bean对象 */ public static T getBean(Class clazz) { return getBeanFactory().getBean(clazz); } /** * 通过name,以及Clazz返回指定的Bean * * @param bean类型 * @param name Bean名称 * @param clazz bean类型 * @return Bean对象 */ public static T getBean(String name, Class clazz) { return getBeanFactory().getBean(name, clazz); } /** * 通过类型参考返回带泛型参数的Bean * * @param reference 类型参考,用于持有转换后的泛型类型 * @param Bean类型 * @return 带泛型参数的Bean * @since 5.4.0 */ @SuppressWarnings("unchecked") public static T getBean(TypeReference reference) { final ParameterizedType parameterizedType = (ParameterizedType) reference.getType(); final Class rawType = (Class) parameterizedType.getRawType(); final Class[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class) type).toArray(Class[]::new); final String[] beanNames = getBeanFactory().getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes)); return getBean(beanNames[0], rawType); } /** * 获取指定类型对应的所有Bean,包括子类 * * @param Bean类型 * @param type 类、接口,null表示获取所有bean * @return 类型对应的bean,key是bean注册的name,value是Bean */ public static Map getBeansOfType(Class type) { return getBeanFactory().getBeansOfType(type); } /** * 获取指定类型对应的Bean名称,包括子类 * * @param type 类、接口,null表示获取所有bean名称 * @return bean名称 */ public static String[] getBeanNamesForType(Class type) { return getBeanFactory().getBeanNamesForType(type); } /** * 获取配置文件配置项的值 * * @param key 配置项key * @return 属性值 */ public static String getProperty(String key) { if (null == applicationContext) { return null; } return applicationContext.getEnvironment().getProperty(key); } /** * 获取配置文件配置项的值 * * @param key 配置项key * @param defaultValue 默认值 * @return 属性值 */ public static String getProperty(String key, String defaultValue) { if (null == applicationContext) { return null; } return applicationContext.getEnvironment().getProperty(key, defaultValue); } /** * 获取配置文件配置项的值 * * @param 属性值类型 * @param key 配置项key * @param targetType 配置项类型 * @param defaultValue 默认值 * @return 属性值 */ public static T getProperty(String key, Class targetType, T defaultValue) { if (null == applicationContext) { return null; } return applicationContext.getEnvironment().getProperty(key, targetType, defaultValue); } /** * 获取应用程序名称 * * @return 应用程序名称 */ public static String getApplicationName() { return getProperty("spring.application.name"); } /** * 获取当前的环境配置,无配置返回null * * @return 当前的环境配置 */ public static String[] getActiveProfiles() { if (null == applicationContext) { return null; } return applicationContext.getEnvironment().getActiveProfiles(); } /** * 获取当前的环境配置,当有多个环境配置时,只获取第一个 * * @return 当前的环境配置 */ public static String getActiveProfile() { final String[] activeProfiles = getActiveProfiles(); return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; } /** * 动态向Spring注册Bean *

* 由{@link org.springframework.beans.factory.BeanFactory} 实现,通过工具开放API *

* 更新: shadow 2021-07-29 17:20:44 增加自动注入,修复注册bean无法反向注入的问题 * * @param Bean类型 * @param beanName 名称 * @param bean bean * @author shadow */ public static void registerBean(String beanName, T bean) { final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory(); factory.autowireBean(bean); factory.registerSingleton(beanName, bean); } /** * 注销bean *

* 将Spring中的bean注销,请谨慎使用 * * @param beanName bean名称 * @author shadow */ public static void unregisterBean(String beanName) { final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory(); if (factory instanceof DefaultSingletonBeanRegistry registry) { registry.destroySingleton(beanName); } else { throw new RuntimeException("Can not unregister bean, the factory is not a DefaultSingletonBeanRegistry!"); } } /** * 发布事件 * * @param event 待发布的事件,事件必须是{@link ApplicationEvent}的子类 */ public static void publishEvent(ApplicationEvent event) { if (null != applicationContext) { applicationContext.publishEvent(event); } } /** * 判断 Bean 的作用域 * * @param name Bean 的名称 * @return 如果 Bean 是单例,则返回 true;否则返回 false * @throws NoSuchBeanDefinitionException 如果找不到指定的 Bean */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return getBeanFactory().isSingleton(name); } /** * 获取 Bean 的类型 * * @param name Bean 的名称 * @return Bean 的 Class 对象 * @throws NoSuchBeanDefinitionException 如果找不到指定的 Bean */ public static Class getType(String name) throws NoSuchBeanDefinitionException { return getBeanFactory().getType(name); } /** * 获取 Bean 的别名 * * @param name Bean 的名称 * @return Bean 的所有别名,如果不存在别名,则返回空数组 * @throws NoSuchBeanDefinitionException 如果找不到指定的 Bean */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return getBeanFactory().getAliases(name); } /** * 获取 AOP 代理对象 * * @param invoker 目标对象 * @param 目标对象的类型 * @return AOP 代理对象 */ @SuppressWarnings("unchecked") public static T getAopProxy(T invoker) { return (T) getBean(invoker.getClass()); } /** * 获取 Spring 应用程序上下文 * * @return 当前 Spring 应用程序上下文 */ public static ApplicationContext context() { return getApplicationContext(); } /** * 判断当前环境是否为虚拟环境 * * @return 如果当前环境为虚拟环境,则返回 true;否则返回 false */ public static boolean isVirtual() { return Threading.VIRTUAL.isActive(getBean(Environment.class)); } @Override public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } @Override public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtil.beanFactory = beanFactory; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy