搭建一个简单的Spring Demo

要学习Spring 源码,一个是从Spring GitHub 上去down源码,然后倒入IDEA编译,但这种方法费时费力,如果你不需要对Spring 源码进行修改后,再编译的话,直接搭建一个Spring Demo 的Maven项目,引入Spring 对应依赖,下载源码包,通过Demo来学习。

本专栏就通过该Demo来学习Spring 源码,并将相关内容记录到该专栏中。

Spring Demo项目地址:https://github.com/jujunchen/Spring-Demo

pom.xml

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

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

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();
    }
}

测试结果

Spring 模块

进度

模块 描述
spring-aop 要使用AOP相关的功能(事务、切面)需要包含该模块
spring-aspects 包含AspectJ AOP库进行高级集成的所有类
spring-beans 包含对Spring bean进行操作的类
spring-beans-groovy 对Spring bean进行操作的Groovy类
spring-context 包含Spring Core提供的许多扩展类,如ApplicationContext
spring-context-indexer 包含一个索引器实现, 它提供对META-INF/spring.components 中定义的候选项的访问功能,但核心类CandidateComponentsIndex 并不能再外部使用
spring-context-support 该模块是对spring-context模块的进一步扩展,在用户界面方面,有一些用于支持邮件并与模块引擎集成的类,还包括与各种任务执行和调度库(CommonJ和Quartz)的集成
spring-core 主要模块,其他Spring模块都会依赖该模块
spring-expression 包含SpEL表达式的支持类
spring-instrument 包含用于JVM启动的Spring工具代理,如果在Spring应用程序中使用AspectJ实现加载织入,那么该模块是必需的
spring-jcl 日志框架
spring-jdbc 包含所有的JDBC支持类
spring-jms 所有JMS支持类
spring-messaging 提供消息传递的基础结构和协议
spring-orm 扩展了Spring的标准JDBC功能集,支持流行的ORM工具,包含Hibernate、JDO、JPA和数据映射器IBATIS。该JAR文件中的许多类都依赖于spring-jdbc JAR文件中所包含的类,因此也需要把它包含在程序中
spring-oxm 为Object/XML映射OXM提供支持,用于抽象XML编组和解组以及支持Castor、JAXB、XMLBeans和XStream等常用工具的类都包含在此模块中
spring-test Spring提供的帮助测试程序的包
spring-tx 提供支持Spring事务的所有类
spring-web 包含Web程序中使用的所需核心类
spring-web-reactive 响应式模型的核心接口和类
spring-webmvc Spring自己的MVC框架
spring-websocket Spring对WebSocket的支持类

作者其他要推荐的文章,欢迎来学习:

基于Spring Boot 3.1.0 系列文章

  1. Spring Boot 源码阅读初始化环境搭建
  2. Spring Boot 框架整体启动流程详解
  3. Spring Boot 系统初始化器详解
  4. Spring Boot 监听器详解
  5. Spring Boot banner详解
  6. Spring Boot 属性配置解析
  7. Spring Boot 属性加载原理解析
  8. Spring Boot 异常报告器解析
  9. Spring Boot 3.x 自动配置详解
相关推荐
阿里云云原生23 分钟前
MCP云托管最优解,揭秘国内最大MCP中文社区背后的运行时
云原生
数字化综合解决方案提供商1 小时前
云原生时代的双轮驱动
云原生
小马爱打代码1 小时前
云原生 - Service Mesh
云原生·service_mesh
Chase_______2 小时前
Java后端开发——分层解耦详解
java·开发语言·spring·web
weisian1512 小时前
云原生--核心组件-容器篇-2-认识下Docker(三大核心之镜像,容器,仓库)
docker·云原生·容器
ApeAssistant3 小时前
Spring + 设计模式 (十八) 行为型 - 责任链模式
spring·设计模式
陈奕昆3 小时前
6.1腾讯技术岗2025面试趋势前瞻:大模型、云原生与安全隐私新动向
算法·安全·云原生·面试·腾讯
陈平安Java and C3 小时前
OpenFeign和Gateway
spring
写bug写bug4 小时前
Spring事务失效的9大场景,你踩过几个?
java·后端·spring
孔令飞4 小时前
Go 1.24 中的弱指针包 weak 使用介绍
人工智能·云原生·go