My email address: zrg1390556486@gmail.com
1. 启动器
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>x.x.x</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.qyzs</groupId> <artifactId>hello</artifactId> <version>1.0-SNAPSHOT</version> <name>hello</name> <description>Hello World!</description> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>x.x.x</spring-boot.version> <spring-cloud.version>x.x.x</spring-cloud.version> </properties> <dependencies> <!-- ... --> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> </plugins> </build> <profiles> <profile> <id>xxx</id> <build> <plugins> </plugins> </build> </profile> </profiles> </project>
说明:spring-boot-starter,帮助导入了web模块正常运行所依赖的组件。Spring boot 将所有的功能场景都抽象出来,做成一个个starters(启动器),只需要在项目里面引入这些starter相关场景,依赖都会导入。
2. 主程序类
主程序类
@SpringBootApplication public class WebDemoApplication { public static void main(String[] args) { SpringApplication.run(WebDemoApplication.class, args); } }
@SpringBootApplication @SpringBootApplication标注在哪个类上说明这个类是Spring Boot的主配置类,SpringBoot就运行这个类的main方法启动应用。转到定义查看:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration // SpringBoot的配置类 @EnableAutoConfiguration // 开启自动配置功能 @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})}) public @interface SpringBootApplication { }
- @SpringBootConfiguration
- @Configuration,配置类上来标注这个注解
- @Component,配置类也是容器中的一个组件
@EnableAutoConfiguration
// @EnableAutoConfiguration 转到定义: @AutoConfigurationPackage // 自动配置包 @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration { } // @AutoConfigurationPackage 转到定义: @Import({Registrar.class}) // 给容器中导入一个组件;导入的组件由Registrar.class; public @interface AutoConfigurationPackage { } // @Import({AutoConfigurationImportSelector.class}) 转到定义: public class EnableAutoConfigurationImportSelector // 导入哪些组件的选择器
- EnableAutoConfigurationImportSelector,表示导入哪些组件的选择器;
- 将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
- SpringBoot 在启动的时候从类路径下的META-IINF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中。以前我们需要自己配置。
3. 问题:Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found
解决:添加<version>2.4.1</version>,版本号与启动器版本一致。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.1</version> </plugin> </plugins> </build>