在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的核心代码了!

相关推荐
二哈赛车手5 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
阿维的博客日记8 小时前
Nacos 为什么能让配置动态生效?(涉及 @RefreshScope 注解)
java·spring
周末也要写八哥12 小时前
Eclipse 2024全流程网盘下载与安装配置教程详解
java·ide·eclipse
tongluowan00714 小时前
@Autowired 和 @Resource 有什么区别?
java·spring·bean
ChampaignWolf16 小时前
在 Eclipse 中使用 Tabnine
java·ide·eclipse
海兰16 小时前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
北风toto17 小时前
为什么 IntelliJ IDEA Community 无法开发 Vue?——附解决方案
java·vue.js·intellij-idea
诗水人间17 小时前
VsCode 中使用Copilot调用Deepseek V4模型
ide·vscode·copilot
梦想家加一18 小时前
vscode为什么下载了汉化插件却不生效
ide·vscode·编辑器
夕除18 小时前
spring boot 9
java·mysql·spring