Spring提供的方法:Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;

1,作用:Find all beans whose Class has the supplied Annotation type.

    找到所有拥有annotationType注解的bean;

2,参数:annotationType - the type of annotation to look for:

    需要获得的bean的注解类型。

3,Returns: a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values。

    返回一个被注解表示的bean的Map对象,bean的names作为key,与name对应的实例做为值。

4. Throws: BeansException - if a bean could not be created、 

    如果bean不能被创建,抛出beanException!

遍历所有bean:

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    Map<String, Object> beans
= event.getApplicationContext().getBeansWithAnnotation(MsgTypeHandler.class);
    MessageServiceContext
messageServiceContext =
event.getApplicationContext().getBean(MessageServiceContext.class);
    beans.forEach((name, bean)
-> {
        MsgTypeHandler typeHandler =
bean.getClass().getAnnotation(MsgTypeHandler.class);
        messageServiceContext.putMessageService(typeHandler.value().code,
(MessageService) bean);
      }

  );
}
未完待续~

Spring通过注解获取所有被注解标注的Beans的更多相关文章

  1. Spring启动后获取所有拥有特定注解的Bean,注解的属性值

    最近项目中遇到一个业务场景,就是在Spring容器启动后获取所有的Bean中实现了一个特定接口的对象,第一个想到的是ApplicationContextAware,在setApplicationCon ...

  2. 为何在新线程中使用注解获取不到Spring管理的Bean

    新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...

  3. Spring启动时获取自定义注解的属性值

    1.自定义注解 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  4. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

  5. spring AOP 之二:@AspectJ注解的3种配置

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  6. Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils

    Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...

  7. Spring 学习笔记(八)—— 注解使用整合

    @Autowired  —— 自动装配 需先在配置文件中,配置一个org.springframework.beans.factory.annotation. AutowiredAnnotationBe ...

  8. Spring 注解编程之模式注解

    Spring 框架中有很多可用的注解,其中有一类注解称模式注解(Stereotype Annotations),包括 @Component, @Service,@Controller,@Reposit ...

  9. Spring——原理解析-利用反射和注解模拟IoC的自动装配

    解析Spring的IoC容器基于注解实现的自动装配(自动注入依赖)的原理 1.本文案例 使用注解和反射机制来模拟Spring中IoC的自动装配功能 定义两个注解:@Component,用来标注组件:@ ...

随机推荐

  1. .Net IOC框架入门之——Unity

    一.概述 IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection). 作用:将各层的对象以松耦合的方式组织在一 ...

  2. Java I/O系统学习系列三:I/O流的典型使用方式

    尽管可以通过不同的方式组合IO流类,但我们可能也就只用到其中的几种组合.下面的例子可以作为典型的IO用法的基本参考.在这些示例中,异常处理都被简化为将异常传递给控制台,但是这只有在小型示例和工具中才适 ...

  3. SpringBoot健康检查实现原理

    相信看完之前文章的同学都知道了SpringBoot自动装配的套路了,直接看spring.factories文件,当我们使用的时候只需要引入如下依赖 <dependency> <gro ...

  4. vue+element省市县的二级联动功能

    项目中有选择省市县的需求,先选择省,再选择县 解决这个需求也不是很难,总体思路就是看后端接口, 一般后端接口都是请求参数为 0 返回省的数据,不为 0 的话返回相对应的市的数据 template代码: ...

  5. uni-app项目记录

    1.如何定义一个全局属性 在App.vue 文件中,以 global.属性名= XXX; 定义 在其他页面就以 global.属性名来调用 或者在min.js 中使用 Vue.prototype 挂载 ...

  6. android studio学习----The project encoding (windows-1252) does not match the encoding specified in the Gradle build files (UTF-8)

    Warning:The project encoding (windows-1252) does not match the encoding specified in the Gradle buil ...

  7. 剑指:链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. 解法 pre 指针走 k-1 步.之后 cur 指针指向 phead,然后两个指针同时走,直至 pre 指针到达尾结点. 即cur与pre始终相距 ...

  8. shell 的 正则表达式

    shell的正则表达式规则 https://www.jb51.net/tools/shell_regex.html 常规字符 字符 描述 \ 将下一个字符标记为一个特殊字符.或一个原义字符.例如,“n ...

  9. pg 数据库操作

    一.pg数据库修改操作 Insert into table (key) values (value) on conflict(主键) do update set key=value; 修改的 valu ...

  10. GCN总结

    一.GCN简介 GNN模型主要研究图节点的表示(Graph Embedding),图边结构预测任务和图的分类问题,后两个任务也是基于Graph Embedding展开的.目前论文重点研究网络的可扩展性 ...