编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
博主的Java版本 :
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5CO5UTO2U2MkFmM5IDZ3ImNzYzX3UjMxgTM5AzLcFTMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
clone
源码
clone
存放目录自己指定。
git clone https://github.com/spring-projects/spring-framework.git
clone
完成后,需要切换到想要编译的
Spring Framework
版本,
5.2.17
是目前最新的
RELEASE
版本。
git checkout e45e77f
修改镜像仓库配置(可选)
还需要修改一些配置,比如修改镜像仓库,可以加快下载依赖的速度:
vim build.gradle
/repositories
搜索镜像仓库配置。
添加阿里云的镜像仓库:
maven {url 'https://maven.aliyun.com/nexus/content/groups/public/'}
maven {url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
:wq
保存、退出。
修改 gradle
的配置(可选)
gradle
下载
gradle-5.6.4-bin.zip
,版本信息在
gradle/wrapper/gradle-wrapper.properties
配置文件中,嫌麻烦就省略这一步(反正程序会自动下载)。
vim gradle/wrapper/gradle-wrapper.properties
配置
gradle-5.6.4-bin.zip
的路径。
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
# distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
distributionUrl=file\:///F\:/tools/gradle/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
预编译
./gradlew :spring-oxm:compileTestJava
IDEA导入源码
IDEA版本:
导入源码,IDEA会自动
build
。
创建一个新的
Module application
。
给新
Module
加依赖。
compile(project(":spring-context"))
依赖等下需要用到。
在源码中使用 ApplicationContext 获取定义的Bean
写一个
service
。
IMessageService
接口。
package com.kaven.service;
public interface IMessageService {
String getMessage();
}
IMessageService
接口实现类
MessageServiceImpl
。
package com.kaven.service.impl;
import com.kaven.service.IMessageService;
import org.springframework.stereotype.Service;
@Service("message")
public class MessageServiceImpl implements IMessageService {
@Override
public String getMessage() {
return "Hello Kaven";
}
}
@Service
注解就是在
spring-context
子项目中,所以需要依赖它,指定
message
为
component name
。
package com.kaven;
import com.kaven.service.IMessageService;
import com.kaven.service.impl.MessageServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import java.util.Arrays;
@ComponentScan({"com.kaven"})
public class Application {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
IMessageService service = (MessageServiceImpl) context.getBean("message");
System.out.println(service.getMessage());
System.out.println(context.getBeanDefinitionCount());
Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
((Application)context.getBean("application")).print("Hello Kaven");
}
public void print(String str){
System.out.println(str);
}
}
@ComponentScan
注解也是在
spring-context
子项目中,指定扫描包
com.kaven
下的
component
,并且在程序中通过
AnnotationConfigApplicationContext
来获取由
Spring
自动创建的
message
和
application
这两个Bean,并且调用其方法。
可见目前有
6
个Bean,
application
、
message
是我们自己定义的。
补充
官方给了以下步骤:
第三步我这里没有按照官方给的步骤来,我发现项目Build后,
spring-aspects
子项目下的代码也都是正常的,并没有爆红,如果有爆红就
unload
这个
spring-aspects
子项目。