处理方式 父类(接口)–>子类(实现)–>含有指定注解–>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> T getBeansWithAnnotation(Class<T> manager, Class<? extends OpenPlatformBusinessCode> annotation, String code) throws BeansException {
if (ObjectUtils.isEmpty(code)) {
throw new RuntimeException("OpenPlatformBusinessCode is null ");
}
Collection<T> 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<? extends Annotation> annotation) throws BeansException {
return applicationContext.getBeansWithAnnotation(annotation);
}
public static <T> T getBeansWithAnnotation(Class<T> manager, Class<? extends OpenPlatformBusinessCode> annotation, String code) throws BeansException {
if (ObjectUtils.isEmpty(code)) {
throw new RuntimeException("OpenPlatformBusinessCode is null ");
}
Collection<T> 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 <T>
* @return
* @throws BeansException
*/
public static <T> T getBeansWithAnnotation(Class<T> manager, String code) throws BeansException {
return getBeansWithAnnotation(manager, OpenPlatformBusinessCode.class, code);
}
@Deprecated
public static <T> Object xx(Class<T> manager, Class<? extends OpenPlatformBusinessCode> annotation, String code) throws BeansException {
// Collection<T> 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;
}
}