Spring Boot中自动配置原理-1

本文探讨了Spring Boot自动配置的原理,主要包括:1. 启动时加载大量自动配置类;2. 检查是否存在对应功能的自动配置类;3. 查看配置类如何配置所需组件;4. 自动配置类如何从properties类获取并使用配置文件中的属性。通过自动配置,我们可以避免手动配置组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot中自动配置原理-1

重点:

1. SpringBoot启动会加载大量的自动配置类;
2. 看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3. 再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)
4. 给容器中自动配置类添加组件的时候,会从properties类中获取某些属性;我们可以在配置文件中指定这些属性的值。

**xxxxAutoConfigurartion:自动配置类:给容器中添加组件;

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({MessageDispatcherServlet.class})
@ConditionalOnMissingBean({WsConfigurationSupport.class})
@EnableConfigurationProperties({WebServicesProperties.class})
@AutoConfigureAfter({ServletWebServerFactoryAutoConfiguration.class})
public class WebServicesAutoConfiguration {
    public WebServicesAutoConfiguration() {
    }

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext, WebServicesProperties properties) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);

在这里插入图片描述

xxxxProperties:封装配置文件中相关属性。**

@ConfigurationProperties(
    prefix = "spring.webservices"
)
public class WebServicesProperties {
    private String path = "/services";
    private final WebServicesProperties.Servlet servlet = new WebServicesProperties.Servlet();

    public WebServicesProperties() {
    }

    public String getPath() {
        return this.path;
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值