springboot原理初探
阿新 • • 發佈:2018-11-07
1. 父專案
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> 它的父專案是 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.0.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent> 這個父專案管理所有依賴的版本,被稱為spring boot版本仲裁中心 以後導依賴預設使用這裡的版本,不需要自己寫,沒有的才需要自己寫版本
2. starter 啟動器
starter 是為滿足某一場景的一系列依賴集合
官方介紹
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
3. Spring Boot入口程式
@SpringBootApplication public class HelloworldMain { public static void main(String[] args) { SpringApplication.run(HelloworldMain.class, args); } }
@SpringBootApplication
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration // 註解標註這是一個spring boot配置類
@EnableAutoConfiguration // 開啟自動配置功能
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@SpringBootConfiguration 註解標註這是一個spring boot配置類
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
@EnableAutoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
// 將被註解類所在包及子包下的元件掃描到spring容器,相當於以前spring裡面的註解配置類
@AutoConfigurationPackage
// 完成功能的自動配置,如以前要自己做的spring mvc配置
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {