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. ThinkPHP各个目录是什么含义ThinkPHP怎么安装和使用

    最近kdchxue看完了smarty之后,想学习下框架,于是乎就选择了ThinkPHP,听说这个框架简单易用,另外还是国产的!所以kdchxue毫不犹豫的就选择了ThinkPHP 了!下面看看Thin ...

  2. delphi 接口

    第四章          接口 前不久,有位搞软件的朋友给我出了个谜语.谜面是“相亲”,让我猜一软件术语.我大约想了一分钟,猜 出谜底是“面向对象”.我觉得挺有趣,灵机一动想了一个谜语回敬他.谜面是“ ...

  3. C# 实体对象作为参数统一去除空格

    /** * ------------------------------------------------------------------------------ * @Copyright in ...

  4. rpm安装包制作

    RPM是RPM Package Manager(RPM软件包管理器) 1. 安装制作工具 # yum install rpm-build 2. 目录结构 /root/rpmbuild/SOURCES ...

  5. Android源码分析(十一)-----Android源码中如何引用aar文件

    一:aar文件如何引用 系统Settings中引用bidehelper-1.1.12.aar 文件为例 源码地址:packages/apps/Settings/Android.mk LOCAL_PAT ...

  6. .net web mvc 权限验证

    这里分享MVC的权限验证,内容中可能存在一些,莫名其妙的方法,那些是以前封装好的,大致可以根据方法名称知道他的意思. using Game.Entity; using Game.Entity.Plat ...

  7. mysql日期存储格式int,timestarmp,datetime

    int (1).4个字节存储,INT的长度是4个字节,存储空间上比datatime少,int索引存储空间也相对较小,排序和查询效率相对较高一点点 (2)可读性极差,无法直观的看到数据. TIMESTA ...

  8. 【目录】Jmeter系列

    为天地立心,为生民立命.为往圣继绝学,为万世开太平. ---<横渠四句>·张载 Jmeter(一)简介以及环境搭建 Jmeter(二)Jmeter目录介绍 Jmeter(三)Test-Pl ...

  9. java.sql.SQLException: Access denied for user 'root'@'10.1.0.2' (using password: YES)

    java.sql.SQLException: Access denied for user 'root'@'10.1.0.2' (using password: YES) at com.mysql.c ...

  10. 小人大作战v0.02原型(单机)发布

    运行环境,pc,windows 链接:https://pan.baidu.com/s/1X5XR0flRAVuinnydNyRNng 提取码:cp9q 复制这段内容后打开百度网盘手机App,操作更方便 ...