SpringBoot是什么
- 約定優(yōu)于配置理念下的產(chǎn)物,簡化Spring使用難度,簡省配置,快速高效使用Spring框架開發(fā)應用
約定優(yōu)于配置
約定是一種規(guī)范,即大家定好某某東西默認應該是怎么樣的,以達到簡化配置的作用,當不配置時,使用約定好的值
快速高效,以SpringMVC為例子
- 不使用SpringBoot
- 創(chuàng)建一個項目
- 添加依賴
- 配置web.xml,DispatchServlet,componentScan
- 寫Controller業(yè)務代碼
- 發(fā)布到Servlet容器
- 使用SpringBoot
- 創(chuàng)建項目
- 寫Controller業(yè)務代碼
SpringBoot 四大特性
- 自動裝配 AutoConfiguration
- 啟動依賴 Starter
- 運行監(jiān)控 Actuator
- 命令行 CLI
Spring注解驅(qū)動的發(fā)展
1.x
此時Java5剛出來,興起了使用Annotation的技術(shù)風。但此時Spring裝載Bean還是xml
<bean id="" class=""/>
2.x
提供了大量注解
- @Component、@Service組件聲明
- @Controller、@RequestMapping等SpringMVC的注解
- @Autowired、@Qualifier依賴注入等等
此時還沒有完全去xml化,還需要xml配置像component-scan
相對1.x減少xml配置
3.x
注解驅(qū)動的里程碑,功能特性有非常大的擴展
- @Configuration 完全的去xml化
- @ComponentScan 配置掃描路徑
- @Import 導入配置類
- @Enablexxx 模塊化裝載
4.x
@Conditional 條件完成Bean初始化
5.x
@Indexed 索引,提升componentScan的性能
Spring的動態(tài)裝配(動態(tài)的裝配)
- ImportSelector
- ImportBeanDefinitionRegistrar
SPI
最早來源于Java Service Loader 稱為service provider interface,為接口提供實現(xiàn)。Spring進行擴展SpringFactoriesLoader:會拿到所有META-INF/spring.factories所配置的接口與實現(xiàn)
- 寫在classpath下的META-INF/spring.factories
- 配置對應的key-value 比如自動裝載的 org.springframework.boot.autoconfigure.EnableAutoConfiguration=你的自動裝載類
自動裝配
由@EnableAutoConfiguration開始,開啟自動裝配
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})//運用Spring的動態(tài)裝載,加載自動裝配類
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}

enableAutoConfiguration.png