【学习笔记】SpringBoot2源码级学习

1、Spring与SpringBoot

自行百度即可~

2、SpringBoot入门

3、了解自动配置原理

1、SpringBoot特点

1.1、pom依赖管理

依赖管理——每个项目的pom.xml都有这么一段代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
它的父项目
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.6.3</version>
</parent>
几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制,可以自己配置版本替换

配置前
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fisXu5kZ-1647133327403)(SpringBoot2.assets/image-20220311201233141.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xg4mRWvT-1647133321379)(SpringBoot2.assets/image-20220311201233141.png)]

配置后:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nTZPQfGC-1647133124152)(SpringBoot2.assets/image-20220311201155749.png)]

1.2、starter场景启动器

官方启动器:spring-boot-starter-*

第三方启动器:*-spring-boot-starter

只要引入场景,所有相关依赖就会自动引入

SpringBoot所有支持的场景:https://doc.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sLIceeYX-1647133124153)(SpringBoot2.assets/image-20220311202248830.png)]

  • 无需关注版本号,自动·版本仲裁
  • 可以修改版本号

1.3、自动配置

  • 自动配置Tomcat

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BMiGUT23-1647133124153)(SpringBoot2.assets/image-20220311203249488.png)]

  • 自动配置SpringMVC

    • 引入SpringMVC全套组件
    • 自动配好SpringMVC常用组件(功能)
  • 自动配好web常见功能

    • 字符编码问题
  • 默认的包结构

    • 包扫描配置
  • 各种配置拥有默认值

    • 默认配置最终都映射到对应配置类中
    • 配置文件的值最终会绑定到对应类上,这个类会在容器中创建对象
  • 按需加载所有自动配置项

    • 非常多的starter场景启动器
    • SpringBoot所有自动配置功能都在spring-boot-autoconfigure包里面

2、容器功能

2.1、组件添加

@Configuration

  • 基本使用
  • Full模式与Lite模式
    • 示例:
    • 最佳实战

1、Full与Lite实践:

相关文章链接:https://blog.csdn.net/hhhjjj201830341/article/details/123441496

proxyBeanMethods=false时,使用依赖注入IDEA会报红,但是不影响运行!!!Method annotated with @Bean is called directly in a @Configuration where proxyBeanMethods set to false. Set proxyBeanMethods to true or use dependency injection.·

译文:用@ bean注释的方法是直接调用在@ configuration proxyBeanMethods设置为false。需要proxyBeanMethods设置为true或使用依赖注入。

User类中的pet属性类型是Pet类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RW40HixB-1647133124155)(SpringBoot2.assets/image-20220311215809341.png)]

测试代码及结果:

//配置类本身也是组件
MyConfig myConfig = run.getBean(MyConfig.class);
System.out.println("配置类本身也是组件:" + myConfig);

//proxyBeanMethods=false测试依赖注入
User u3 = myConfig.user01();
Pet p31 = u3.getPet();

Pet p1 = myConfig.pet01();
//结果为false,即不是同一个,则无法实现依赖注入!!!
System.out.println("u3中的p31是否和p1相同:" + (p31 == p1));

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tm02CVUG-1647133124156)(SpringBoot2.assets/image-20220311220632928.png)]

2、@Bean、@Component、@Controller、@Service、@Repository

3、@ComponentScan、@Import

  • @ComponentScan——包扫描,定义包扫描规则
  • @Import——@Import({User.class,CloudPlatform.class}):给容器中自动创建出这两个组件,默认组件名为全限定类名!!!

4、@Conditional

条件装配:满足Conditional指定的条件,则进行组件注入

2.2、原生配置文件引入

1、@ImportResource

在任何一个配置类上导入即可:
@ImportResource("classpath:spring.xml")
2.3、属性值绑定

1、@ConfigurationProperties

配置文件

#定义car对象,brand、price都为属性名
car.brand=比亚迪
car.price=19999.0

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Component //一定要将该类交给Spring容器管理,才能使用Spring功能
@ConfigurationProperties(prefix = "car")
public class Car {
    private String brand;
    private double price;
}

测试代码

//@ConfigurationProperties(prefix="car") car为配置文件定义的对象
System.out.println("--@ConfigurationProperties(prefix=car)--");
Car bean = run.getBean(Car.class);
System.out.println(bean);

结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eLQofNHx-1647133124157)(SpringBoot2.assets/image-20220312135932957.png)]

2、@EnableConfigurationProperties+@ConfigurationProperties

适用于实体类为第三方类上没有注册到容器中的注解

一定要在配置类中定义该注解

@Configuration(proxyBeanMethods = true) //配置类
//Import({User.class, CloudPlatform.class})
@ConditionalOnMissingBean(name = "aml")
@ImportResource("classpath:spring.xml")
@EnableConfigurationProperties(Car.class)//1、开启自动绑定功能 2、自动将此类交给Spring注册,可以不需要在实体类上加上Component注解
public class MyConfig {
    ......
}

3、自动配置原理入门

3.1、引导加载自动配置类

***@SpringBootApplication == @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan***

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    
}

1、@SpringBootConfiguration——表明配置类

2、@ComponentScan——包扫描,指定哪些包下被spring扫描

3、@EnableAutoConfiguration——核心

包含两个注解:@AutoConfigurationPackage、@Import(AutoConfigurationImportSelector.class)

  • @AutoConfigurationPackage——自动配置包
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
    
}
AutoConfigurationPackages.Registrar.class内实现批量注册组件的方法(主程序类所在包下的所有标注的注解)
  • @Import(AutoConfigurationImportSelector.class)

核心:getAutoConfigurationEntry(annotationMetadata)

//AutoConfigurationImportSelector.class 该方法是具体导入需要注册组件
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!isEnabled(annotationMetadata)) {
        return NO_IMPORTS;
    }
    AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
    return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
3.2、按需开启自动配置项

项目启动时默认全部加载127个场景的所有自动配置

但最终会根据条件装配规则按需加载,

3.3、定制化修改自动配置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xtGjb4PP-1647133124158)(SpringBoot2.assets/image-20220312151525378.png)]

//SpringBoot官方代码
@Bean
@ConditionalOnBean(MultipartResolver.class)
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
public MultipartResolver multipartResolver(MultipartResolver resolver) {
    // Detect if the user has created a MultipartResolver but named it incorrectly
    return resolver;
}

@Bean
@ConditionalOnBean({MultipartResolver.class})
@ConditionalOnMissingBean(
    name = {"multipartResolver"}
)
public MultipartResolver multipartResolver(MultipartResolver resolver) {
    return resolver;
}

SpringBoot默认会配置好所有的组件,如果用户也配置了,则以用户配置的优先!!!因为@Conditional

总结:

  • SpringBoot先加载所有的自动配置类,xxxAutoConfiguration
  • 每个自动配置类按照条件进行生效
  • 生效的配置类就会给容器中装配很多组件
  • 只要容器中有这些组件,相当于这些功能就有了
  • 只要用户自己配置了,就以用户配置的为主
  • 每个自动配置类生效就会绑定对应配置文件指定的值,xxxProperties——application的配置文件
  • 定制化配置
    • 用户直接自己@Bean替换底层的组件
    • 直接修改配置文件相关的值
3.4、最佳实践
  • 引入场景依赖
  • 查看提供了哪些自动配置
  • 是否需要修改
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值