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也成功获取到了其中的方法。

目录
相关文章
|
12天前
|
小程序 前端开发 Java
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
JavaDog Chat v1.0.0 是一款基于 SpringBoot、MybatisPlus 和 uniapp 的简易聊天软件,兼容 H5、小程序和 APP,提供丰富的注释和简洁代码,适合初学者。主要功能包括登录注册、消息发送、好友管理及群组交流。
33 0
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
|
14天前
|
人工智能 前端开发 Java
【实操】Spring Cloud Alibaba AI,阿里AI这不得玩一下(含前后端源码)
本文介绍了如何使用 **Spring Cloud Alibaba AI** 构建基于 Spring Boot 和 uni-app 的聊天机器人应用。主要内容包括:Spring Cloud Alibaba AI 的概念与功能,使用前的准备工作(如 JDK 17+、Spring Boot 3.0+ 及通义 API-KEY),详细实操步骤(涵盖前后端开发工具、组件选择、功能分析及关键代码示例)。最终展示了如何成功实现具备基本聊天功能的 AI 应用,帮助读者快速搭建智能聊天系统并探索更多高级功能。
136 2
【实操】Spring Cloud Alibaba AI,阿里AI这不得玩一下(含前后端源码)
|
22天前
|
缓存 前端开发 JavaScript
前后端分离 SpringBoot+Vue商城买卖系统通杀版本。大家可以参考学习一下
这篇文章介绍了一个使用SpringBoot+Vue开发的前后端分离商城系统,包括技术架构、开发环境、实现的功能以及项目截图,并展示了普通用户和商家端的功能界面。
前后端分离 SpringBoot+Vue商城买卖系统通杀版本。大家可以参考学习一下
|
2月前
|
Java 数据格式 微服务
2024最新首发,全网最全 Spring Boot 学习宝典(附思维导图)
📚 《滚雪球学Spring Boot》是由CSDN博主bug菌创作的全面Spring Boot教程。作者是全栈开发专家,在多个技术社区如CSDN、掘金、InfoQ、51CTO等担任博客专家,并拥有超过20万的全网粉丝。该教程分为入门篇和进阶篇,每篇包含详细的教学步骤,涵盖Spring Boot的基础和高级主题。
158 4
2024最新首发,全网最全 Spring Boot 学习宝典(附思维导图)
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的在线学习过程管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线学习过程管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线学习过程管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
2月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
78 11
|
2月前
|
安全 Java 数据库
三更草堂 Spring Security学习总结(思路整理)
Spring Security学习总结(思路整理)
|
20天前
|
设计模式 Java 程序员
学习 Spring 源码的意义是什么呢?
研究Spring源码能深化框架理解,提升代码分析与设计能力,助您掌握设计模式及最佳实践,增强解决问题的效率,促进职业生涯发展,并激发技术热情。选择稳定版本,从核心模块开始,结合实际项目并参与社区,让学习之旅既充实又具乐趣。
|
2月前
|
缓存 Java 程序员
spring IoC 源码
spring IoC 源码
47 3
|
30天前
|
安全 Java 网络安全
Spring Framework JDK >= 9 远程代码执行(CVE-2022-22965)
Spring Framework JDK >= 9 远程代码执行(CVE-2022-22965)