1. 基本配置:
jdk1.11(注意版本别太高会报错)、Maven3.6、idea、Spring5.2.20。
2 官网下载源码:
https://spring.io/projects/spring-framework#learn
目前选中的是5.2.20版。下载通过3进入Github下载对应版本。
3. 编译
从build from source查看官方编译步骤
以下是官网推荐编译步骤
3.1. 编译步骤1
通过idea编译方式编译源码:
前提条件,更改项目根目录build.gradle文件内的下载地址切换为阿里云(如下图位置)。然后执行
./gradlew :spring-oxm:compileTestJava
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter/'}
3.2. 编译步骤2
以gradle形式导入idea并移除spring-aspects
spring-aspects右键-> (步骤如下图所示)
4. 源码尝试使用:
在项目路根目录创建一个Gradle的项目(不可创建MAVEN,不能和Gradle共存,会报错)
4.1. 项目总览及创建顺序内容
4.2. 项目具体内容及代码
根据上图顺序创建文件:
WelcomeService.java
public interface WelcomeService {
public String sayHello(String name);
}
WelcomeServiceImpl.java
public class WelcomeServiceImpl implements WelcomeService {
@Override
public String sayHello(String name) {
System.out.println("welcome"+name);
return "success";
}
}
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="welcomeService" class="com.lyd.service.impl.WelcomeServiceImpl"></bean>
</beans>
build.gradle
dependencies {
//将spring-context相关依赖引入到本项目来
compile(project(":spring-context"))
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
Entrence.java
public class Entrence {
public static void main(String[] args) {
System.out.println("Hello");
// 获取bean文件绝对路径
String xmlPath = "//home/lyd/javaHub/spring-framework-5.2.0.RELEASE/spring-framework-5.2.0.RELEASE/SpringDemo/src/main/resources/spring/spring-config.xml";
//读取xml文件
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
//获取bean容器
WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
//通过bean执行方法
welcomeService.sayHello("lyd");
}
}