Spring的依赖注入过程

测试环境配置

个人测试环境目录结构

测试环境依赖导入

1
2
3
4
5
6
7
8
<dependencies>
<!--Spring框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.16</version>
</dependency>
</dependencies>

测试结构

domain层

Test01类

@Component 代表该类被交由Spring容器管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.kangkang.domain;

import org.springframework.stereotype.Component;

/**
* @description: //TODO
* @author: kangkang
* @date: 2024-01-26 19:58
*/
@Component
public class Test1 {

}

Test02类

@Component 代表该类被交由Spring容器管理

@AutoWired 会从Spring容器中寻找匹配的bean来注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.kangkang.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* @description: //TODO
* @author: kangkang
* @date: 2024-01-26 19:58
*/
@Component
public class Test2 {

@Autowired
private Test1 name;

public void test(){
System.out.println("Spring自动注入:"+name);
}
}

AppConfig配置类

@ComponentScan 配置组件扫描的基础包路径,Spring 将在这些包中查找标有 @Component 注解的类。

@Configuration 表明该类是一个配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.kangkang;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* @description: //TODO
* @author: kangkang
* @date: 2024-01-26 20:05
*/
@ComponentScan("com.kangkang")
@Configuration
public class AppConfig {
}

Test 测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.kangkang;

import com.kangkang.domain.Test2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @description: //TODO
* @author: kangkang
* @date: 2024-01-26 20:06
*/
public class Test {

public static void main(String[] args) {

AnnotationConfigApplicationContext application = new AnnotationConfigApplicationContext(AppConfig.class);

Test2 test4 = (Test2) application.getBean(Test2.class);
test2.test();
}
}

具体执行步骤

程序会从Test测试类开始

1
AnnotationConfigApplicationContext application = new  AnnotationConfigApplicationContext(AppConfig.class);
  1. 首先会创建一个AnnotationConfigApplicationContext对象 ,这个类是ApplicationContex这个接口的实现类


  2. ApplicationContext继承了BeanFactory,这个BeanFactory才是真正去制造bean的,之后的ApplicationContext、AnnotationConfigApplicationContext都是在它的基础上提供了更多的功能,如事件发布、国际化、AOP 等,使得它更适合用于企业级应用程序的开发。


  3. AnnotationConfigApplicationContext会根据传入参数的AppConfig.class中的@ ComponentScan这个注解中的包路径去寻找是否带@Component注解的类,以及获取类的所有属性以及方法来确认是否有@AutoWired注解或其他注解 ()

    1
    2
    3
    4
    @ComponentScan("com.kangkang")
    @Configuration
    public class AppConfig {
    }

  4. 这个时候就会根据@Component注解往Spring容器中注入两个bean,(一个Test01一个Test02) 而Test02中的@AutoWired注解会从容器中得到之前注入的Test01bean,注入到Test02中的name属性中

    1
    2
    @Autowired
    private Test1 name;
    1. 在容器中查找 Bean 定义: Spring 容器会根据传入的类型 Test2.class 在容器中查找对应的 Bean 定义。这可以是通过注解标记的类(例如 @Component@Service 等),也可以是通过 Java 配置类中的 @Bean 方法定义的。

    2. 实例化 Bean: 如果找到了匹配的 Bean 定义,Spring 会实例化一个 Test2 类型的对象。这可能涉及到构造函数的调用,依赖项的注入等。

    3. 注入依赖项: 如果 Test2 类中有其他依赖项(通过构造函数、字段或方法注入),Spring 会尝试解析这些依赖项并递归地注入它们。

    4. 返回 Bean 实例: 最终,getBean(Test2.class) 方法会返回类型为 Test2 的 bean 实例。

      1
      2
      Test2 test4 = (Test2) application.getBean(Test2.class);
      test2.test();

      注意Spring中默认模式是单例的也就是说每个bean都是唯一的,可以通过debug一下来演示,可以看见每个对象都是一样的 Test2@1869


  5. 最后通过Test2bean对象成功调用Test 输出Test01的对象

    1
    Spring自动注入:com.kangkang.domain.Test1@8458f04

Spring的依赖注入过程
http://example.com/2024/01/26/Spring的依赖注入过程/
作者
kangkang
发布于
2024年1月26日
许可协议