【Spring】@Autowired注解自动装配的过程

@Autowired 注解是Spring框架中用于自动装配Bean的一个重要注解。它可以帮助开发者在无需手动编写setter方法或构造函数的情况下,自动注入依赖的Bean。下面是 @Autowired 注解自动装配的详细过程:

自动装配的过程
1、扫描和加载Bean定义:

  • Spring容器启动时,会扫描配置类或XML配置文件中的Bean定义。
  • 这些Bean定义会被加载到Spring容器中。

2、实例化Bean:

  • Spring容器根据Bean定义创建Bean实例。
  • 如果Bean定义中指定了构造函数或工厂方法,Spring会使用这些方法来创建Bean实例。

3、解析 @Autowired 注解:

  • Spring容器在创建Bean实例后,会解析该Bean类中的 @Autowired 注解。
  • @Autowired 注解可以用于字段、setter方法、构造函数或方法参数上。

4、查找匹配的Bean:

  • Spring容器会根据 @Autowired 注解的类型和名称,在容器中查找匹配的Bean。
  • 查找规则如下:

类型匹配 :Spring会查找与 @Autowired 注解指定的类型相匹配的Bean。
名称匹配 :如果 @Autowired 注解上有 @Qualifier 注解,Spring会根据 @Qualifier 指定的名称来查找Bean。
默认名称匹配:如果 @Autowired 注解没有 @Qualifier 注解,Spring会尝试使用字段名或参数名作为Bean的名称来查找。

5、注入依赖:

  • 如果找到匹配的Bean,Spring会将该Bean注入到目标Bean中。
  • 如果找不到匹配的Bean,Spring会抛出 NoSuchBeanDefinitionException 异常。
  • 如果找到多个匹配的Bean,Spring会抛出 NoUniqueBeanDefinitionException 异常。

6、处理可选依赖:

  • 如果 @Autowired 注解上有 required=false 属性,Spring会允许找不到匹配的Bean的情况,不会抛出异常。

**示例代码

  1. 定义Bean**
java 复制代码
import org.springframework.stereotype.Component;

@Component
public class MyService {
    public void doSomething() {
        System.out.println("Doing something...");
    }
}

2. 使用 @Autowired 注解
字段注入

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyClient {

    @Autowired
    private MyService myService;

    public void performAction() {
        myService.doSomething();
    }
}

setter方法注入

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyClient {

    private MyService myService;

    @Autowired
    public void setMyService(MyService myService) {
        this.myService = myService;
    }

    public void performAction() {
        myService.doSomething();
    }
}

构造函数注入

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyClient {

    private final MyService myService;

    @Autowired
    public MyClient(MyService myService) {
        this.myService = myService;
    }

    public void performAction() {
        myService.doSomething();
    }
}

方法参数注入

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyClient {

    private MyService myService;

    @Autowired
    public void init(MyService myService) {
        this.myService = myService;
    }

    public void performAction() {
        myService.doSomething();
    }
}

处理多个匹配的Bean

如果容器中有多个匹配的Bean,可以使用 @Qualifier 注解来指定具体的Bean。

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class MyClient {

    @Autowired
    @Qualifier("myService1")
    private MyService myService;

    public void performAction() {
        myService.doSomething();
    }
}

可选依赖

如果依赖是可选的,可以使用 required=false 属性。

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyClient {

    @Autowired(required = false)
    private MyService myService;

    public void performAction() {
        if (myService != null) {
            myService.doSomething();
        } else {
            System.out.println("MyService is not available.");
        }
    }
}

总结

  • 扫描和加载Bean定义:Spring容器扫描配置类或XML配置文件中的Bean定义。
  • 实例化Bean:Spring容器根据Bean定义创建Bean实例。
  • 解析 @Autowired 注解:Spring容器解析Bean类中的 @Autowired 注解。
  • 查找匹配的Bean:Spring容器根据类型和名称查找匹配的Bean。
  • 注入依赖:Spring容器将匹配的Bean注入到目标Bean中。
  • 处理可选依赖:如果依赖是可选的,Spring容器允许找不到匹配的Bean的情况。
相关推荐
用户849137175471630 分钟前
JDK 17 实战系列(第7期):迁移指南与最佳实践
java·jvm
duration~35 分钟前
SpringAI实现Reread(Advisor)
java·人工智能·spring boot·spring
我们从未走散40 分钟前
面试题-----微服务业务
java·开发语言·微服务·架构
YuforiaCode42 分钟前
24SpringCloud黑马商城微服务整合Seata重启服务报错的解决办法
java·spring·微服务
考虑考虑1 小时前
JDK21中的Sequenced Collections(序列集合)
java·后端·java ee
蓝眸少年CY1 小时前
(第三篇)spring cloud之Zookeeper注册中心
spring·spring cloud·zookeeper
一 乐2 小时前
心理咨询|学生心理咨询评估系统|基于Springboot的学生心理咨询评估系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·学生心理咨询评估系统
Java技术小馆2 小时前
Gemini Storybook AI驱动的交互式故事创作
java·程序员·架构
dylan_QAQ2 小时前
【附录】Spring 环境配置 基础及应用
后端·spring
碎花里2 小时前
IOC控制反转深度解析
spring