Jimmy小站

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

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

2022-02-09 / 未分类 / 6837 次围观 / 20 次吐槽

方式一 遍历所有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程序猿的转型之路,读研深造,专注机器学习推荐算法]

已有20位网友发表了看法:

1#访客  2024-08-12 00:10:26 回复该评论
识别码:xvtzpw16495JC-好下款的借款平台2020http://www.slearning.cn/post/67334.html
2#访客1  2024-08-07 21:06:12 回复该评论
识别码:xqytgl45728PT-这游戏真不错!http://www.a5km.com/yxgl/jdqs/29601.html
3#访客  2024-08-04 04:01:18 回复该评论
识别码:xcjflw84275OJ-二十岁能贷款的平台http://www.zsdai.com/post/25526.html
识别码:uldjbh17286YD-文章写的真不错https://gaofeick.com/
5#访客游戏攻略网  2024-07-28 20:37:17 回复该评论
识别码:cirajy84721FT-文章写的真不错https://www.ldyyouxi.com/
6#访客游戏攻略网  2024-07-28 02:30:20 回复该评论
识别码:lejmcy13548JY-文章写的真不错https://www.gaofeick.com/
识别码:thlpym82746YP-文章写的真不错https://www.gaofeick.com/
8#访客游戏攻略网  2024-07-26 20:54:25 回复该评论
识别码:mdzxqs41052ZJ-文章写的真不错https://www.gaofeick.com/
9#访客  2024-07-25 23:34:06 回复该评论
识别码:orgake06295CY-急用钱但是未成年怎么办http://www.zsdai.com/post/64550.html
10#访客  2024-07-25 22:44:23 回复该评论
识别码:ispqld49351OB-怎么借到1千块钱http://www.zsdai.com/post/10418.html
11#访客  2024-07-25 19:07:49 回复该评论
识别码:rtuaym03451IZ-网贷逾期最新情况http://www.lhtyyynk.net/tags-25.html
12#访客  2024-07-25 19:07:04 回复该评论
识别码:benxfz74832YR-借款太多了借不了了怎么办http://www.lhtyyynk.net/post/36.html
13#访客游戏攻略网  2024-07-25 16:05:32 回复该评论
识别码:pmqbst89325EC-文章写的真不错https://www.ldyyouxi.com/
14#访客1  2024-07-25 04:50:42 回复该评论
识别码:supyat17980MK-这游戏真不错!http://www.a5km.com/yxgl/dnf/21605.html
15#访客1  2024-07-25 01:43:46 回复该评论
识别码:jsgoif59734PL-这游戏真不错!http://www.a5km.com/yxgl/dnf/24713.html
16#访客  2024-07-23 17:59:49 回复该评论
识别码:zicmnv10958CZ-放水借款平台论坛http://www.lhtyyynk.net/tags-12.html
17#访客  2024-07-23 17:59:10 回复该评论
识别码:qtnkrs13504EC-好下款的借款平台2020http://www.lhtyyynk.net/tags-2.html
18#访客  2024-07-23 16:46:47 回复该评论
识别码:eszmny35819YZ-怎样可以借1000块钱http://www.zsdai.com/post/40656.html
19#访客追忆追忆  2024-07-23 16:26:30 回复该评论
识别码:dgzasc64380IR-要多掌握分段要领http://www.yjtpsh.com/post/16988.html
20#访客追忆  2024-07-23 16:25:47 回复该评论
识别码:klvmbi60852LE-充分掌握主旨段落分明可取http://www.sh-fuci.com/post/17028.html

发表评论

必填

选填

选填

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

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