创建bean的注入规则
ImportBeanDefinitionRegistrar接口,实现该接口用来向容器中注入bean的注入规则,例如按照某一个注解注入,或者注入后进行proxy,等等操作,比如我们可以自定一个注解,创建一个scanner,用来扫描该注解,进行注入到容器中bean。
方法:
实现importbeandefinitionregister接口,registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry){
//annotationMetadata 可以通过attribute方法拿到容器中注解的信息
// 例如:可以拿到EnableAspectJAutoProxy 的注解信息,比如exposed,proxytargetclass 信息
AnnotationAttributes enableAspectJAutoProxy = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(EnableAspectJAutoProxy.class.getName(), false));
//创建一个默认的scanner,设置AnnotationTypeFilter ,然后scan,这样在包com.gxn.garden包下使用了@Mapper的类会被注入到容器中。
AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(Mapper.class);
scanner.addExcludeFilter(annotationTypeFilter);
scanner.scan(“com.gxn.garden”);
}
//声明一个注解。
@interface Mapper{
public String name();
}
springboot的run方法
- run里面,可以看到一个createapplicationcontent的方法:
然后对applicationcontentclass进行判断,如果是web,就加载一个AnnotationConfigServletWebServerApplicationContext,如果是REACTIVE就加载AnnotationConfigReactiveWebServerApplicationContext,,
然后对该class进行newinstance到容器中。 - 然后执行preparedcontent,执行postProcessApplicationContext,logstart 以及对容器的config进行配置,listener加载到容器中,准备初始化。
- 然后执行refresh
this.postProcessBeanFactory(beanFactory);
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
this.initMessageSource();
this.initApplicationEventMulticaster();
this.onRefresh();
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
4.invokeBeanFactoryPostProcessors 主要对bean的配置做处理,例如扫描configbean, 加载@value 等等
1万+

被折叠的 条评论
为什么被折叠?



