Spring源码下载与编译

本文档详细介绍了如何在Windows环境下准备开发工具,从GitHub下载Spring源码,通过IDEA导入并预编译项目。重点讲述了如何配置阿里云镜像加速以及在IDEA中排除无法编译的模块。同时,提供了简单的测试案例,包括创建HelloService接口及其实现,通过spring-context配置并调用。最后,给出了测试Main方法的代码,展示了如何通过XML配置文件获取并调用服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


环境准备

  • 安装jdk,idea,maven并做好相应配置
  • 操作系统:windows

Spring源码下载

Spring源码Github仓库地址为:https://github.com/spring-projects/spring-framework

首先,选择合适的RELEASE版本,然后下载ZIP包即可

软件版本介绍:

  • GA:General Availability,官方正式发布的稳定版本,同质的还有RELEASE,Stable,Final
  • RC:Release Candidate,发行候选版本,基本不再加入新的功能
  • Alpha:内部测试版本,bug较多,功能不全
  • Beta:公开测试版,比Alpha版本晚些,还会加功能,修bug
  • M:Milestone,开发期发行版本,边开发边发行

在这里插入图片描述

在这里插入图片描述

导入IDEA

import-into-idea

Within your locally cloned spring-framework working directory:

  1. Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
  2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
  3. When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
  4. Code away
  1. 在解压缩后的源码路径下执行预编译指令:./gradlew :spring-oxm:compileTestJava

在这里插入图片描述

  1. build.gradle 文件里配置阿里云镜像加速

在这里插入图片描述

buildscript {
	repositories {
		maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
		maven { url 'https://maven.aliyun.com/repository/jcenter' }
	}
	dependencies {
		classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
		classpath 'io.spring.asciidoctor:spring-asciidoctor-extensions:0.1.3.RELEASE'
	}
}

在这里插入图片描述

		repositories {
			maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
			maven { url 'https://maven.aliyun.com/repository/jcenter' }
			mavenCentral()
			maven { url "https://repo.spring.io/libs-spring-framework-build" }
		}
  1. 导入IntelliJ IDEA:File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle

在这里插入图片描述

在这里插入图片描述

  1. 排除 spring-aspects,aspects包在idea中不能完成编译,只能unload

在这里插入图片描述

在这里插入图片描述

测试

  1. new Module

在这里插入图片描述

在这里插入图片描述

  1. 引入spring-context项目

在这里插入图片描述

spring-context 包含了 spring-aopspring-beansspring-core 等 Spring核心

  1. 简单测试

接口:

package com.uestc;

public interface HelloService {
	void sayHello(String name);
}

实现:

package com.uestc.Impl;
import com.uestc.HelloService;

public class HelloServiceImpl implements HelloService {
	@Override
	public void sayHello(String name) {
		System.out.println("Hi~" + name);
	}
}

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="helloService" class="com.uestc.Impl.HelloServiceImpl"/>

</beans>

参考:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html

调用Main方法:

package com.uestc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		// xml文件绝对路径
		String xmlPath = "E:\\Java\\javaWorkSpace\\spring-framework-5.2.0.RELEASE\\spring-demo\\src\\main\\resources\\spring-config.xml";
		// 解析spring的配置文件
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		// 获取配置文件的 <bean id="helloService" .... />
		HelloService helloService = (HelloService)applicationContext.getBean("helloService");
		// 调用方法
		helloService.sayHello("China");
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xylitolz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值