标签:调用 beanFactory Spring 初识 bean 源码 构造函数 方法 componentClasses
Spring源码
使用JavaConfig方式运行Spring
代码如下
public class SpringApplication {
public static void main(String[] args) {
//注解方式启动spring
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
System.out.println(context.getBean(TestService.class));
}
}
构造函数代码
构造函数只有三行代码
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
//调用无参构造函数
this();
//注册当前配置类
register(componentClasses);
//刷新容器
refresh();
}
分析前两行代码
无参构造函数
public AnnotationConfigApplicationContext() {
//创建bean定义读取器(这里会注册底层的后置处理器)
//会调用AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
this.reader = new AnnotatedBeanDefinitionReader(this);
//创建bean定义扫描器
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
创建bean定义读取器
初始化reader 时,会调用AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)方法,此方法会注册很多后置处理器
名称 | 后置处理器类型 | 作用 |
---|---|---|
ConfigurationClassPostProcessor | BeanDefinitionRegistryPostProcessor | 解析配置类@Configuration,解析@Component、@ComponentScan、@Import、@ImportResource等,注册到bean定义中 |
AutowiredAnnotationBeanPostProcessor | BeanPostProcessor SmartInstantiationAwareBeanPostProcessor MergedBeanDefinitionPostProcessor | 解析@Autowired、@Value、@Inject注解,完成自动注入 |
CommonAnnotationBeanPostProcessor | BeanPostProcessor MergedBeanDefinitionPostProcessor DestructionAwareBeanPostProcessor | 解析@Resource、@PostConstruct、@PreDestroy注解 |
EventListenerMethodProcessor | BeanFactoryPostProcessor SmartInitializingSingleton ApplicationContextAware | 解析@EventListener注解 |
DefaultEventListenerFactory | 无 | 通过解析@EventListener注解结果和此工厂类,创建ApplicationListener |
register(componentClasses)方法
调用此方法会将配置类注入到bean定义集合中
public void register(Class<?>... componentClasses) {
Assert.notEmpty(componentClasses, "At least one component class must be specified");
this.reader.register(componentClasses);
}
refresh方法,spring核心方法
序号 | 名称 | 作用 |
---|---|---|
1 | prepareRefresh() | 准备刷新上下文 设置启动时间,设置启动状态 初始化earlyApplicationListeners、earlyApplicationEvents |
2 | ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory() | 获取beanFactory |
3 | prepareBeanFactory(beanFactory) | 属性填充: 注入ApplicationContextAwareProcessor,后期执行扩展的Aware ignoreDependencyInterface设置忽略指定接口的实现类不进行创建 注册environment、systemProperties、systemEnvironment对象 |
4 | postProcessBeanFactory(beanFactory) | 空方法,子类可以实现此方法修改beanFactory(例如spring boot中就重写了) |
5 | invokeBeanFactoryPostProcessors(beanFactory) | 调用ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry方法解析配置和注解,将需要注册的bean加载到bean定义中 调用自定义的BeanDefinitionRegistryPostProcessor里的postProcessBeanDefinitionRegistry方法 调用BeanFactoryPostProcessor里的postProcessBeanFactory方法 |
6 | registerBeanPostProcessors(beanFactory) | 获取bean定义里实现BeanPostProcessor接口的类,将这些类创建对象放到容器中(getBean) |
7 | initMessageSource() | 国际化 |
8 | initApplicationEventMulticaster() | 创建事件多播器 |
9 | onRefresh() | 空方法,子类实现 |
10 | registerListeners() | 注册所有监听 获取bean定义中实现ApplicationListener接口的类,转成ApplicationListenerBean对象,添加到多播器中 触发早期事件 |
11 | finishBeanFactoryInitialization(beanFactory) | 注册所有bean |
12 | finishRefresh() | resourceCaches.clear(); 注册LifecycleProcessor 获取所有Lifecycle的实现,调用start方法 触发ContextRefreshedEvent |
13 | resetCommonCaches() | 清除缓存 |
详细代码流程如下:
标签:调用,beanFactory,Spring,初识,bean,源码,构造函数,方法,componentClasses 来源: https://blog.csdn.net/u010883443/article/details/111023701
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。