# splendid-spring **Repository Path**: efairy520/splendid-spring ## Basic Information - **Project Name**: splendid-spring - **Description**: 手写Spring源码 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-13 - **Last Updated**: 2026-04-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Splendid Spring 从零手写Spring框架,实现原理 ## 项目简介 Splendid Spring 是一个从零开始手写 Spring 框架核心功能的教学项目。通过逐步构建一个简化版的 Spring 框架,帮助开发者深入理解 Spring 框架的内部原理和设计思想。 本项目涵盖了 Spring 框架的核心功能,包括 IoC 容器、AOP 面向切面编程、事件机制、注解驱动等关键技术的实现。 ## 技术架构 ### 模块划分 项目包含 17 个独立章节模块,逐步递进实现完整功能: - **Chapter 01-04**: 基础 IoC 容器构建 - BeanDefinition 定义 - BeanFactory bean工厂 - 属性配置注入 - bean引用 - **Chapter 05-06**: XML配置加载 - XmlBeanDefinitionReader - Resource 资源加载 - ClassPathXmlApplicationContext - **Chapter 07**: 生命周期管理 - BeanPostProcessor - InitializingBean - DisposableBean - 初始化/销毁方法 - **Chapter 08**: Aware接口 - BeanNameAware - BeanFactoryAware - ApplicationContextAware - **Chapter 09**: FactoryBean与作用域 - FactoryBean 接口 - singleton/prototype 作用域 - **Chapter 10**: 事件机制 - ApplicationEvent - ApplicationListener - ApplicationEventMulticaster - **Chapter 11-12**: AOP 面向切面编程 - JDK 动态代理 - CGLIB 代理 - AspectJ 表达式切点 - MethodInterceptor - **Chapter 13-14**: 注解驱动开发 - @Component 组件扫描 - @Autowired 自动装配 - @Value 属性占位符 - Scope 作用域 - **Chapter 15-16**: 高级特性 - AOP 自动代理 - 循环依赖处理 - **Chapter 17**: 多Advisor支持 - 链式拦截器 - 多个切面配置 ## 核心组件 ### BeanFactory 体系 ``` BeanFactory ├── SingletonBeanRegistry │ └── DefaultSingletonBeanRegistry ├── AbstractBeanFactory │ └── FactoryBeanRegistrySupport └── AbstractAutowireCapableBeanFactory └── DefaultListableBeanFactory ``` ### ApplicationContext 体系 ``` ConfigurableApplicationContext └── AbstractApplicationContext ├── AbstractRefreshableApplicationContext │ └── AbstractXmlApplicationContext │ └── ClassPathXmlApplicationContext └── AnnotationConfigApplicationContext ``` ### AOP 体系 ``` AopProxy ├── JdkDynamicAopProxy └── Cglib2AopProxy Advisor └── PointcutAdvisor └── AspectJExpressionPointcutAdvisor ``` ## 使用说明 ### 快速开始 ```bash # 克隆项目 git clone https://gitee.com/efairy520/splendid-spring.git # 进入目录 cd splendid-spring # 运行测试 mvn test ``` ### 运行特定章节测试 ```bash # 进入章节目录 cd splendid-spring-chapter-01 # 运行测试 mvn test ``` ## 功能演示 ### 基本Bean获取 ```java ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = (UserService) context.getBean("userService"); userService.queryUserInfo(); ``` ### AOP 代理 ```java AdvisedSupport advisedSupport = new AdvisedSupport(); advisedSupport.setTargetSource(new TargetSource(new UserService())); advisedSupport.setMethodInterceptor(new CatInterceptor()); advisedSupport.setMethodMatcher(new AspectJExpressionPointcut("execution(* *.queryUserInfo())")); AopProxy aopProxy = new JdkDynamicAopProxy(advisedSupport); IUserService proxy = (IUserService) aopProxy.getProxy(); ``` ## 项目结构 ``` splendid-spring/ ├── splendid-spring-chapter-01/ # 基础BeanFactory ├── splendid-spring-chapter-02/ # BeanDefinition ├── splendid-spring-chapter-03/ # 实例化策略 ├── splendid-spring-chapter-04/ # 属性注入 ├── splendid-spring-chapter-05/ # XML配置加载 ├── splendid-spring-chapter-06/ # ApplicationContext ├── splendid-spring-chapter-07/ # 生命周期 ├── splendid-spring-chapter-08/ # Aware接口 ├── splendid-spring-chapter-09/ # FactoryBean ├── splendid-spring-chapter-10/ # 事件机制 ├── splendid-spring-chapter-11/ # AOP基础 ├── splendid-spring-chapter-12/ # AOP自动代理 ├── splendid-spring-chapter-13/ # 组件扫描 ├── splendid-spring-chapter-14/ # 自动装配 ├── splendid-spring-chapter-15/ # AOP与Autowired └── splendid-spring-chapter-17/ # 多Advisor支持 ``` ## 技术亮点 1. **手写IoC容器** - 完全模拟 Spring IoC 容器实现原理 2. **多种实例化策略** - 反射+CGLIB 动态代理 3. **完整生命周期** - 初始化、销毁回调机制 4. **事件驱动** - 发布/订阅模式实现 5. **AOP切面编程** - 方法拦截器链式调用 6. **注解驱动** - @Component、@Autowired、@Value ## 依赖环境 - JDK 8+ - Maven 3.x+ ## 贡献指南 欢迎提交 Issue 和 Pull Request! ## 参考资料 - [Spring 源码](https://github.com/spring-projects/spring-framework) - [Spring 核心技术](https://docs.spring.io/spring-framework/docs/current/reference/) ## 开源协议 本项目仅供学习交流使用。