墨风如雪博客

  • 源码小店
  • 传家宝VPS
让AI使用变得如此简单
  1. 首页
  2. java
  3. spring
  4. 正文

Spring Boot自动配置原理详解(超详细)

2023年 5月 16日 266点热度 0人点赞 0条评论

一、Spring Boot自动配置概述

1.1 什么是Spring Boot自动配置

Spring Boot自动配置是Spring Boot框架提供的一个重要特性,它可以根据应用程序的依赖关系自动配置应用程序的环境和功能。在没有显式配置的情况下,Spring Boot会根据应用程序的类路径和其他条件自动配置应用程序,使得开发者无需手动配置大量的参数和选项,从而使得开发变得更加快捷、简便和高效。

例如,在Spring Boot中使用JDBC时,我们只需要在pom.xml文件中添加相应的依赖,Spring Boot就会自动配置数据源,并且我们无需手动配置数据库连接信息、事务管理等。

1.2 为什么需要Spring Boot自动配置

在传统的Java Web应用中,我们需要手动编写大量的XML配置文件或Java配置类,才能完成应用程序的配置。这种方式既繁琐又容易出错,而且对于初学者来说,学习成本也很高。而Spring Boot自动配置则可以解决这个问题,使得我们可以更加关注业务逻辑的实现,而不用过多地关注应用程序的配置。

二、Spring Boot自动配置实现原理

2.1 Spring Boot自动配置启动流程

Spring Boot自动配置的启动流程如下:

  1. Spring Boot启动时,会加载META-INF/spring.factories文件,该文件中定义了各个自动配置类的类路径。

  2. Spring Boot会根据类路径加载所有的自动配置类,并将它们存储在一个名为autoConfigurations的列表中。

  3. Spring Boot会根据autoConfigurations列表中的自动配置类,依次尝试自动配置应用程序的环境和功能。

下面是一个简单的示例,演示了如何使用Spring Boot自动配置JDBC数据源。

首先,在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

然后,在application.properties文件中添加以下配置:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

最后,在代码中使用JdbcTemplate操作数据库:

@Autowired
private JdbcTemplate jdbcTemplate;

public void saveUser(User user) {
    String sql = "INSERT INTO user (name, age) VALUES (?, ?)";
    jdbcTemplate.update(sql, user.getName(), user.getAge());
}

在这个示例中,我们只需要添加相应的依赖和配置,Spring Boot就会自动配置JDBC数据源,并且我们可以直接使用JdbcTemplate操作数据库,无需手动配置数据源信息。

2.2 Spring Boot自动配置原理解析

Spring Boot自动配置的实现原理非常复杂,但是可以简单地概括为以下几个步骤:

  1. Spring Boot会根据应用程序的类路径和其他条件自动配置应用程序的环境和功能。

  2. Spring Boot会根据自动配置类中定义的条件,判断是否需要进行自动配置。

  3. Spring Boot会根据自动配置类中定义的配置属性,对应用程序进行必要的配置。

  4. Spring Boot会将自动配置类中配置的Bean注册到Spring容器中,供应用程序使用。

下面是一个简单的示例,演示了如何自定义Spring Boot自动配置类。

首先,创建一个自定义的自动配置类:

@Configuration
@ConditionalOnClass(MyService.class)
public class MyAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MyService myService() {
        return new MyService();
    }

}

在这个示例中,我们定义了一个名为MyAutoConfiguration的自动配置类。@Configuration注解表示这是一个配置类,@ConditionalOnClass注解表示只有当MyService类在类路径中时,才会进行自动配置。@Bean注解表示定义了一个名为myService的Bean,并且使用@ConditionalOnMissingBean注解表示只有当容器中不存在名为myService的Bean时,才会创建该Bean。

然后,在代码中使用MyService:

@Autowired
private MyService myService;

在这个示例中,我们只需要在类路径中添加MyService类,Spring Boot就会自动配置Bean,并且我们可以直接使用@Autowired注解注入MyService,无需手动配置Bean。

三、自定义Spring Boot自动配置

3.1 自定义starter

在Spring Boot中,我们可以通过自定义starter来扩展应用程序的功能。自定义starter主要包括以下几个步骤:

  1. 创建一个Maven项目,命名为xxx-spring-boot-starter。

  2. 在项目中,创建一个名为xxx-spring-boot-autoconfigure的模块,并在该模块中编写自动配置类。

  3. 在项目中,创建一个名为xxx-spring-boot-starter的模块,并在该模块中编写starter类。

  4. 在项目中,创建一个名为xxx-spring-boot-sample的模块,并在该模块中演示如何使用自定义starter。

下面是一个简单的示例,演示了如何创建一个自定义starter。

首先,创建一个xxx-spring-boot-autoconfigure模块,并在该模块中编写自动配置类:

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService() {
        return new HelloService(helloProperties.getMessage());
    }

}

在这个示例中,我们定义了一个名为HelloAutoConfiguration的自动配置类。@Configuration注解表示这是一个配置类,@EnableConfigurationProperties注解表示启用配置属性,@ConditionalOnClass注解表示只有当HelloService类在类路径中时,才会进行自动配置。@Bean注解表示定义了一个名为helloService的Bean,并且使用@ConditionalOnMissingBean注解表示只有当容器中不存在名为helloService的Bean时,才会创建该Bean。

