文章目录

    • 一、SpringBoot简介和特性
    • 二、SpringBoot中如何配置bean
    • 三、SpringBoot的核心配置文件和核心注解
    • 四、SpringBoot自动配置原理
    • 五、SpringBoot Stater
    • 六、SpringBoot Actuator

一、SpringBoot简介和特性

1.简介:SpringBoot是一个用于快速搭建、开发、维护Spring应用的框架,集成了Spring框架的各种特性如AOP/IOC等。
2.特性:
(1)独立运行:内置Servlet容器
(2)简化配置:无需考虑包依赖和版本是否兼容,无需配置bean。只需使用starter引入web(spring+springmvc)\mybatis\mysql等模块
(3)无代码生成和XML配置
(4)运行监控:Spring Actuator

二、SpringBoot中如何配置bean

配置bean有下面几种方式,(2)和(3)是SpringBoot配置bean的主要方式:
(1)基于xml
(2)基于java代码
用于代替xml,适用那种从传统xml配置项目迁移到SpringBoot项目(如MyBatisAutoConfiguration)
(3)基于注解
①传统注解(@Autowired)
②条件依赖注解(starter就是以这种方式)
条件依赖注解(SpringBoot独有):
@ConditionalOnClass/@ConditionalOnBean:有某个class/bean存在时才生效
@AutoConfigureAfter指定顺序等

三、SpringBoot的核心配置文件和核心注解

1.SpringBoot的核心配置文件是application.properties/application.yml
2.SpringBoot的核心注解
(1)@SpringBootApplication底层由下面三个注解组成:@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan
(2)@SpringBootConfiguration:
底层是@Configuration,用来代替xml配置如applicationContext.xml。
(传统Spring项目中applicationContext.xml主要用于配置Bean/SqlSessionFactory/Aop事务这些。)
*(3)@EnableAutoConfiguration:
自动配置注解,开启后SpringBoot就能根据当前路径下的包(如.Jar)或者类来配置Spring Bean。
在下面 “四、SpringBoot自动配置原理”中会详细介绍
(4)@ComponentScan:
开启组件扫描,扫描@Compement下的bean实例,将@Controller/@Service/@Component/@Repository加载到容器中。

四、SpringBoot自动配置原理

1.源码跟踪
参考:https://editor.csdn/md/?articleId=122360367
2.总结
SpringBoot的自动配置使用@EnableAutoConfiguration注释实现,是通过找到spring.factories配置文件中的所有XXXAutoConfiguration的自动配置类,并将autoconfigure-metadata.properties中含@ConditionalOnClass@AutoConfigureAfter的配置类过滤掉,然后通过@bean注册,@Configuration加载到容器,实现这些类的自动配置。

五、SpringBoot Stater

1.介绍
(1)实现原理:SpringBoot Stater是一个集成接合器,用于引入模块所需的相关jar包和自动配置各自模块所需的属性。spring-boot启动的时候会找到starter jar包中的resources/META-INF/spring.factories文件,根据spring.factories文件中的配置,找到需要自动配置的类。通过@EnableAutoConfiguration/@ConfigurationProperties实现,使用ConfigurationProperties保存配置,且每个配置都有一个默认值,所有配置属性聚集到application.properties。
(2)自定义starter优点
①可以封装部分逻辑代码,提高项目的拓展性和易用性
②对比常规的jar包,具有自动装配的功能。

2.自定义springBoot starter使用
核心还是实现自动配置那一套东西
参考:https://editor.csdn/md/?articleId=122360367

六、SpringBoot Actuator

对程序进行监控管理,如实时查看服务器的CPU/IO,核心是端点EndPoint,内置了health/info/beans/httptrace/showdown等端点,也可以自定义端点。

更多推荐

SpringBoot常见面试题