阅读Spring源码第一步:源码编译与创建调试入口

本文阅读 3 分钟
首页 代码,Java 正文

  Spring开源框架经过很长时间的发展,各个模块均已成熟,一个常识就是一个可靠,可扩展的高性能框架,它的代码行数是相当可观的,我用static插件简略测算了一下,Spring的源码有100多万行,可以想象其中的调用逻辑是相当复杂的,所以将Spring源码下载到本地再编译的话,我们就可以通过IDE的debug来来到抽丝剥茧分析源码的目的,并且我们可以很方便的使用idea来查看调用栈,方法的调用关系也就比较明了了。

下载Spring源码: https://github.com/spring-projects/spring-framework/tree/v5.2.0.RELEASE

Spring是通过gradle构建的: https://www.zhihu.com/question/30432152

  gradle可以在不安装的情况下提供一个一键运行的脚本,打开Spring源码的根目录,找到build.gradle文件,添加阿里云镜像:

repositories {
        maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }

img img

将Spring源码导入到idea中: https://mp.weixin.qq.com/s/FIjGS13J10DF9_bqohYVjQ

导入完毕后要将spring-aspects排除出去,右键,load/unload modules,将其unload再重新build

看源码之前要先读一下spring的文档: https://docs.spring.io/spring-framework/docs/current/reference/html/

spring源码下载到本地后,为了调试我们还要创建一个程序入口。

创建一个新模块spring-demo,目的是引用上面那些模块的服务,作为后面调试的入口。

新建service接口:

package com.wjw.service;

public interface WelcomeService { 
   String sayHello(String name);
}

具体实现类实现一下:

package com.wjw.service.impl;

import com.wjw.service.WelcomeService;

public class WelcomeServiceImpl implements WelcomeService { 

   @Override
   public String sayHello(String name) { 
      System.out.println("欢迎你:" + name);
      return "success";
   }
}

创建resources/spring/spring-config.xml生成一个bean:

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
   
   <bean id="welcomeService" class="com.wjw.service.impl.WelcomeServiceImpl"/>
</beans>

创建程序主入口,读取xml文件,获取bean并执行方法:

package com.wjw;

import com.wjw.service.WelcomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Entrance { 
   public static void main(String[] args) { 
      System.out.println("Hello World!");
      String xmlPath = "F:\\Java\\spring-framework-5.2.0.RELEASE\\springdemo\\src\\main\\resources\\spring\\spring-config.xml";
      ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
      WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
      welcomeService.sayHello("强大的spring框架");
   }
}

此外还可以使用另一种注释的方式,来保证启动时将WelcomeServiceImpl的实例加载到容器里: img img 打印结果: img

最后再提一句官网对软件版本的命名规则:

  • GA:General Availability,官方正式发布的稳定版本
  • 同质的还有RELEASE,Stable,Final
  • RC:Release Candidate,发行候选版本,基本不再加入新的功能
  • Alpha:内部测试版本,bug较多,功能不全
  • Beta:公开测试版,比Alpha版本晚些,还会加功能,修bugumuoamn
  • M:Milestone,开发期发行版本,边开发边发行
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://wangjiawei.blog.csdn.net/article/details/115314867
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复