天天看点

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

博主的Java版本 :

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

​clone​

​源码

存放目录自己指定。

git clone https://github.com/spring-projects/spring-framework.git      
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

​clone​

​​完成后,需要切换到想要编译的​

​Spring Framework​

​​版本,​

​5.2.17​

​​是目前最新的​

​RELEASE​

​版本。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
git checkout e45e77f      
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

修改镜像仓库配置(可选)

还需要修改一些配置,比如修改镜像仓库,可以加快下载依赖的速度:

vim  build.gradle      

​/repositories​

​搜索镜像仓库配置。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

添加阿里云的镜像仓库:

maven {url 'https://maven.aliyun.com/nexus/content/groups/public/'} 
maven {url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}      

​:wq​

​保存、退出。

修改​

​gradle​

​的配置(可选)

下载​

​gradle-5.6.4-bin.zip​

​​,版本信息在​

​gradle/wrapper/gradle-wrapper.properties​

​配置文件中,嫌麻烦就省略这一步(反正程序会自动下载)。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
vim gradle/wrapper/gradle-wrapper.properties      

配置​

​gradle-5.6.4-bin.zip​

​的路径。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
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      
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

IDEA导入源码

IDEA版本:

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

导入源码,IDEA会自动​

​build​

​。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

创建一个新的​

​Module application​

​。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

给新​

​Module​

​加依赖。

compile(project(":spring-context"))      
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

依赖等下需要用到。

在源码中使用 ApplicationContext 获取定义的Bean

写一个​

​service​

​。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

​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​

​。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
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,并且调用其方法。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

可见目前有​

​6​

​​个Bean,​

​application​

​​、​

​message​

​是我们自己定义的。

补充

官方给了以下步骤:

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean

第三步我这里没有按照官方给的步骤来,我发现项目Build后,​

​spring-aspects​

​​子项目下的代码也都是正常的,并没有爆红,如果有爆红就​

​unload​

​​这个​

​spring-aspects​

​子项目。

编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean
编译 Spring Framework 5.2.17源码 & 在源码中使用 ApplicationContext 获取定义的Bean