之前的文章中进行过springboot的一些应用,再说一说一些增强的内容.
SpringBoot启动原理阐明
咱们都晓得springboot非常弱小,能够实现零配置/少配置运行,以及开箱即用的个性,那么他是怎么做到的呢?
pom.xml
当咱们创立一个springboot我的项目,并只在创立时导入spring web依赖时能够看到pom.xml中有什么配置:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jt</groupId> <artifactId>springboot_demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_demo1</name> <description>入门案例</description> <!--parent标签作用: 定义了SpringBoot中所有关联我的项目的版本号信息.--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> <!--我的项目打包时,跳过测试类打包--> <skipTests>true</skipTests> </properties> <!--开箱即用:SpringBoot我的项目只须要引入大量的jar包及配置,即可领有其性能. spring-boot-starter 领有开箱即用的能力. maven我的项目中依赖具备传递性. A 依赖 B 依赖 C我的项目 导入A bc会主动依赖 --> <dependencies> <!--间接依赖web springMVC配置--> <dependency> <groupId>org.springframework.boot</groupId> <!--springBoot-start SpringBoot启动项 --> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--SpringBoot重构了测试形式 能够在测试类中 间接引入依赖对象--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <!--在我的项目打包部署时失效,如果不增加build,则程序公布时不然会报 我的项目中没有main办法. --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
pom.xml文件内标签的含意在上述代码中有所正文.
开箱即用
开箱即用:指我的项目中只须要注入大量的jar包及配置,就可领有其性能.
就是上述的pom.xml中的spring-starter启动项领有开箱即用的能力.
上图是开箱即用个性的一个梳理,接下来说一下:
首先启动类会执行
SpringApplication.run(SpringbootDemo01Application.class, args);
run办法会加载@SpringBootApplication注解:
- 有元注解(润饰注解的注解)
@Target(ElementType.TYPE)//注解对类无效@Retention(RetentionPolicy.RUNTIME)//在运行期无效@Documented//动静生成文档信息@Inherited//能够被继承
2.加载对象,但要排除的过滤器
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
3.springboot配置类
@SpringBootConfiguration又由@Configuration配置类以及元注解润饰
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Configurationpublic @interface SpringBootConfiguration {}
意味着主启动类自身就是一个超大的配置文件,能够去加载其余@Configuration注解形容的类,当启动类加载时,其余类都会加载.
4.实现开箱即用配置
@EnableAutoConfiguration由元注解以及@AutoConfigurationPackage和@Import(AutoConfigurationImportSelector.class)润饰
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import(AutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage--主动依照包扫描的形式实例化对象,之后所有的配置都须要在启动类所在包以及子孙包中进行定义.
@Import(AutoConfigurationImportSelector.class)--当程序启动时,依据SpringBoot中的Seletor选择器,去查看pom.xml文件中是否增加了启动项的包.