spring 获取指定接口或父类的所有实现类 并筛选出指定的实现类

spring 获取指定接口或父类的所有实现类 并筛选出指定的实现类

  • 处理方式 父类(接口)-->子类(实现)-->含有指定注解-->bean
    • 注解
    • 接口
    • 接口实现类
    • 核心工具方法
    • 核心工具类

处理方式 父类(接口)–>子类(实现)–>含有指定注解–>bean

注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Service
public @interface OpenPlatformBusinessCode {
    /***
     * 平台业务代码
     * @return
     */
    OpenPlatformBusinessEnums openPlatformBusinessCode();
}

用去筛选指定实现bean

接口

public interface UserManager {}

接口实现类

@OpenPlatformBusinessCode(openPlatformBusinessCode = OpenPlatformBusinessEnums.LECHANGE)
public class xx implements UserManager {}

核心工具方法

    public static  T getBeansWithAnnotation(Class manager, Class annotation, String code) throws BeansException {
        if (ObjectUtils.isEmpty(code)) {
            throw new RuntimeException("OpenPlatformBusinessCode is null ");
        }
        Collection tCollection = applicationContext.getBeansOfType(manager).values();
        for (T t : tCollection) {
            OpenPlatformBusinessCode openPlatformBusinessCode = t.getClass().getAnnotation(annotation);
            if (ObjectUtils.isEmpty(openPlatformBusinessCode)) {
                throw new RuntimeException("not found code by OpenPlatformBusinessCode :" + code);
            }
            if (code.equals(openPlatformBusinessCode.openPlatformBusinessCode().getOpenPlatformCode())) {
                return t;
            }
        }
        throw new RuntimeException("fail to find  code by OpenPlatformBusinessCode :" + code);
    }

核心工具类

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        // TODO Auto-generated method stub
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static Object getBean(String beanId) throws BeansException {
        return applicationContext.getBean(beanId);
    }

    public static Object getBeansWithAnnotation(Class annotation) throws BeansException {
        return applicationContext.getBeansWithAnnotation(annotation);
    }

    public static  T getBeansWithAnnotation(Class manager, Class annotation, String code) throws BeansException {
        if (ObjectUtils.isEmpty(code)) {
            throw new RuntimeException("OpenPlatformBusinessCode is null ");
        }
        Collection tCollection = applicationContext.getBeansOfType(manager).values();
        for (T t : tCollection) {
            OpenPlatformBusinessCode openPlatformBusinessCode = t.getClass().getAnnotation(annotation);
            if (ObjectUtils.isEmpty(openPlatformBusinessCode)) {
                throw new RuntimeException("not found code by OpenPlatformBusinessCode :" + code);
            }
            if (code.equals(openPlatformBusinessCode.openPlatformBusinessCode().getOpenPlatformCode())) {
                return t;
            }
        }
        throw new RuntimeException("fail to find  code by OpenPlatformBusinessCode :" + code);
    }

    /**
     * @param manager
     * @param code
     * @param 
     * @return
     * @throws BeansException
     */
    public static  T getBeansWithAnnotation(Class manager, String code) throws BeansException {
        return getBeansWithAnnotation(manager, OpenPlatformBusinessCode.class, code);
    }


    @Deprecated
    public static  Object xx(Class manager, Class annotation, String code) throws BeansException {

//        Collection tCollection= applicationContext.getBeansOfType(manager).values();
//        for (T t : tCollection) {
//            OpenPlatformBusiness openPlatformBusiness= t.getClass().getAnnotation(annotation);
//            if(code.equals(openPlatformBusiness.openPlatformBusinessCode())){
//                return t;
//            }
//        }
//        ReflectionUtils

//            String[] names = BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(beanFactory,
//                    EnableAuthorizationServer.class);
//            if (names.length > 0) {
//                BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TokenKeyEndpoint.class);
//                builder.addConstructorArgReference(names[0]);
//                registry.registerBeanDefinition(TokenKeyEndpoint.class.getName(), builder.getBeanDefinition());
//            }

//        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(manager);
//        return applicationContext.getBeansWithAnnotation(annotation);

        return null;
    }

}

你可能感兴趣的:(java,SpringBoot)