IDEA调试spring框架源码—图文详细

IDEA调试spring框架源码—图文详细

spring源码是java学习的核心,如果我们想要修改源码,例如加一些注释什么的,需要会编译源码,因此本文一步步教大家如何编译spring框架源码。

下载spring框架源码到本地

  • 由于github比较慢,使用码云Spring-Framework拉取代码

https://gitee.com/mirrors/Spring-Framework?_from=gitee_search

image-20220104202854795

  • 复制下载仓库链接

https://gitee.com/mirrors/Spring-Framework.git

image-20220104202915503

https://gitee.com/mirrors/Spring-Framework.git

  • 使用IDEA的git管理工具拉取代码

image-20220105221434122

  • 设置本地存储路径

image-20220105200414829

  • 打开拉取的代码

image-20220105200435687

  • 新打开一个窗口

image-20220105200451582

  • 可以看到spring框架源码已经拉取下来

image-20220105200507593

  • 默认可能会出现编译失败

image-20220105200638224

切换spring源码分支

  • 切换spring源码分支

image-20220105211857896

  • 点击展示更多分支

image-20220105211917606

  • 选择5.2.x版本

image-20220105211944126

修改仓库下载地址

​ 这一步比较重要,很多编译失败都是包下载不下来导致的。

  • 打开IDEA搜索框

image-20220105200720958

  • 搜索以下关键字
repositories {
  • 打开build.gradle

image-20220105201045224

  • 添加如下地址
maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}

image-20220105201105198

  • 重新编译

image-20220105222050308

  • 编译成功

image-20220105214322729

创建测试场景

  • spring-test中新建一个包

image-20220105215459344

  • 添加一个domain包

image-20220105215521230

  • 新建一个java类

image-20220105215603863

  • 类名为Person

image-20220105215622762

  • 不加入git版本控制管理

image-20220105215636676

  • 添加如下代码
package org.springframework.test.domain;

public class Person {
	private String name;
	public Integer age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}
}
  • 在resources下新建一个xml文件

image-20220105215710431

  • 命名为applicationContext.xml

image-20220105215759366

  • 不加入git版本控制

image-20220105215816026

  • 添加如下代码
<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
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 导入约束之后,即可在此处装配bean了 -->
    <bean id="person" class="org.springframework.test.domain.Person" >
        <property name="name" value="houww"></property>
        <property name="age" value="13"></property>
    </bean>
</beans>

image-20220105215843781

  • 在test包下新建一个类

image-20220105215932569

  • 随意命名为test

image-20220105215954695

  • 同样不加入git版本控制

image-20220105220011340

  • 添加如下代码
package org.springframework.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.domain.Person;

public class test {
   public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = context.getBean("person", Person.class);
        System.out.printf("person:"+person.toString());
   }
}

image-20220105220113458

  • 在第9行打个断点,右键以debug模式启动

image-20220105220142294

  • debug模式启动成功,点击继续运行

image-20220105220801638

  • 运行成功,输出消息

image-20220105220820897

总结

​ 经过一些操作,我们成功debug了spring源码,可以在源码的基础上自由的创作了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值