# spring-2.0-ioc **Repository Path**: coolingme/spring-2.0-ioc ## Basic Information - **Project Name**: spring-2.0-ioc - **Description**: Spring手写实现IoC和DI - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-09 - **Last Updated**: 2024-09-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: Spring ## README ### 基于Spring-1.0 [https://gitee.com/coolingme/spring-1.0](https://gitee.com/coolingme/spring-1.0) ### Spring启动流程 > 从Servlet到Application 1. 调用`Servlet`的`init()`方法 - 创建`ApplicationContext` 2. 读取配置文件(`properties`,`yml`,`xml`) - `BeanDefinitionReader`读取配置文件,解析为`BeanDefinition` - 配置文件保存在内存中都变为`BeanDefinition` 3. 扫描相关的类 4. `IoC`容器初始化,并且实例化对象 - `ApplicationContext.getBean()`,完成对象实例化和依赖注入 - `BeanWrapper`保存了实例对象,由`getBaen`创建 5. `DI`注入 6. `HandlerMapping`映射 ### 相关解释 - `IoC`和`DI`属于`spring-core`和`spring-beans` - `ApplicationContext` 简单理解为工厂类 - `getBean()` 从`IoC`容器中获取实例 - 在调用`Servlet init()` 方法时,就要初始化`ApplicationContext` - `Spring`默认是单例,而且是延时加载的(Lazy),用的时候才创建 - `DI`是在初始化之后发生的 - `DI`是由`getBean()`触发的 - 调用`getBean()`会创建对象 - 立刻会发生DI ### 使用到的设计模式 - 享元模式 - 工厂模式 - 装饰器模式 ### 对象循环依赖 A{B b} B{A a} - 循环两次注入就行了 ### 使用Jetty启动 #### 访问路径 - [http://127.0.0.1:8080/demo/index](http://127.0.0.1:8080/demo/index) - [http://127.0.0.1:8080/demo/add?a=1&b=2](http://127.0.0.1:8080/demo/add?a=1&b=2) - [http://127.0.0.1:8080/demo/hello?name=joey](http://127.0.0.1:8080/demo/hello?name=joey) - [http://127.0.0.1:8080/demo/hello123?name=joey](http://127.0.0.1:8080/demo/hello123?name=joey) - [http://127.0.0.1:8080/demo/remove?id=1](http://127.0.0.1:8080/demo/remove?id=1) #### 视图访问路径 - index 页面 [http://127.0.0.1:8080/web/index.html?name=joey](http://127.0.0.1:8080/web/index.html?name=joey) - error 页面 [http://127.0.0.1:8080/web/error.html](http://127.0.0.1:8080/web/error.html) - 404 页面随意 #### AOP - 异常通知 [http://127.0.0.1:8080/demo/add?a=1&b=2a](http://127.0.0.1:8080/demo/add?a=1&b=2a) - 前后通知 [http://127.0.0.1:8080/demo/add?a=1&b=2](http://127.0.0.1:8080/demo/add?a=1&b=2)