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,用来标注组件:@ ...
随机推荐
- python 双层for循环,在第二层的for循环中的else中的continue,会退出到第一层for循环继续执行
for a in [1,2,3,4,5]: for b in [1,2,3]: if a == b: print("a = b = %s" % a) break # 退出本次for ...
- CSP复赛day2模拟题
没错,我又爆零了.....先让我自闭一分钟.....so 当你忘记努力的时候,现实会用一记响亮的耳光告诉你东西南北在哪. 好了,现在重归正题: 全国信息学奥林匹克联赛(NOIP2014) 复赛模拟题 ...
- iOS - 逆向调试自用工具(reveal 14 Hopper Go2Shell ifunboxmac MachOView Alfred Go2Shell iTerm)
研究了挺长一段时间的逆向感觉没啥可弄的了,再深处对我也用处不大,渐渐兴趣有些掉头了.最近有问我要工具的朋友,分享一些常用工具给用到的朋友. 用法自己百度吧,这里不再赘述. 图例(希望看到你想要的): ...
- uni-app常用 HTML5+APP 设置
1.锁定屏幕方向 锁定屏幕方向后屏幕只能按锁定的屏幕方向显示,关闭当前页面后仍然有效. 可再次调用此方法修改屏幕锁定方向或调用 unlockOrientation() 方法恢复到应用的默认值. 锁定屏 ...
- 打包工具webpack和热加载深入学习
本次小编呢,为大家带来一篇深入了解打包工具 webpack. 我们今天使用的是 webpack3.8.1版本的,我们学习使用 3.8.1更稳定些,并学习自己如何配置文件,最新版本不需要自己配置文件,但 ...
- python-job
- ssh实现无密码登陆
参考教程:https://linux.cn/article-5444-1.html 1.假设你有一台主机A(ip:111.111.111.111),用户名为server123,想无密码登陆到主机B(1 ...
- php观察者模式(observer pattern)
... <?php /* The observer pattern implements a one-too-many dependency between objects. The objec ...
- 第09节-使用BTStack编写蓝牙程序
Bluetooth LE Exploer(微软提供的)可以读取BLE蓝牙设备,可以读写它的值.本篇博客将使用BTStack写出一个精简版的Bluetooth LE Exploer. 涉及文件: bts ...
- 使用WIFI网卡 wpa_supplicant
在上篇文章中,编译了应用程序iw,它使得我们的开发板可以通过usb wifi网卡连接到无线热点,为了方便实验,我们采用了手机设置了无线热点.对手机的热点有4中安全方式:无WEPWPAWPA2使用iw工 ...