修改编译构建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();

}

}

如果你进阶的路上缺乏方向,可以加入我们的圈子和安卓开发者们一起学习交流!

  • Android进阶学习全套手册

    img

  • Android对标阿里P7学习视频

    img

  • BATJ大厂Android高频面试题

    img

最后,借用我最喜欢的乔布斯语录,作为本文的结尾:

人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。
你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。
最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
-8yz10z16-1715187114133)]

  • Android对标阿里P7学习视频

    [外链图片转存中…(img-fBdJHF5H-1715187114134)]

  • BATJ大厂Android高频面试题

    [外链图片转存中…(img-IVkj8SrU-1715187114134)]

最后,借用我最喜欢的乔布斯语录,作为本文的结尾:

人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。
你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。
最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值