然后,创建一个xxx-spring-boot-starter模块,并在该模块中编写starter类:

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
@AutoConfigureBefore(HelloAutoConfiguration.class)
@AutoConfigureAfter(HelloConfiguration.class)
public class HelloAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService() {
        return new HelloService(helloProperties.getMessage());
    }

}

在这个示例中,我们定义了一个名为HelloStarter的starter类。@Configuration注解表示这是一个配置类,@EnableConfigurationProperties注解表示启用配置属性,@ConditionalOnClass注解表示只有当HelloService类在类路径中时,才会进行自动配置。@AutoConfigureBefore注解表示在HelloAutoConfiguration之前进行自动配置,@AutoConfigureAfter注解表示在HelloConfiguration之后进行自动配置。

最后,创建一个xxx-spring-boot-sample模块,并在该模块中演示如何使用自定义starter:

@SpringBootApplication
public class SampleApplication {

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

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.sayHello();
    }

}

在这个示例中,我们使用@Autowired注解注入HelloService,然后在@GetMapping注解中使用helloService.sayHello()方法输出Hello World。

3.2 自定义属性配置

在Spring Boot中,我们可以通过@ConfigurationProperties注解来自定义属性配置。@ConfigurationProperties注解可以将配置文件中的属性值映射到Java对象的属性中。

以下是一个简单的示例,演示了如何自定义属性配置。

首先,在xxx-spring-boot-autoconfigure模块中创建一个名为HelloProperties的类,并使用@ConfigurationProperties注解将配置文件中的属性值映射到该类的属性中:

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    private String message = "World";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

在这个示例中,我们使用@ConfigurationProperties注解将配置文件中以hello为前缀的属性值映射到HelloProperties类的属性中,并且默认的message属性值为"World"。

然后,在HelloAutoConfiguration类中使用@Autowired注解将HelloProperties类注入到自动配置类中,并使用该类中的属性值对应用程序进行配置:

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService() {
        return new HelloService(helloProperties.getMessage());
    }

}

在这个示例中,我们使用@Autowired注解将HelloProperties类注入到HelloAutoConfiguration自动配置类中,并使用该类中的message属性值进行自动配置。

最后,在application.properties文件中添加以下配置:

hello.message=ChatGPT

在这个示例中,我们使用hello.message属性覆盖了默认的message属性值,使得应用程序输出"Hello ChatGPT"。

3.3 自定义条件注解

在Spring Boot中,我们可以使用@Conditional注解来定义自定义条件注解,从而实现更加灵活的自动配置。

以下是一个简单的示例,演示了如何定义自定义条件注解。

首先,在xxx-spring-boot-autoconfigure模块中创建一个名为MyCondition的类,并实现Condition接口:

public class MyCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // TODO: 实现条件判断逻辑
        return false;
    }

}

在这个示例中,我们实现了Condition接口,并在matches方法中实现了自定义的条件判断逻辑。

然后,在HelloAutoConfiguration类中使用@Conditional注解将自定义条件注解绑定到自动配置类上:

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
@Conditional(MyCondition.class)
public class HelloAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService() {
        return new HelloService(helloProperties.getMessage());
    }

}

在这个示例中,我们使用@Conditional注解将MyCondition自定义条件注解绑定到HelloAutoConfiguration自动配置类上,表示只有当MyCondition条件满足时,才会进行自动配置。

最后,在代码中使用HelloService:

@Autowired
private HelloService helloService;

在这个示例中,我们只需要在MyCondition条件满足的情况下,Spring Boot就会自动配置HelloService,并且我们可以直接使用@Autowired注解注入HelloService,无需手动配置Bean。

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: AOP DI ioc java JDK spring springboot 依赖注入 自动装配
最后更新:2023年 5月 16日

墨风如雪

一个热爱生活,热爱分享的程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

墨风如雪

一个热爱生活,热爱分享的程序员

最新 热点 随机
最新 热点 随机
美团LongCat-Audio-Codec:给语音大模型装上“顺风耳”与“巧舌” 告别无声AI视频!谷歌Veo 3.1打造沉浸式视听盛宴 Karpathy的nanochat:百元就能造ChatGPT?AI圈炸锅了! 微软MAI-Image-1:告别依赖,自研图像AI能否破局? 具身智能的里程碑:Figure 03 解读 快手CodeFlicker:AI编程新战局,开发者准备好了吗?
“小钢炮”驾到!VoxCPM:0.5B参数,震撼AI语音圈双面魔术师:Wan2.2-Animate,让视频焕发生机语音AI的“奇点”降临?小米MiMo-Audio,不止开源!机器人“小脑”上线:InternVLA-A1如何颠覆未来?国产大模型安全新突破:DeepSeek-R1-Safe,平衡木上的舞者10秒100MB,ChatExcel一键PPT:它真把报告变“魔法”了?
每日一道算法题:随机乱序化 如何使用 Cloudflare 免费 CDN 加速和保护你的网站 每日一道算法题:合并两个有序链表 设计模式:命令设计模式 每日一道算法题:最长回文子串 iOS快捷指令×DeepSeek:三步打造智能自动化工作流
标签聚合
AI 大模型 教程 设计模式 deepseek 算法 java spring

COPYRIGHT © 2023 墨风如雪博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang