修改编译构建spring-framework源码

  1. 操作系统:win10 64;

  2. JDK:1.8.0_144;

  3. Maven:3.5.0;

  4. IntelliJ IDEA:2018.1.5(Ultimate Edition);

全文概要

本次实战所有步骤如下:

  1. 下载spring-framwork源码,用IDEA打开此工程;

  2. 修改spring-framework中的类,添加代码;

  3. 编译构建spring-framework,添加到本地maven仓库;

  4. 基于maven创建一个新的demo,使用本地仓库的spring-framework;

  5. 执行demo工程验证修改的代码已经生效;

接下来开始实战吧;

下载spring-framework源码,用IDEA打开此工程

  1. 打开spring-framework在GitHub上的release列表,地址是:https://github.com/spring-projects/spring-framework/releases,下载4.1.8.RELEASE版本,如下图红框所示:

  2. 解压后,用Intellij IDEA以Gradle工程的形式导入,Gradle版本记得使用2.14.1,如下图:

修改spring-framework中的类

本次修改的类是PropertyPlaceholderHelper.java,这个类用来替换字符串中的占位符,本次修改源码的目的如下:

  1. 将替换前后的字符串打印出来用于对比;

  2. 将调用堆栈打印出来,这样我们在学习spring源码时,可以知道处理占位符的时机和位置;

对PropertyPlaceholderHelper.java的具体修改如下所示:

  1. 新增一个private方法,用于打印当前堆栈位置:

private void printTrack(String prefix){

StackTraceElement[] st = Thread.currentThread().getStackTrace();

if(st==null){

logger.info(“invalid stack”);

return;

}

StringBuffer sbf =new StringBuffer();

for(StackTraceElement e:st){

if(sbf.length()>0){

sbf.append(" <- ");

sbf.append(System.getProperty(“line.separator”));

}

sbf.append(java.text.MessageFormat.format(“{0}.{1}() {2}”

,e.getClassName()

,e.getMethodName()

,e.getLineNumber()));

}

logger.info(prefix + “\n” + sbf.toString());

}

  1. 找到方法parseStringValue,注意是protected类型的那个,在该方法的起始位置添加如下两行代码:

protected String parseStringValue(

String strVal, PlaceholderResolver placeholderResolver, Set visitedPlaceholders) {

printTrack(“start parseStringValue”);

logger.info(“before parse : [” + strVal + “]”);

上述代码先是将parseStringValue被调用的堆栈打印出来,接着将入参strVal打印出来;3. 在上一步中的parseStringValue方法的结束位置,在return之前增加一行代码,将处理后的字符串打印出来,如下:

logger.info(“after parse : [” + result + “]”);

return result.toString();

}

  1. 为了避免当前电脑的其他项目用到我们构建的spring版本,我们把版本号改掉,打开gradle.properties文件,修改版本号为"4.1.88.RELEASE",如下:

version=4.1.88.RELEASE

编译构建spring-framework

  1. 用命令build -x test编译和构建工程,操作如下图所示:

  2. 等待构建完成之后,用命令install将构建的jar部署到本地maven仓库中,如下图:

  3. 去本地maven仓库看看,果然已经有文件了,如下图:

创建demo,使用本地仓库的spring-framework

基于maven创建一个java应用,以下是步骤:

  1. pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

com.bolingcavalry

springcoredemo

1.0-SNAPSHOT

org.springframework

spring-core

4.1.88.RELEASE

org.springframework

spring-context

4.1.88.RELEASE

  1. 新增文件:src/main/resources/applicationContext.xml,用于配置bean,内容如下:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=“http://www.springframework.org/schema/p”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

  1. 创建一个类:com.bolingcavalry.bean.Simple.java:

public class Simple {

public void execute() {

System.out.println(“Simple execute method”);

}

}

  1. 创建启动类com.bolingcavalry.DemoApplication.java:

public class DemoApplication {

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“${CONFIG_PATH}”);

Simple bean = context.getBean(Simple.class);

bean.execute();

context.close();

}

}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
mg-qIDVne4q-1715883373641)]

[外链图片转存中…(img-2AmBKR2x-1715883373644)]

[外链图片转存中…(img-HBYaW7ri-1715883373646)]

[外链图片转存中…(img-WutFsyxi-1715883373648)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值