SpringBoot 创建对象常见的几种方式

SpringBoot 创建对象常见的几种方式

在 Spring Boot 中,将 Bean 对象添加到 IOC 容器中,通用的有下面几种方式:

  1. 使用 @Component@Service@Repository@Controller 注解
  2. 使用 @Configuration@Bean 注解
  3. 使用 @Import 注解导入其他配置类
  4. 通过 ApplicationContext 编程方式手动注册 Bean

1. 使用 @Component(或 @Service@Repository@Controller)注解

最常见的方式,Spring 会自动扫描并将带有这些注解的类注册为 Bean。

java 复制代码
import org.springframework.stereotype.Component;

@Component
public class User {
    public void say() {
        System.out.println("Hello User!");
    }
}

配置扫描路径 (通常在 @SpringBootApplication 上已经启用了扫描,如果需要指定包路径,可以使用 @ComponentScan 注解):

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

在 Spring Boot 启动时,@Component 注解的类会自动注册为 Bean,并添加到 IOC 容器中。

2. 使用 @Configuration@Bean 注解

通过在 @Configuration 注解的配置类中使用 @Bean 注解,可以手动将对象添加到 Spring 容器中。

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean
    public User user() {
        return new User();
    }
}

User 类

java 复制代码
public class user {
    public void say() {
         System.out.println("Hello User!");
    }
}

BeanConfig 类使用 @Bean 注解注册了一个 user 实例,Spring 会将其自动加入到 IOC 容器中。

3. 使用 @Import 注解导入配置类

@Import 注解可以导入其他配置类,将其配置的 Bean 添加到当前应用的 IOC 容器中。

java 复制代码
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(BeanConfig.class)
public class MainConfig {
    // 通过 @Import 导入 BeanConfig 中的 Bean
}

MainConfig 类通过 @Import(BeanConfig.class) 导入了 BeanConfig 中定义的所有 Bean,这样 MyBean 也会被注册到 IOC 容器中。

4. 使用 ApplicationContext 编程方式手动注册 Bean

在某些特殊的场景中,可能需要手动编程注册 Bean,这时可以使用 AnnotationConfigApplicationContextGenericWebApplicationContext 类。

java 复制代码
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ManualBeanRegistration {

    public static void main(String[] args) {
        // 创建 Spring 容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 注册配置类
        context.register(BeanConfig.class);

        // 启动容器
        context.refresh();

        // 获取并使用 Bean
        MyBean myBean = context.getBean(MyBean.class);
        myBean.printMessage();

        // 关闭容器
        context.close();
    }
}

通过 AnnotationConfigApplicationContext 显式地手动注册了 BeanConfig 配置类,并启动了 Spring 容器。

总结

  1. @Component 注解 (及其衍生注解 @Service@Repository@Controller)是最常用的方式,通过自动扫描自动将 Bean 注册到 IOC 容器中。
  2. @Configuration@Bean 注解 可以在配置类中手动注册 Bean。
  3. @Import 注解 可以将其他配置类中的 Bean 导入到当前配置类中。
  4. 手动注册 通过 ApplicationContext 等类可以编程方式注册 Bean。
相关推荐
fire-flyer40 分钟前
Spring Boot 源码解析之 Logging
java·spring boot·spring·log4j·logging
遥不可及~~斌44 分钟前
Spring Boot RESTful API 设计指南:查询接口规范与最佳实践
spring boot·后端·restful
KoiHeng2 小时前
部分排序算法的Java模拟实现(复习向,非0基础)
java·算法·排序算法
allenXer5 小时前
Spring Boot测试全景指南:JUnit 5 + Testcontainers实现单元与集成测试
spring boot·微服务·log4j
cui_hao_nan5 小时前
JVM——如何对java的垃圾回收机制调优?
java·jvm
熟悉的新风景7 小时前
springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test
java·spring boot·后端
心平愈三千疾7 小时前
学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
java·分布式·学习
玩代码7 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
BUTCHER58 小时前
Docker镜像使用
java·docker·容器
岁忧8 小时前
(nice!!!)(LeetCode 面试经典 150 题 ) 30. 串联所有单词的子串 (哈希表+字符串+滑动窗口)
java·c++·leetcode·面试·go·散列表