Springboot -自动配置分析

学习视频: https://www.bilibili.com/video/BV1PE411i7CV?p=6

1.1 相关依赖配置

Pom.xml

它主要是依赖一个父项目,主要是管理项目的资源过滤及插件!

<!--父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
</parent>

点进去,发现还有一个父依赖。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.4.5</version>
</parent>

这里才是真正管理SpringBoot应用里面所有依赖版本的地方,SpringBoot的版本控制中心,以后导入依赖默认是不需要写版本。但是如果导入的包没有在依赖中管理着就需要手动配置版本了。

启动器 spring-boot-starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • springboot-boot-starter-xxx:就是spring-boot的场景启动器。
  • spring-boot-starter-web:帮我们导入了web模块正常运行所依赖的组件。
  • SpringBoot将所有的功能场景都抽取出来,做成一个个的starter 启动器,只需要在项目中引入这些starter即可,所有相关的依赖都会导入进来。
  • 要用什么功能就导入什么样的场景启动器即可,我们未来也可以自己自定义 starter

1.2 主启动类自动装配原理

默认启动类

package cn.guardwhy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // 标注了一个主程序类,说明这是一个Spring Boot应用.
public class SpringbootDemo01Application {
   

    public static void main(String[] args) {
   
        // 启动了一个服务
        SpringApplication.run(SpringbootDemo01Application.class, args);
    }
}

@SpringBootApplication

进入这个注解,可以看到上面还有很多其他注解。

作用:标注在某个类上说明这个类是SpringBoot的主配置类 , SpringBoot就应该运行这个类的main方法来启动SpringBoot应用

public @interface SpringBootApplication {
   
  // 源码示例
}

1.2.1 @ComponentScan

这个注解在Spring中很重要 ,它对应XML配置中的元素,扫描当前启动类同级的包。
自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中

1.2.2 @SpringBootConfiguration

标注在某个类上 , 表示这是一个SpringBoot的配置类,继续点击注解查看。

@Configuration

@Target({
   ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
   
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

这里的 @Configuration,说明这是一个配置类 ,配置类就是对应Spring的xml 配置文件。继续点击下去

@Component

@Target({
   ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
   
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";

    boolean proxyBeanMethods() default true;
}

里面的 @Component 这就说明,启动类本身也是Spring中的一个组件而已,负责启动应用!!!

1.2.3 @EnableAutoConfiguration

单体应用架构需要自己配置文件,而现在SpringBoot可以自动帮我们配置 。

@EnableAutoConfiguration(自动导入包)告诉SpringBoot开启自动配置功能,这样自动配置才能生效,点击注解继续查看。

@AutoConfigurationPackage

具体作用

自动配置包,继续点击下去。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
   

	/**
	 * Environment property that can be used to override when auto-configuration is
	 * enabled.
	 */
	String ENABLED_OVERRIDE_PROPERTY = "spring.b
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值