Jimmy小站

小明也有大梦想 — 蒋明/铭
当前位置:网站首页 / 未分类 / 正文

根据自定义的注解,将spring容器中的Bean定义换一个名字(注:前提是这个Bean定义已经存在)

2022-02-09 / 未分类 / 1150 次围观 / 0 次吐槽

方式一 遍历所有Beanname 检查是否有指定注解

@Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {


        String[] beanDefinitionNames = registry.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = registry.getBeanDefinition(beanDefinitionName);
            if (!(beanDefinition instanceof AnnotatedBeanDefinition)) {
                continue;
            }
            AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
            AnnotationMetadata annotationMetadata = annotatedBeanDefinition.getMetadata();
            Map<String, Object> annotationAttributes = annotationMetadata.getAnnotationAttributes(GPServiceImpl.class.getName());

        }

    }

方式二 通过enviroment过滤指定注解

(缺点,需要知道具体的包路径,如果是提供二方包给第三方使用,不能明确知道第三方的包路径,所以不推荐)

@Slf4j
@Component
public class DemoSpringContext2 implements BeanDefinitionRegistryPostProcessor, ResourceLoaderAware, EnvironmentAware {

private Environment environment;
private ResourceLoader resourceLoader;

@Value("${project.name}")
String projectName;

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false, environment, resourceLoader);
    scanner.addIncludeFilter(new AnnotationTypeFilter(GPServiceImpl.class));
    // 扫描的包路径
    Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents("xxx.general.platform.core");
    try {
        registerCandidateComponents(registry,candidateComponents);
    } catch (ClassNotFoundException e) {
        log.error("postProcessBeanDefinitionRegistry error");
    }
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    System.out.println("beanFactory = " + beanFactory);
}

@Override
public void setEnvironment(Environment environment) {
    this.environment = environment;
}

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
}

/**
 * 注册 BeanDefinition
 */
private void registerCandidateComponents(BeanDefinitionRegistry registry, Set<BeanDefinition> candidateComponents) throws ClassNotFoundException {
    for (BeanDefinition candidateComponent : candidateComponents) {
        if (candidateComponent instanceof AnnotatedBeanDefinition) {
            AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) candidateComponent;
            AnnotationMetadata annotationMetadata = annotatedBeanDefinition.getMetadata();
            Map<String, Object> map2 = Optional.ofNullable(annotationMetadata.getAnnotationAttributes(GPServiceImpl.class.getName()))
                    .orElse(new HashMap<>());

            String spiCode = String.valueOf(map2.get("spiCode"));
            String bundleId = String.valueOf(map2.get("bundleId"));
            String finalBundleId = StringUtils.isNotBlank(bundleId) ? bundleId : "jimmy";

            String implVersion = Joiner.on("#").skipNulls().join(spiCode, finalBundleId);

            registry.registerBeanDefinition(implVersion, candidateComponent);
        }
    }
}

}

推荐您阅读更多有关于“”的文章

[一个Java程序猿的转型之路,读研深造,专注机器学习推荐算法]
额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Copyright © Jimmy小站 Allrights Reserved.备案号:桂ICP备 15005996