Spring Framework源码编译,开始Spring源码学习

简介: 本文是博主学习Spring源码的记录,希望对大家有所帮助。

🍀Spring Framework源码编译

博主本地java版本为 java version "11.0.10"

🍀拉取源码

选定存放路径直接只用git clone拉取最新源码

git clone https://github.com/spring-projects/spring-framework.git

目前最新RELEASE版本是5.2.18,编译前需要将tag切换至这个版本。
在这里插入图片描述

git checkout a1225f0

在这里插入图片描述

🍀修改仓库镜像地址,加快依赖下载速度

vim  build.gradle

进入后直接输入/repositories,搜索仓库镜像设置。
在这里插入图片描述
加下来添加阿里云的镜像地址。
回车后,按i进入编辑模式,输入以下代码。

// 阿里云
maven {url "https://maven.aliyun.com/nexus/content/groups/public/"} 
maven {url "https://maven.aliyun.com/nexus/content/repositories/jcenter"}

在这里插入图片描述
ESC后,输入:wq保存并退出。

🍀源码命令行编译测试

使用以下两条指令进行编译测试

./gradlew :spring-oxm:compileTestJava // 官方建议
./gradlew :spring-core:compileTestJava

在这里插入图片描述
在这里插入图片描述

🍀导入IDEA

idea版本
在这里插入图片描述
导入IDEA后会自动编译,会自动重新下载依赖。

☘️创建一个新的Module

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建Module成功。

☘️给Module加依赖

compile(project(":spring-context"))

在这里插入图片描述
添加依赖后,需要重新加载。

🍀使用ApplicationContext获取自定义的Bean

在这里插入图片描述

package com.ber.service;

/**
 * @author ber
 * @version 1.0
 * @date 21/11/9 13:12
 */
public interface MsgService {
    String getMsg();
}
package com.ber.service.impl;

import com.ber.service.MsgService;
import org.springframework.stereotype.Service;

/**
 * @author ber
 * @version 1.0
 * @date 21/11/9 13:13
 */
@Service("msg")
public class MsgServiceImpl implements MsgService {
    @Override
    public String getMsg() {
        return "Hello Ber!";
    }
}
package com.ber;

import com.ber.service.MsgService;
import com.ber.service.impl.MsgServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.Arrays;

/**
 * @author ber
 * @version 1.0
 * @date 21/11/9 13:18
 */
@ComponentScan({"com.ber"})
public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        MsgService msgService = (MsgServiceImpl) context.getBean("msg");
        System.out.println(msgService.getMsg());
        System.out.println(context.getBeanDefinitionCount());
        Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);

        Application application = (Application) context.getBean("application");
        System.out.println(application.getMsg());
    }

    public String getMsg() {
        return "Hello Ber!!!";
    }
}

@Service注解是在spring-context中,前面依赖导入的作用就体现了。指定msgcomponent name

@ComponentScan注解也是在spring-context中,这里指定扫描包com.ber下的component names

在程序中通过AnnotationConfigApplicationContext来获取由Spring自动创建的msgapplication这两个Bean,并且调用其方法。
在这里插入图片描述

☘️运行测试

在这里插入图片描述
这里可以看出一共有6个Bean,我们定义了2个Bean,分别是msgapplication。通过AnnotationConfigApplicationContext获取Bean也成功获取到了其中的方法。

目录
相关文章
|
8天前
|
小程序 数据可视化 Java
Java+后端Spring boot 开发的全套UWB定位方案,0.1米高精度定位系统源码
UWB定位系统由硬件定位设备、定位引擎和应用软件组成。该定位系统应用软件支持PC端和移动端访问,并提供位置实时显示、历史轨迹回放、人员考勤、电子围栏、行为分析、智能巡检等功能。定位精度高达10cm,同时具备高动态、高容量、低功耗的优点。应用场景包括:隧道、化工、工厂、煤矿、工地、电厂、养老、展馆、整车、机房、机场等。
34 8
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的诗词学习系统的详细设计和实现
基于SpringBoot+Vue+uniapp的诗词学习系统的详细设计和实现
24 11
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的中医学习服务管理系统的详细设计和实现
基于SpringBoot+Vue的中医学习服务管理系统的详细设计和实现
25 13
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的在线学习过程管理系统软件的详细设计和实现
基于SpringBoot+Vue的在线学习过程管理系统软件的详细设计和实现
26 12
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的大学生国学自主学习平台的详细设计和实现
基于SpringBoot+Vue的大学生国学自主学习平台的详细设计和实现
26 10
|
2天前
|
XML Java API
IoC 之 Spring 统一资源加载策略【Spring源码】
IoC 之 Spring 统一资源加载策略【Spring源码】
11 2
|
2天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的线上学习资源智能推荐系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的线上学习资源智能推荐系统附带文章源码部署视频讲解等
14 5
|
2天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的驾校预约学习系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的驾校预约学习系统附带文章源码部署视频讲解等
5 0
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp微信小程序的在线学习系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的在线学习系统的详细设计和实现
7 0
|
3天前
|
监控 Java API
【监控】spring actuator源码速读
【监控】spring actuator源码速读
7 1