当前位置: 首页 > news >正文

在 Spring Boot 中使用 `@Autowired` 和 `@Bean` 注解

文章目录

      • 在 Spring Boot 中使用 `@Autowired` 和 `@Bean` 注解
        • 示例背景
      • 1. 定义 Student 类
      • 2. 配置类:初始化 Bean
      • 3. 测试类:使用 `@Autowired` 注解自动注入 Bean
      • 4. Spring Boot 的自动装配
      • 5. 总结


在 Spring Boot 中使用 @Autowired@Bean 注解

在 Spring Boot 中,依赖注入(Dependency Injection,简称 DI)是通过 @Autowired 注解来实现的,能够有效地简化对象之间的依赖关系。同时,使用 @Bean 注解可以帮助我们在配置类中显式地定义和初始化 Bean。本文将通过一个具体示例,演示如何在 Spring Boot 中使用 @Autowired@Bean 来管理 Bean。

示例背景

假设我们有一个 Student 类,并希望通过配置类 TestConfig 来初始化 Student 对象,之后在测试类中通过 @Autowired 注解将其自动注入并使用。

1. 定义 Student 类

首先,我们定义一个简单的 Student 类,使用 @Data 注解来生成常见的 Getter、Setter、toString 等方法。

import lombok.Data;

@Data
public class Student {
    private String name;
}

在上面的 Student 类中,@Data 注解来自 Lombok,它会自动为我们生成类的所有 Getter、Setter 和 toString 等方法。这样,我们就不需要手动编写这些常见的代码,使得代码更加简洁。

2. 配置类:初始化 Bean

接下来,我们需要创建一个配置类 TestConfig,其中定义一个 @Bean 注解的方法来初始化 Student 对象。

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

@Configuration
public class TestConfig {
    @Bean
    public Student studentInit() {
        Student student = new Student();
        student.setName("初始化");
        return student;
    }
}
  • @Configuration 注解表示该类是一个配置类,Spring 会扫描该类并根据其中的 Bean 定义来初始化 Bean。
  • @Bean 注解用于告诉 Spring 容器:studentInit() 方法返回的对象(在这里是 Student)应该作为一个 Bean 进行管理。这样,Student 对象就会成为 Spring 容器中的一个管理对象。

在这个配置类中,我们显式地初始化了一个 Student 对象,并设置了它的 name 属性为 "初始化"

3. 测试类:使用 @Autowired 注解自动注入 Bean

在测试类中,我们将通过 @Autowired 注解将 Student 对象自动注入,并输出 Student 的名字。

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class StudentTest {

    @Autowired
    private Student student;

    @Test
    void contextLoads13() {
        System.out.println(student.getName()); // 输出:初始化
    }
}
  • @SpringBootTest 注解表示这是一个 Spring Boot 测试类,它会启动 Spring 容器来进行集成测试。
  • @Autowired 注解自动注入 Student Bean。Spring 会自动找到符合类型的 Student Bean 并注入到该字段中。
  • 在测试方法 contextLoads13() 中,调用 student.getName() 输出 Student 对象的 name 属性值,应该输出 "初始化",这与我们在 TestConfig 中定义的值一致。

4. Spring Boot 的自动装配

  • 在这个示例中,我们看到通过 @Autowired 注解,Spring 容器会根据 Student 类型自动为我们注入合适的 Bean。无需手动配置或创建实例。
  • 这种自动注入机制是 Spring Framework 中非常强大的特性,可以极大地简化类与类之间的依赖管理。

5. 总结

通过上述示例,我们学到了以下几点:

  • @Bean 注解:通过该注解,我们可以在配置类中显式地定义 Bean,使得对象被 Spring 容器管理。
  • @Autowired 注解:通过该注解,Spring 会自动根据类型将 Bean 注入到需要依赖的地方。
  • @Data 注解:简化了 Student 类的代码,不必手动编写 Getter、Setter 等方法。

在实际开发中,Spring 的依赖注入(DI)功能使得类之间的耦合度大大降低,提高了代码的可维护性和扩展性。通过灵活使用 @Autowired@Bean 注解,可以有效地管理和共享对象。

相关文章:

  • 一周学会Flask3 Python Web开发-response响应格式
  • 第4章:在LangChain中如何实现响应式流(Response Streaming)
  • 数据结构系列一:初识集合框架+复杂度
  • Py2学习笔记
  • Golang学习笔记_36——装饰器模式
  • 臻识相机,华夏相机,芊熠车牌识别相机加密解密
  • 【C++复习专题】—— 类和对象,包含类的引入、访问限定符、类的6个默认成员函数等
  • 通过例子学 rust 个人精简版 5-all
  • 【C语言】指针(5)
  • 小米AX3000T 路由器如何开启 SSH 安装 OpenWRT 系统,不需要降级 v1.0.91 (2025)
  • C++ unordered_map和unordered_set的使用,哈希表的实现
  • One-Prompt-One-Story:无须训练即可保持身份一致性
  • 量子计算的威胁,以及企业可以采取的措施
  • Android Studio SVN下载安装
  • Denoising Diffusion Restoration Models论文解读
  • 解释器vs编译器
  • 前端利器:DateUtils 日期时间工具深度剖析
  • 用openresty和lua实现壁纸投票功能
  • Linux运维_Dockerfile_打包Moby-26.1.4编译dockerd环境
  • 【AI】VS Code中使用GitHub Copilot
  • 关于网站开发论文参考文献/百度搜索引擎网址
  • hm网上商城/百度系优化
  • php网站源码免费下载/万能搜索引擎网站
  • 有口碑的合肥网站建设/崇左seo
  • 十款看免费行情的软件推荐/seo优化网络
  • 苏州做网站要多少钱/推广普通话文字素材