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,用来标注组件:@ ...
随机推荐
- 基于第三方vuejs库组件做适配性个性开发
相信大家在使用vuejs时候会用到很多的第三方库,能够找到适合自己的库并且加以使用可以大大加快进度,减少bug.但是很多时候会出现这样一个尴尬的境地: 基线的第三方组件并不能很好地满足我们自己地需求, ...
- Vue.js项目实战-打造线上商城
首先上一下完成后的效果: 首页: 商品详情页: 购物车页(其实还有个订单页,只是和购物车页基本类似,所以就不截图啦): 开始项目: 由于涉及的是前后端分离,所以我们的后台数据就模拟存储于浏览器端(co ...
- iOS编程
一.语法 1. performSelector 2.
- webview学习
Android中WebView使用6,js调java实现播放视频 https://blog.csdn.net/zhaihaohao1/article/details/77993890 android ...
- YUM方式安装MYSQL5.7
首先mysql官网下载yum源https://dev.mysql.com/downloads/repo/yum/ 然后将下载好的rpm文件安装并校验 [root@elegant-snap-3 /sta ...
- 生成Uuid工具类
package com.freeter.util; import java.util.UUID; /** * @author liuqi * **/public class Uuid{ public ...
- Python字符编码和转码
一:Python2 python2默认编码格式是ascii码,解释器解释代码时会将代码以及代码中的字符串等转换成ascii码再执行.这样会导致字符串输出或传输时,与当前环境编码格式不同的话会显示乱码. ...
- php观察者模式(observer pattern)
... <?php /* The observer pattern implements a one-too-many dependency between objects. The objec ...
- windows命令行模式打开目录
cmd命令行里面,打开当前目录方式如下: explorer .
- 接口测试中,数据驱动时,参数各类型,空或None的处理
天天说接口测试,天天说数据驱动,但网上的各种教程太烂,遇到实际情况就傻眼了. 来来来,我们看一个例子 假设,有下面这样一个接口,获取用户信息,可以带的参数如下: 用户名(uname) str(),非必 ...