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

com.gitee.starblues.utils.SpringBeanUtils Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.gitee.starblues.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.util.ClassUtils;

import java.util.*;

/**
 * 插件bean工具类
 * @author starBlues
 * @version 2.4.0
 */
public class SpringBeanUtils {

    /**
     * 得到ApplicationContext中的bean的实现
     * @param applicationContext ApplicationContext
     * @param aClass 接口或者抽象类型bean类型
     * @param  接口或者抽象类型bean类型
     * @return 所有的实现对象
     */
    public static  List getBeans(ApplicationContext applicationContext, Class aClass) {
        Map beansOfTypeMap = applicationContext.getBeansOfType(aClass);
        if(beansOfTypeMap.isEmpty()){
            return Collections.emptyList();
        }
        return new ArrayList<>(beansOfTypeMap.values());
    }

    /**
     * 得到某个接口的实现对象
     * @param sourceObject 遍历的对象
     * @param interfaceClass 接口类类型
     * @param  接口类型
     * @return 实现对象
     */
    public static  T getObjectByInterfaceClass(Set sourceObject, Class interfaceClass){
        if(sourceObject == null || sourceObject.isEmpty()){
            return null;
        }
        for (Object configSingletonObject : sourceObject) {
            Set> allInterfacesForClassAsSet = ClassUtils
                    .getAllInterfacesAsSet(configSingletonObject);
            if(allInterfacesForClassAsSet.contains(interfaceClass)){
                return (T) configSingletonObject;
            }
        }
        return null;
    }

    /**
     * 获取具体类的对象
     * @param sourceObject 源对象集合
     * @param aClass 对象对应的类类型
     * @param  类实现
     * @return T
     */
    public static  T getObjectClass(Set sourceObject, Class aClass){
        if(sourceObject == null || sourceObject.isEmpty()){
            return null;
        }
        for (Object configSingletonObject : sourceObject) {
            if(Objects.equals(configSingletonObject.getClass(), aClass)){
                return (T) configSingletonObject;
            }
        }
        return null;
    }

    /**
     * 得到存在的bean, 不存在则返回null
     * @param applicationContext ApplicationContext容器
     * @param aClass bean 类型
     * @param  bean 类型
     * @return 存在bean对象, 不存在返回null
     */
    public static  T getExistBean(ApplicationContext applicationContext, Class aClass){
        String[] beanNamesForType = applicationContext.getBeanNamesForType(aClass, false, false);
        if(beanNamesForType.length > 0){
            return applicationContext.getBean(aClass);
        } else {
            return null;
        }
    }

    /**
     * 得到存在的bean, 不存在则返回null
     * @param applicationContext ApplicationContext容器
     * @param beanName bean 名称
     * @param  返回的bean类型
     * @return 存在bean对象, 不存在返回null
     */
    public static  T getExistBean(ApplicationContext applicationContext, String beanName){
        if(applicationContext.containsBean(beanName)){
            Object bean = applicationContext.getBean(beanName);
            return (T) bean;
        } else {
            return null;
        }
    }

}