在idea中看spring源码

一、搭建环境

1.1 下载源码到本地

在github中找到spring-framework项目,或者这个地址(https://github.com/spring-projects/spring-framework)

然后把项目下载到本地目录,如图

1.2 然后用idea打开这个项目

1.3 然后等构建,我这里耗时比较久,要等构建成功

二、新增模块

2.1 新增模块,命名my-spring

2.2 在新建模块的build.gradle.kts文件添加依赖

复制代码
    implementation(project(":spring-context"))
	implementation(project(":spring-beans"))
	implementation(project(":spring-core"))
	implementation(project(":spring-aop"))

如图:

2.3 更新依赖,如图

2.4 创建实体类TestBean,如图

2.5 增加spring.xml文件

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="testBean" class="org.springframework.bean.TestBean">
		<property name="id" value="100"></property>
		<property name="name" value="Tom"></property>
	</bean>
</beans>

​​​​​如图

2.6 在main方法加载xml

XML 复制代码
package org.springframework;

import org.springframework.bean.TestBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		TestBean testBean = context.getBean("testBean", TestBean.class);
		System.out.println("Hello ," + testBean.getName());
	}
}

​如图

三、查看源码

3.1 debug ClassPathxmlapplicationContext类,启动程序,进入该类。

3.2 进入该类后,一直往下走,走到AbstractApplicationContext类的​​​​​​​refresh()方法,这块就是spring的核心代码了,如图

​​​​​​

到这步就可以慢慢看spring的核心代码了!

相关推荐
IT空门:门主3 小时前
Spring AI 2.1.0 更新了什么:从 2.0 升上来的完整梳理
人工智能·python·spring
Joy T1 天前
Spring Security 从 Acegi 到 AI Agent——JWT、角色鉴权与异步任务安全实战
人工智能·安全·spring·spring security·json web token·securitycontext·多次鉴权
 梦晓天明1 天前
1.Spring学习笔记_IOC入门案例
笔记·学习·spring
CoderYanger2 天前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
CoderYanger2 天前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互
软件技术NINI2 天前
基于SpringBootVue 的出租车拼车系统
vue.js·spring boot·spring·毕业设计·毕设
BD_Marathon2 天前
安装Jupyter notebook
ide·python·jupyter
Flynt2 天前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
CoderYanger2 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
CoderYanger3 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第三弹):统一数据返回格式+统一异常处理
java·数据库·spring boot·后端·spring·java-ee·mybatis