java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot Starter 自动装配

Spring Boot Starter 自动装配原理全解析

作者:葵续浅笑

Spring Boot Starter 的核心设计理念是 约定优于配置,其核心实现基于 自动配置(Auto-Configuration) 和 条件化注册(Conditional Registration),这篇文章主要介绍了Spring Boot Starter 自动装配原理全解析,需要的朋友可以参考下

Spring Boot Starter 的核心设计理念是 约定优于配置,其核心实现基于 自动配置(Auto-Configuration)条件化注册(Conditional Registration)。以下是其生效原理:

约定大于配置

通过预定义合理的默认行为和规范,减少开发者需要手动配置的步骤。比较显著的变化就是减少XML配置。还有一些实际体现如下所示:

自动配置机制

触发阶段:@EnableAutoConfiguration

public String[] selectImports(AnnotationMetadata metadata) {
  // 1. 加载所有候选自动配置类
  List<String> configurations = getCandidateConfigurations();
  // 2. 去重、过滤、排序
  configurations = removeDuplicates(configurations);
  configurations = filter(configurations, autoConfigurationMetadata);
  return configurations.toArray(new String[0]);
}

加载与筛选:spring.factories

从所有 META-INF/spring.factories 文件中读取 EnableAutoConfiguration 对应的配置类。在 Spring Boot 3.x 中,自动配置类的加载方式从 spring.factories 过渡到 AutoConfiguration.imports,并引入了 ImportCandidates 类来处理这一变化。

移除重复的配置类,并通过条件注解(如 @ConditionalOnClass ,@ConditionalOnMissingBean ) 有选择的保留当前环境的配置类。

排序

根据 @AutoConfigureOrder@AutoConfigureAfter 调整配置类的加载顺序。

Bean 注册

编写自定义Spring Boot Starter

项目结构规划

建议分为两个模块:

hello-spring-boot-starter-parent(父POM)
├── hello-spring-boot-autoconfigure(自动配置模块)
└── hello-spring-boot-starter(Starter模块)
hello-spring-boot-starter/
├── hello-spring-boot-autoconfigure/
│   ├── src/
│   │   ├── main/
│   │   │   ├── java/com/example/autoconfigure/
│   │   │   │   ├── HelloAutoConfiguration.java
│   │   │   │   ├── HelloProperties.java
│   │   │   │   └── HelloService.java
│   │   │   └── resources/
│   │   │       └── META-INF/
│   │   │           └── spring.factories
│   │   └── test/
│   └── pom.xml
├── hello-spring-boot-starter/
│   └── pom.xml
└── pom.xml

创建自动配置模块(hello-spring-boot-autoconfigure)

添加Maven依赖

<!-- pom.xml -->
<dependencies>
    <!-- Spring Boot 自动配置基础依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>3.1.5</version>
    </dependency>
    <!-- 可选:配置属性处理 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>3.1.5</version>
        <optional>true</optional>
    </dependency>
</dependencies>

定义核心服务类

public class HelloService {
    private String message = "Hello, World!";  // 默认消息
    public String sayHello() {
        return message;
    }
    // Getter和Setter用于通过配置修改message
    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }
}

定义配置属性类(可选)

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String message = "Hello, World!";
    // Getter和Setter
    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }
}

编写自动配置类

@Configuration
@EnableConfigurationProperties(HelloProperties.class)  // 启用配置属性
@ConditionalOnClass(HelloService.class)  // 当HelloService在类路径时生效
public class HelloAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean  // 当用户未自定义HelloService时生效
    public HelloService helloService(HelloProperties properties) {
        HelloService service = new HelloService();
        service.setMessage(properties.getMessage());
        return service;
    }
}

注册自动配置

resources/META-INF/ 下创建 spring.factories 文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfigure.HelloAutoConfiguration

创建Starter模块(hello-spring-boot-starter)

添加Maven依赖

<!-- pom.xml -->
<dependencies>
    <!-- 引入自动配置模块 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>hello-spring-boot-autoconfigure</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

使用自定义Starter

在应用中引入Starter依赖

<!-- 用户项目的pom.xml -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

在代码中注入Bean

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    public String hello() {
        return helloService.sayHello();
    }
}

自定义配置(可选)

application.properties 中修改消息:

hello.message=你好, Spring Boot!

到此这篇关于Spring Boot Starter 自动装配原理的文章就介绍到这了,更多相关Spring Boot Starter 自动装配内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文