springframework5.2.15源码编译导入Intelij教程

Published on 2024-03-20 10:53 in 分类: 博客 with 狂盗一枝梅
分类: 博客

一、源代码下载和配置文件修改

源代码下载地址:https://github.com/spring-projects/spring-framework

下载源代码之后,切换git分支到 v5.2.15.RELEASE 分支,然后修改以下配置文件

settings.gradle

pluginManagement新增阿里云仓库

maven { url "https://maven.aliyun.com/repository/public" }

新增后长这样

pluginManagement {
	repositories {
		maven { url "https://maven.aliyun.com/repository/public" }
		gradlePluginPortal()
		maven { url 'https://repo.spring.io/plugins-release' }
	}
}

build.gradle

repositories新增阿里云仓库

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

新增后长这样

repositories {
    maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
    maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}	
    mavenCentral()
    maven { url "https://repo.spring.io/libs-spring-framework-build" }
}

二、编译和导入Intelij

预编译spring-oxm

在项目根目录下执行以下命令 gradlew :spring-oxm:compileTestJava

image-20240320094507119

预编译spring-core

在项目根目录下执行以下命令gradlew :spring-core:compileTestJava

image-20240320094443405

导入Intelij

File->Open,直接打开,Intelij会自动识别出是Gradle项目并自动导包构建,第一次这个过程可能会很长,我用了将近半个小时才成功。

20240320091440

第二次就会快很多

image-20240320095139652

三、测试

源代码项目中有个项目名叫做: integration-tests,里面有很多单元测试类,找个运行下看看结果,一切正常就表示没问题了。

也可以新建一个module,自己做测试,build.gradle文件可以参考integration-tests模块,内容如下

plugins {
    id 'java'
}

group 'org.springframework'
version '5.2.15.RELEASE'

repositories {
    mavenCentral()
}

dependencies {
    testCompile(project(":spring-aop"))
    testCompile(project(":spring-beans"))
    testCompile(project(":spring-context"))
    testCompile(project(":spring-core"))
    testCompile(testFixtures(project(":spring-aop")))
    testCompile(testFixtures(project(":spring-beans")))
    testCompile(testFixtures(project(":spring-core")))
    testCompile(testFixtures(project(":spring-tx")))
    testCompile(project(":spring-expression"))
    testCompile(project(":spring-jdbc"))
    testCompile(project(":spring-orm"))
    testCompile(project(":spring-test"))
    testCompile(project(":spring-tx"))
    testCompile(project(":spring-web"))
    testCompile("org.projectlombok:lombok:1.18.18")
    testCompile("javax.inject:javax.inject")
    testCompile("javax.resource:javax.resource-api")
    testCompile("javax.servlet:javax.servlet-api")
    testCompile("org.aspectj:aspectjweaver")
    testCompile("org.hsqldb:hsqldb")
    testCompile("org.hibernate:hibernate-core")
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

test {
    useJUnitPlatform()
}

然后做个IOC容器测试

首先新建个UserInfo类

import lombok.Data;
import org.springframework.stereotype.Component;

@Data
@Component
public class UserInfo {
	private String name;
}

然后写个测试类

public class TestIoc {

	@Test
	public void test() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				"cn.kdyzm.ioc.po"
		);
		UserInfo student = context.getBean(UserInfo.class);
		System.out.println("获取到的实例对象:"+student);
		context.close();
	}
}

运行可能会提示:"Test events were not received",这时候,打开菜单File -> Settings ->Build,Execution, Deployment -> Build Tools -> Gradle,找到Run tests using ,默认是Gralde,改成Intelij

image-20240320105041167

然后重试运行,运行结果如下

image-20240320105120298

OK,测试成功,可以愉快的玩耍了~



END.


#spring
目录