spring

  • spring 容器,一个 IOC(Inversion of Control 控制反转)容器

  • springMVC,基于 Spring 和 servlet 的 web 应用框架

  • springBoot,集成度和自动化程度更高

spring 容器的核心概念

  • Bean 容器中的最小工作单元,通常为一个 java 对象

  • BeanFactory ApplicationContext

  • 依赖注入(dependence injection)容器负责注入所有的依赖

  • 控制翻转(inversion of control)用户将控制权交给容器

手写一个简单的 spring 实现

注意: 为了简单起见,依然使用了 maven 依赖插件 springContext 的 Autowired 注 解接口来获取依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>
    </dependencies>
  1. 定义 Bean 配置文件:我们使用 Java 支持的古老的.properties 配置文件格式 在 src/main/resource/ioc.properties

  1. Bean 文件:

  • src/mian/java/org/example/OrderDao

  • src/mian/java/org/example/OrderService

  1. 实例化 Bean,查找依赖,实现自动注入

  • src/mian/java/org/example/MyIocContainer

spring 是怎么实现的

  • 在xml中定义Bean,或者使用注解定义Bean

  • BeanDefinition的载入和解析

  • Bean 的实例化和依赖注入

  • 对外提供服务

Last updated

Was this helpful?