首页 > 编程语言 >Spring 源码阅读(一)环境搭建

Spring 源码阅读(一)环境搭建

时间:2024-04-18 23:00:27浏览次数:17  
标签:name spring age gradle maven 源码 Spring public 搭建

注意事项:

  1. 使用 2024-03-14 发布的 Spring 5.3.33 版本
  2. IDE 工具使用了 Intellij IDEA,同时为了简化不必要的内容没单独配置 Gradle 环境
  3. JDK 版本采用 Eclipse Temurin 1.8/11 均可

下载源码

下载 SpringFramework 源码,本次选择 5.3.33 版本,发布日期 2024-03-14,通过 Intellij IDEA 打开。

Gihub地址: https://github.com/spring-projects/spring-framework/releases/tag/v5.3.33

image

配置 Gradle 环境

由于国内下载 gradle 比较慢可以考虑使用腾讯云镜像源,在 /gradle/wrapper/gradle-wrapper.properties 中修改 distributionUrl

distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.5.1-bin.zip

同时仓库地址可修改为阿里云,在 build.gradle 中配置阿里云镜像

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/'}
        maven { url 'https://maven.aliyun.com/repository/jcenter/'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/'}
        mavenLocal()
        mavenCentral()
    }
}

在 Gradle 先 Reload All Gradle Projects,再执行 build 命令进行编译。
image

期间碰到报错信息,报错详情:

spring-core\src\main\java\org\springframework\core\CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {

解决方法:在 org.springframework.core.CoroutinesUtils#invokeSuspendingFunction 加上 @SuppressWarnings("deprecation")

若控制台输出中文乱码,Intellij IDEA 中设置 vm.properties

-Dfile.encoding=UTF-8

image

新建测试 Module

通过 gradle 新建一个 module,取名为 spring-research

image

module 新增完成后会自动在 settings.gradle 中引用 spring-research 模块,若没有自动引入可以手动加入

include 'spring-research'

在 spring-research 中的 build.gradle 中引入 spring-context

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    // 引入 spring-context 依赖
    api(project(":spring-context"))
}

可选项:此外由于 Spring 源码工程配置了 checkStyle,在做测试类的时候有些方法不能够满足 Spring 的规范要求(由于使用 Intellij IDEA 自动生成或者格式化的代码不满足要求,比如 tab indent,实体类 this 指向,包括 import 隔行等规则),可以通过在 src/checkstyle/checkstyle.xml 中添加过滤规则:

<module name="BeforeExecutionExclusionFileFilter">  
    <property name="fileNamePattern" value="^.*\\spring\-research\\.*$"/>  
</module>

image

新建实体类 Person

package io.github.linweiwang.bean;

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

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

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

	@Override
	public String toString() {
		return "Person{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}
}

在 resources.properties 中新建 spring-config.xml

<?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="person" class="io.github.linweiwang.bean.Person">
		<property name="name" value="王"/>
		<property name="age" value="18"/>
	</bean>
</beans>

在 Main 中调用 SpringContext 获取 Bean 的实例

package io.github.linweiwang;

import io.github.linweiwang.bean.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		Person person = context.getBean(Person.class);
		System.out.println(person);
	}
}

image

运行成功即环境搭建成功!

标签:name,spring,age,gradle,maven,源码,Spring,public,搭建
From: https://www.cnblogs.com/linweiwang/p/18144717

相关文章

  • Spring 中的发布-订阅模式
    发布订阅模式是怎样的?现在市面上流行的很多消息中间件就是采用的该种模式,这种模式在实际业务中将事件发布者(Publisher)与事件订阅者(Subscriber)通过额外的事件通道(EventChannel)来解耦,其基本原理与先前提到的观察者模式有些许类似,但发布订阅模式额外存在了EventChannel的......
  • hyperf windows使用docker搭建开发环境
    2024年4月13日23:44:16首先安装好docker注意:powershell是不支持命令换行符的dockerrun--namehyperf-vD:/code:/data-w/data-p9501:9501-it--privileged-uroothyperf/hyperf:8.1-alpine-v3.18-swoole或者使用最新版本dockerrun--namehyperf-vD:/code:/dat......
  • LibreCAD源码编译过程记录
    获取源码远程仓库https://github.com/LibreCAD/LibreCAD.git将源码从GitHub克隆到本地将仓库源码检出到提交节点:4b91d9b0f919be41f7e7568c87c5c67dfac189aa,这是LibreCADv2.2.0稳定版的提交节点,如果用更新的普通节点有可能编译出错(已踩坑)部署编译环境Qt5.9.7注意......
  • VuePress搭建文档网站
    VuePress官方文档:https://v2.vuepress.vuejs.org/zh/guide/getting-started.html以下是官方文档复制过来的步骤,加上了我的一些经验注释。1、依赖环境Node.jsv18.16.0+(我试了v14是运行不了的,必须这个版本及以上,建议安装nvm,方便切换node版本)包管理器,如pnpm、yarn、npm等(我安......
  • Spring Security快速入门
    使用SpringBoot+SpringSecurity整合,先定义一个空项目,主要为项目里面各个模块提供依赖,空项目依赖如下:<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version>......
  • SpringBoot 3.1.10 整合Mybatis-plus(快速学会)
    一、简介1、什么是Mybatis-plus?Mybatis-Plus(简称MP)是一个Mybatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。官网:https://baomidou.com/2、Mybatis-plus特性无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑损耗小:启动......
  • springcloud alibaba gateway网关鉴权
    登录鉴权:在gateway网关中实现全局过滤器GlobalFilter以及拦截器的顺序Ordered,在nacos中配置好需要放行的路径(如登录/login),获取请求头中的用户id,组装reids的key,来redis中存放的value,即token,再获取请求头中的token来跟redis中的value值进行比对,一致则放行,否则抛出异常。核心代码如......
  • SpringCloud(七.4)ES(elasticsearch)-- DSL查询语法
    DSL查询语法 1、查询所有以下是简写和全写 总结:  2、全文检索查询(match)全文检索查询,会对用户输入内容分词,常用于搜索框搜索: 回顾在 SpringCloud(七.3)ES(elasticsearch)--RestClient操作 中创建索引时添加的all字段,以及字段拷贝copy_to。这里all字段就派上了用......
  • Spring boot中使用groovy
    groovy是当做一个脚本来用的,也可以从数据库加载代码做一些动态数据处理。搭建一个springboot环境,pom.xml中编辑器和jar包,springboot是2.1.6.RELEASE<dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-all</artifactId><version>2.4.7</vers......
  • vscode+gdbserver远程调试ARM环境搭建
     一、编译gdbserver1.下载gdb  http://ftp.gnu.org/gnu/gdb/2.解压缩  tarxfgdb-8.0.tar.xz3.交叉编译  cdgdb/gdbserver/ ./configure--host=arm-none-linux-gnueabihf--target=arm-none-linux-gnueabihf--program-prefix=arm-none-linux-gnueabihf-......