spring源码之--启动入口

前面的文章搭建过spring源码,这里暂时不做展开讲解

spring源码搭建-略

1、那么spring的源码入口从哪查看呢?springboot的源码是如何启动spring的源码呢?追着这个疑问总结了一下如下:

 在spring源码中直接添加一个模块,然后进行启动测试,编写启动入口

/**
 * @author mc
 * @date 2021/11/29 9:50
 * @description 冒烟测试使用
 */
public class Main {
	public static void main(String[] args) {

		//方法1-0
		AnnotationConfigApplicationContext context0 = new AnnotationConfigApplicationContext();
		context0.scan("service");
		context0.refresh();
		ServiceA aService0 = context0.getBean(ServiceA.class);
		aService0.printA();

		//方法1-1
//		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("service");
//		ServiceA aService = context.getBean(ServiceA.class);
//		aService.printA();



		//方法2
//		ApplicationContext context2 = new ClassPathXmlApplicationContext("applicationContext.xml");
//		context2.getBean("hello");

//		//方法3
//		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//		Object helloObj = beanFactory.getBean("hello");

	}
}

然后根据启动方法最终都会找到AnnotationConfigApplicationContext类然后查看到核心方refresh()

不做展开讲解

2、那么springboot的项目调用spring的方法在哪呢?

查看源码会看到这个方法

2.1、但是调用的是spring的哪个类呢?context是哪个呢?

根据追踪发现context可以有三个如下

 然后这三个和spring的关系图如下

 启动发现使用的是NONE,也就是说,使用的是spring启动的方法1的方式。

springboot启动也是使用的new AnnotationConfigApplicationContext的方式

猜你喜欢

转载自blog.csdn.net/sinat_38259539/article/details/132103432