0
点赞
收藏
分享

微信扫一扫

Spring源码下载与编译

快乐小码农 2022-04-26 阅读 65
springjava

文章目录


环境准备

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

Spring源码下载

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

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

在这里插入图片描述

在这里插入图片描述

导入IDEA

  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项目

在这里插入图片描述

  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>

调用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");
	}
}

在这里插入图片描述

举报

相关推荐

0 条评论