spring源码阅读(一)--环境搭建

记录一下自己对spring源码的阅读理解(一)

这是本人第一次记博客,平时都是记录在映像笔记中,发现印象不够深刻,模糊的始终是模糊的,打算用博客的方式鞭策自己,也希望一直能坚持下去,

Spring编译环境搭建

spring采用gradle进行编译的,和maven异曲同工,下面讲解一下spring源码阅读的环境搭建:

  1. 去github上搜索spring-framework进行对应版本下载,笔者下载的是5.2版本

https://github.com/spring-projects/spring-framework

  1. 下载gradle 下载地址

https://services.gradle.org/distributions/

  1. 配置gradle环境变量

GRADLE_HOME D:\gradle-6.8.3-bin\gradle-6.8.3
path %GRADLE_HOME%\bin

  1. 测试是否安装成功

gradle -version

  1. gradle文件讲解

build.gradle 放一些依赖配置,有点像maven的pom文件
gradle.properites 存放当前Spring的版本,可以对比下看看是不是5.0.x版本
gradlew.bat 放的是项目的构建脚本,不过在本章用不上,本章是使用idea去构建
import-into-idea.md 这个是spring导入idea的说明文件,可以根据里面的步骤去导入
gradle文件讲解

  1. 前置设置,否则无法正常编译

1.注释掉dokka和asciidoctor两个配置项
2.替换docs.gradle文件中的task schemaZip配置
3.在build.gradle文件配置阿里云镜像

task schemaZip(type: Zip) {
    group = "Distribution"
    baseName="spring-framework"
    classifier="schema"
    description = "Builds -${classifier} archive containing all " +
            "XSDs for deployment at https://springframework.org/schema."
    duplicatesStrategy 'exclude'
    moduleProjects.each { subproject ->
        def Properties schemas = new Properties();
        subproject.sourceSets.main.resources.find{
        it.path.endsWith("META-INF/spring.schemas")
        }?.withInputStream { schemas.load(it) }


        for (def key : schemas.keySet()) {
            def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
            assert shortName != key
            File xsdFile = module.sourceSets.main.resources.find {
                 it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
            }
            assert xsdFile != null
            into (shortName) {
                from xsdFile.path
            }
        }
    }
}
allprojects {
   repositories {
      maven { url 'https://maven.aliyun.com/repository/public/' }
      mavenLocal()
      mavenCentral()
   }
}
  1. 添加一个自己的模块,修改build.gradle
dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    compile(project(":spring-context")) //重点,导入本地spring-context
}

  1. 编写一个配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   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/context https://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="cn.sm.beans"></context:component-scan> //使用xml的方式装配bean
</beans>
  1. 编写测试类
		ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:spring.xml");
		System.out.println(ioc.getBean("car"));
  1. 注:第一次使用可能会存在乱码问题

idea设置 Settings–>Editor—>File Encodings—设置成utf-8
jvm参数设置 -Dfile.encoding=UTF-8
配置好重启下idea集合

在这里插入图片描述
在这里插入图片描述

  1. 测试是否能从容器中获取bean
    在这里插入图片描述

到此环境已经搭建完,有不对的地方欢迎指出

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值