Spring通过注解获取所有被注解标注的Beans
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的更多相关文章
- Spring启动后获取所有拥有特定注解的Bean,注解的属性值
最近项目中遇到一个业务场景,就是在Spring容器启动后获取所有的Bean中实现了一个特定接口的对象,第一个想到的是ApplicationContextAware,在setApplicationCon ...
- 为何在新线程中使用注解获取不到Spring管理的Bean
新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...
- Spring启动时获取自定义注解的属性值
1.自定义注解 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- 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 ...
- spring AOP 之二:@AspectJ注解的3种配置
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils
Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...
- Spring 学习笔记(八)—— 注解使用整合
@Autowired —— 自动装配 需先在配置文件中,配置一个org.springframework.beans.factory.annotation. AutowiredAnnotationBe ...
- Spring 注解编程之模式注解
Spring 框架中有很多可用的注解,其中有一类注解称模式注解(Stereotype Annotations),包括 @Component, @Service,@Controller,@Reposit ...
- Spring——原理解析-利用反射和注解模拟IoC的自动装配
解析Spring的IoC容器基于注解实现的自动装配(自动注入依赖)的原理 1.本文案例 使用注解和反射机制来模拟Spring中IoC的自动装配功能 定义两个注解:@Component,用来标注组件:@ ...
随机推荐
- nginx配置ssl证书实现https
一,环境说明 服务器系统:centos6 服务器IP地址:0.0.0.0 域名:bjubi.com 二,域名解析到服务器 在阿里云控制台-产品与服务-云解析DNS-找到需要解析的域名点“解析”,进入解 ...
- topshelf注册服务
1.需要从nutget上获取toshelf配置 2.代码 using Common.Logging; using Quartz; using Quartz.Impl; using System; us ...
- RabbitMQ高级面试题
本文涉及:投递失败的消息怎么处理.如何实现延时队列.如何指定消息的优先级.消息的持久化是如何实现的.如何保证消息不丢失 投递失败的消息怎么处理 首先投递失败存在如下两个情况 当交换器无法根据自身的类型 ...
- 左右对齐Justify遇到的坑
遇到的问题 这两天在开发一个病历的对外展示页面,设计稿上label是左右拉伸对齐的,显示效果如下: 怎么实现这种效果呢? 首先想到的是文字中间加空格,但是这种方式太low了,而且不太容易控制.网上查资 ...
- Android中实现Activity的透明背景效果
实现方式一(使用系统透明样式) 通过配置 Activity 的样式来实现,在 AndroidManifest.xml 找到要实现透明效果的 Activity,在 Activity 的配置中添加如下的代 ...
- springboot 启动停止脚本
https://www.cnblogs.com/lovychen/p/6211209.html 参考 centos 转码解决方案: yum install dos2unix dos2unix ** ...
- Docker在Centos 7上的部署
Docker在Centos 7上的部署 方法1---开启centos 7上自带的 extras YUM源,然后 yum install docker来安装 安装前必须保证 Linux Kernel ...
- rhel7学习第五天
管道命令符的功能的确强大!
- HDU 1253 胜利大逃亡 题解
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- STL杂记
STL介绍: 我所理解的stl: 容器: 是一种数据结构,如list,vector,和deques ,以模板类的方法提供.为了访问容器中的数据,可以使用由容器类输出的迭代器: 算法: 是用来操作容器 ...