要学习Spring 源码,一个是从Spring GitHub 上去down源码,然后倒入IDEA编译,但这种方法费时费力,如果你不需要对Spring 源码进行修改后,再编译的话,直接搭建一个Spring Demo 的Maven项目,引入Spring 对应依赖,下载源码包,通过Demo来学习。
本专栏就通过该Demo来学习Spring 源码,并将相关内容记录到该专栏中。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.31</version>
</dependency>
</dependencies>
</project>
这里引入了spring-context
依赖,做为最基本的spring依赖,因为其包含了主要的Spring特性spring-beans、spring-aop、spring-context、spring-core、spring-expression、spring-jcl
。
Test
Bean.java
package com.spring.test.demo.bean;
import org.springframework.stereotype.Component;
@Component
public class Bean {
public void print() {
System.out.println("Spring Test");
}
}
SpringTest.java
package com.spring.test.demo;
import com.spring.test.demo.bean.Bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(Bean.class);
applicationContext.refresh();
Bean bean = applicationContext.getBean(Bean.class);
bean.print();
}
}