全网最细的SpringBoot3系列教程
1、开发第⼀个Spring Boot应用
创建POM
因为是3.0.0-M1版本,是⾥程碑版本,不是正式发布版,需要从Spring提⾼的Maven仓库中才能下载到3.0.0-M1版本的依赖包,需要在pom.xml⽂件中单独指定仓库地址。
如果使⽤的是正式版,是不需要加下⾯这段配置的
<!-- jar包的仓库地址--> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <!-- maven插件的仓库地址 --> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories>
另外还要在pom.xml⽂件中,添加Spring Boot所提供的⽗pom.xml。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0-M1</version> </parent>
关于这个parent的作⽤,后续课程中会介绍。
添加依赖
最后添加⼀个开发web应⽤的starter依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
写代码
在写代码之前,请注意,我现在⽤的JDK8,也许你也是。 创建⼀个类,类名随便取,我的叫MyApplication,包路径为com.zhouyu
package com.zhouyu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class MyApplication { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
然后,运⾏main⽅法。 我这报错了:
因为Spring Boot 3⽤的是Spring 6,⽽Spring 6需要⽤JDK17。
所以,我们要在IDEA中配上JDK17,JDk17的安装没什么特殊的,和JDK8的安装⼀样,就不在笔记⾥浪费篇幅了。
⽤上JDK17之后,再次运⾏main⽅法,就能正常启动我们的第⼀个Spring Boot应⽤程序了,并且会看到如下⽇志:
"C:Program FilesJavajdk-17.0.1injava.exe" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2021.1.3libidea_rt.jar=64855:C:Program FilesJetBrainsIntelliJ IDEA 2021.1.3in" -Dfile.encoding=UTF-8 - classpath D:IdeaProjectshouyuSpringBootTeacher argetclasses;C:Userszhouyu.m2 epositoryorgspringframework ootspring-boot-starter-web3.0.0-M1spring-boot-starter-web-3.0.0- M1.jar;C:Userszhouyu.m2 epositoryorgspringframeworkootspringboot-starter3.0.0-M1spring-boot-starter-3.0.0- M1.jar;C:Userszhouyu.m2 epositoryorgspringframeworkootspringboot3.0.0-M1spring-boot-3.0.0- M1.jar;C:Userszhouyu.m2 epositoryorgspringframeworkootspringboot-autoconfigure3.0.0-M1spring-boot-autoconfigure-3.0.0- M1.jar;C:Userszhouyu.m2 epositoryorgspringframeworkootspringboot-starter-logging3.0.0-M1spring-boot-starter-logging-3.0.0- M1.jar;C:Userszhouyu.m2 epositorychqoslogbacklogbackclassic1.2.10logback-classic1.2.10.jar;C:Userszhouyu.m2 epositorychqoslogbacklogbackcore1.2.10logback-core1.2.10.jar;C:Userszhouyu.m2 epositoryorgslf4jslf4japi1.7.33slf4j-api1.7.33.jar;C:Userszhouyu.m2 epositoryorgapachelogginglog4jlog4jto-slf4j2.17.1log4j-to-slf4j2.17.1.jar;C:Userszhouyu.m2 epositoryorgapachelogginglog4jlog4japi2.17.1log4j-api2.17.1.jar;C:Userszhouyu.m2 epositoryorgslf4jjul-toslf4j1.7.33jul-to-slf4j1.7.33.jar;C:Userszhouyu.m2 epositoryjakartaannotationjakarta.anno tation-api2.0.0jakarta.annotation-api2.0.0.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringcore6.0.0-M2spring-core-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringjcl6.0.0-M2spring-jcl-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgyamlsnakeyaml1.30snakeyaml1.30.jar;C:Userszhouyu.m2 epositoryorgspringframeworkootspringboot-starter-json3.0.0-M1spring-boot-starter-json-3.0.0- M1.jar;C:Userszhouyu.m2 epositorycomfasterxmljacksoncorejacksondatabind2.13.1jackson-databind2.13.1.jar;C:Userszhouyu.m2 epositorycomfasterxmljacksoncorejack son-annotations2.13.1jackson-annotations2.13.1.jar;C:Userszhouyu.m2 epositorycomfasterxmljacksoncorejack son-core2.13.1jackson-core2.13.1.jar;C:Userszhouyu.m2 epositorycomfasterxmljacksondatatype jackson-datatype-jdk82.13.1jackson-datatype-jdk8- 2.13.1.jar;C:Userszhouyu.m2 epositorycomfasterxmljacksondatatype jackson-datatype-jsr3102.13.1jackson-datatype-jsr310- 16 2.13.1.jar;C:Userszhouyu.m2 epositorycomfasterxmljacksonmoduleja ckson-module-parameter-names2.13.1jackson-module-parameter-names2.13.1.jar;C:Userszhouyu.m2 epositoryorgspringframeworkootspring -boot-starter-tomcat3.0.0-M1spring-boot-starter-tomcat-3.0.0- M1.jar;C:Userszhouyu.m2 epositoryorgapache omcatembed omcatembed-core10.0.16 omcat-embed-core10.0.16.jar;C:Userszhouyu.m2 epositoryorgapache omcatembed omcat -embed-el10.0.16 omcat-embed-el10.0.16.jar;C:Userszhouyu.m2 epositoryorgapache omcatembed omcat -embed-websocket10.0.16 omcat-embed-websocket10.0.16.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringweb6.0.0-M2spring-web-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringbeans6.0.0-M2spring-beans-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringwebmvc6.0.0-M2spring-webmvc-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringaop6.0.0-M2spring-aop-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringcontext6.0.0-M2spring-context-6.0.0- M2.jar;C:Userszhouyu.m2 epositoryorgspringframeworkspringexpression6.0.0-M2spring-expression-6.0.0-M2.jar com.zhouyu.MyApplication . ____ _ __ _ _ /\ / ____ __ _ _(_)_ __ __ _ ( ( )\___ | _ | _| | _ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.0.0-M1) 2022-03-16 20:43:21.849 INFO 17520 --- [ main] com.zhouyu.MyApplication : Starting MyApplication using Java 17.0.1 on DESKTOP-P9KHN97 with PID 17520 (D:IdeaProjectshouyuSpringBoot-Teacher argetclasses started by zhouyu in D:IdeaProjectshouyuSpringBoot-Teacher) 2022-03-16 20:43:21.852 INFO 17520 --- [ main] com.zhouyu.MyApplication : No active profile set, falling back to default profiles: default 2022-03-16 20:43:23.257 INFO 17520 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-03-16 20:43:23.271 INFO 17520 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-03-16 20:43:23.272 INFO 17520 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.0.16] 2022-03-16 20:43:23.406 INFO 17520 --- [ main] o.a.c.c.C. [Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-03-16 20:43:23.409 INFO 17520 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1472 ms 2022-03-16 20:43:24.147 INFO 17520 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path 2022-03-16 20:43:24.164 INFO 17520 --- [ main] com.zhouyu.MyApplication : Started MyApplication in 2.934 seconds (JVM running for 3.532)
因为我们创建的是⼀个Web⼯程,从⽇志中,我们可以发现Spring Boot默认使⽤了Tomcat,并绑定了8080端⼝。
此时打开浏览器访问localhost:8080,就可以访问到我们在上⾯所定义的controller,注意配置的路径为“/”,所以直接通过localhost:8080就可以访问home⽅法。
@RequestMapping("/") String home() { return "Hello World!"; }
创建可执⾏Jar包
在实际⽣产环境中,我们是需要把应⽤程序⼯程打成⼀个Jar去运⾏的,Spring Boot也给我们提供了⼀个创建可执⾏Jar包的插件,只需要在pom.xml加上以下配置即可
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
然后再执⾏maven的package命令就可以打包了,不过⼀般建议先执⾏clean命令进⾏清空,再执⾏其他命令。
执⾏完package命令后,就会在本项⽬⼯程中的target⽂件夹出现所打出来的Jar包。
此时就可以运⾏java命令来执⾏这个Jar包
执⾏的效果就相当于运⾏main⽅法。
2、Spring Boot中的Starters
Spring Boot中的starter是Spring Boot的神器之⼀,Spring Boot提⾼了很多的starter,⽽每个starter其实就是⼀个pom.xml⽂件。
⽐如在我们项⽬的pom.xml⽂件中,我们依赖了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
这段相当于我们的⼯程依赖了spring-boot-starter-web,但是我们并没有指定具体的version,那到底依赖的是哪个版本的spring-boot-starter-web呢?
这就是由<parent>控制的。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0-M1</version> </parent>
这段代码是在给我们⾃⼰的⼯程指定了⼀个⽗⼯程,那这个⽗⼯程在哪呢?
Maven会先从本地仓库根据groupId和artifactId看是否有匹配的Jar包,如果没有就会进⾏下载,⽐如在我电脑的.m2中就已经有了
这个⽂件夹就是我们项⽬⼯程的⽗⼯程,我们的项⽬可以直接⽤⽗⼯程中所提供的。 那⽗⼯程中有什么东⻄呢?
其实就是⼀个pom⽂件。 所以,在我们的项⽬中,只要加上
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0-M1</version> </parent>
这段代码,就相当于引⼊了spring-boot-starter-parent-3.0.0-M1.pom这个pom⽂件。 在spring-boot-starter-parent-3.0.0-M1.pom中有很对内容,其中最重要的是:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.0.0-M1</version> </parent> <artifactId>spring-boot-starter-parent</artifactId> <packaging>pom</packaging> <name>spring-boot-starter-parent</name> <description>Parent pom providing dependency and plugin management for applications built with Maven</description> <properties> <java.version>17</java.version> <resource.delimiter>@</resource.delimiter> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding> </properties>
⼜指定了⼀个⽗⼯程,以及⼀些properties,⽐如java.version为17也就是,我们⾃⼰项⽬的⽗⼯程为spring-boot-starter-parent-3.0.0-M1,⽽它的⽗⼯程为springboot-dependencies
同样,我们可以在Maven仓库中找到spring-boot-dependencies
进⽽看看spring-boot-dependencies⼜是什么?
其实也就是⼀个pom⽂件,但是这个⽂件的内容就⾮常⾮常关键,因为它管理了Spring Boot默认⽀持的所有依赖以及对应的版本。
就是因为了有了这个pom⽂件,在我们⾃⼰项⽬的pom⽂件中,如果想要⽤某个依赖,只要这个依赖在spring-boot-dependencies-3.0.0-M1.pom中提供了,那你就可以不写版本,就相当于⽤的SpringBoot给你提供的版本。
回到我们的项⽬,所以我们可以不写version。
那spring-boot-starter-web这个starter⾥⾯⼜有什么呢?
⾸先我们知道,这个依赖表示,我们是⼀个Spring Boot的web⼯程,正常来说,搭建⼀个web⼯程,是要依赖很多东⻄的,⽐如tomcat,spring-web,spring-webmvc等等依赖,⽽这就是spring-bootstarter-web的作⽤,spring-boot-starter-web的作⽤就是帮我们提前把我们要开发⼀个web应⽤的依赖都写好了,我们只要依赖spring-boot-starter-web,就相当于了依赖其他很多相关的依赖。 Spring Boot真的很贴⼼。
除开web场景,还有很多其他场景也是类似的,所以Spring Boot默认提供了很多starter,具体可以看官⽹的统计:https://docs.spring.io/spring-boot/docs/3.0.0- M1/reference/html/using.html#using.build-systems.starters
值得注意的是,Spring Boot官⽅虽然提供了很多starter,但是有时可能仍然需要第三⽅来来⾃⼰实现⼀个starter并提供出来,对于这种情况,Spring Boot是有规范的,Spring Boot官⽅默认提供的starter命名格式为 spring-boot-starter-* ,第三⽅⾃⼰实现的starter的命名格式为 *-spring-boot -starter 。
3、Spring Boot中的配置类
在Spring中,我们可以使⽤XML的⽅式来对Spring进⾏配置,也可以通过Java Config(也就是类+注解)的⽅式进⾏配置,在Spring Boot中也是⼀样的。
我们可以通过@ImportResource注解来导⼊⼀个XML⽂件作为Spring的配置⽂件
@EnableAutoConfiguration @ImportResource("spring.xml") public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
@RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/") String home() { return userService.test(); } }
public class UserService { public String test() { return "hello world, nice"; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userService" class="com.zhouyu.service.UserService" /> <bean id="userController" class="com.zhouyu.controller.UserController" /> </beans>
我们也可以通过@Import+@Configuration+@Bean来进⾏等价替换掉XML的形式
@Configuration public class AppConfig { @Bean public UserService userService(){ return new UserService(); } @Bean public UserController userController(){ return new UserController(); } }
@Import(AppConfig.class) @EnableAutoConfiguration public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
当然,我们可以直接让MyApplication成为⼀个配置类,这样就不⽤额外添加⼀个AppConfig类了。
@EnableAutoConfiguration @Configuration public class MyApplication { @Bean public UserService userService(){ return new UserService(); } @Bean public UserController userController(){ return new UserController(); } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
配置类的作⽤除开可以通过@Bean来定义Bean之外,也可以配置扫描路径,⽐如:
@EnableAutoConfiguration @Configuration @ComponentScan("com.zhouyu") public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } 1
这样就不需要⽤@Bean了,但是得在类上加上@Component注解来定义Bean
扫描过程中,除开可以扫描到@Component、@Service、@Controller、@RestController等注解之外,也能扫描到@Configuration
也就是我们可以在扫描路径下定义其他的配置类。
另外,由于MyApplication类所在包就是com.zhouyu,所以我们可以直接这么写:
@EnableAutoConfiguration @Configuration @ComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
此时扫描路径就是@ComponentScan注解所在的类的包路径。 此时MyApplication就存在三个注解: @EnableAutoConfiguration @Configuration @ComponentScan
在Spring Boot中,提供了⼀个注解来替代这三个注解,这个注解就是@SpringBootApplication 所以代码就可以改成
@SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
所以,这个经典的写法,它表示了什么意思呢 1. 定义了MyApplication类是⼀个配置类 2. 定义了扫描路径,就是MyApplication所在的包路径 3. 加了@EnableAutoConfiguration,那这个注解⼜表示什么意思呢?翻译⼀下就是开启⾃动配置
接下来就来看看什么是⾃动配置
4、Spring Boot中的⾃动配置
Spring Boot⾃动配置会根据项⽬中所添加的依赖进⾏⾃动配置,⽐如我们项⽬中添加了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
⽽这个依赖中,间接添加了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>3.0.0-M1</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>3.0.0-M1</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>6.0.0-M2</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.0.0-M2</version> <scope>compile</scope> </dependency>
我们知道,我们在搭建Spring MVC⼯程时,除开要假如spring-web,spring-webmvc等依赖包之外,最复杂的就是还要进⾏很多额外的配置
那在Spring Boot中,这些配置在哪呢?
注意,在我们项⽬中引⼊的spring-boot-starter-web中,引⼊了spring-boot-starter,⽽这个⾥⾯⼜ 引⼊了spring-boot-autoconfigure
在spring-boot-autoconfigure依赖中存在⼀个⽂件spring.factories,这个⽂件中记录了各种各样的 *****AutoConfiguration类,这些⾃动配置类(其实就是配置类)就是⽤来进⾏⾃动配置的。
那这个spring.factories⽂件中所记录的⾃动配置类,是什么时候⽣效的呢,这就是 @EnableAutoConfiguration注解的作⽤,只有加了这个注解,那这些⾃动配置类才会⽣效,因为 @EnableAutoConfiguration注解会去寻找spring.factories⽂件,并解析内容,所以能解析出来⾃动配置类,并进⼀步对配置类进⾏解析。
⽐如在spring.factories⽂件中存在⼀个DispatcherServletAutoConfiguration,很明显是⽤来对 DispatcherServlet进⾏⾃动配置的,具体的细节,我们暂时就不深⼊了,本节课只需⼤体理解⾃动配置的作⽤。
⾃动配置并不是去帮助我们配置扫描路径之类的,⽽是针对各种各样的场景,Spring Boot已经给我们配置好了本来是我们需要配置的⼀些Bean以及⼀些参数。
5、Spring Boot中的条件注解
只⾃动配置类中,通常能看到很多条件注解(⽐如@ConditionalOnClass、@ConditionalOnBean),这是因为,如果我们要⽤Spring Boot的⾃动配置功能,就会加上@EnableAutoConfiguration注解,从⽽就会将解析spring.factories⽂件中的所有⾃动配置类,但是在⼀个项⽬中并不是所有⾃动配置类都要使⽤到。
⽐如我不需要⽤到MVC,那么WebMvcAutoConfiguration就没什么⽤,⽐如我不⽤Jta,那 JtaAutoConfiguration也就没什么⽤,没⽤的配置类,就不要让Spring去解析它。
条件注解就⽤来实现这种控制的。
⽐如WebMvcAutoConfiguration上就有
@ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
-
@ConditionalOnWebApplication(type = Type.SERVLET),表示WebMvcAutoConfiguration只有在应⽤类型是SERVLET类型是才⽣效 @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}),表示只有在项⽬以及依赖中存在这三个类时,WebMvcAutoConfiguration才⽣效 @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}),表示只有Spring容器中没有WebMvcConfigurationSupport.class类型的Bean时,WebMvcAutoConfiguration才⽣效
所以总结⼀下,WebMvcAutoConfiguration⽣效的条件是:当前应⽤类型是SERVLET类型的Web项⽬,并且项⽬的classpath中存在Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class三个,并且项⽬的Spring容器中没有WebMvcConfigurationSupport.class类型的Bean。
我们现在并不需要去理解为什么是这么⼀个条件,我们先来看看Spring Boot中到底有多少种这些条件注解,分别表示什么意思。
1. ConditionalOnBean:是否存在某个某类或某个名字的Bean 2. ConditionalOnMissingBean:是否缺失某个某类或某个名字的Bean 3. ConditionalOnSingleCandidate:是否符合指定类型的Bean只有⼀个 4. ConditionalOnClass:是否存在某个类 5. ConditionalOnMissingClass:是否缺失某个类 6. ConditionalOnExpression:指定的表达式返回的是true还是false 7. ConditionalOnJava:判断Java版本 8. ConditionalOnJndi:JNDI指定的资源是否存在 9. ConditionalOnWebApplication:当前应⽤是⼀个Web应⽤ 10. ConditionalOnNotWebApplication:当前应⽤不是⼀个Web应⽤ 11. ConditionalOnProperty:Environment中是否存在某个属性 12. ConditionalOnResource:指定的资源是否存在 13. ConditionalOnWarDeployment:当前项⽬是不是以War包部署的⽅式运⾏ 14. ConditionalOnCloudPlatform:是不是在某个云平台上
6、Spring Boot中的spring.factories
在我们⾃⼰的项⽬中,我们⾃⼰定义的配置类,我们⾃然可以扫描到它,但是如果某个配置类不在我们的扫描范围内,该怎么办呢?
此时就可以利⽤spring.factories机制,可以在spring.factories中指定想添加的配置类,并可以使⽤
-
@AutoConfigureAfter(BppConfig.class):表示本配置类要在BppConfig解析之后才解析 @AutoConfigureBefore(BppConfig.class):表示本配置类要在BppConfig解析之前才解析 @AutoConfigureOrder(1):直接⽤数字定义顺序
@Configuration @AutoConfigureOrder(2) public class AppConfig { @Bean public OrderService orderService(){ return new OrderService(); } }
@Configuration @AutoConfigureOrder(2) public class AppConfig { @Bean public OrderService orderService(){ return new OrderService(); } }
META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.zhouyu.AppConfig, com.zhouyu.BppConfig
注意,上⾯的上个能排序的注解,只能针对spring.factories中的配置类进⾏排序,如果配置类是我们⾃⼰扫描出来的,则这三个注解⽆效,⽽且如果是扫描出来的使⽤@Order或者Ordered接⼝来排序也是没有⽤的,因为Spring的扫描中根本就不会进⾏排序。
7、Spring Boot中的属性绑定
我们可以使⽤@Value("${xxx}")的⽅式来获取properties中的属性值。
如果properties⽂件的名字是application.properties,那就不需要⽤@PropertySource注解,如果不 是,就需要@PropertySource("zhouyu.properties")
在Spring Boot提供了⼀种更⽅便的⽅式来获取properties⽂件中的属性值。
⽐如我们⽤@Value,在UserService得写⼀遍所有的@Value,可能在其他Service也得写⼀遍
@Component public class UserService { @Value("${username}") private String username; @Value("${password}") private String password; public String test() { return username + ":" + password; } }
现在⽤Spring Boot,我们可以
@ConfigurationProperties @Component public class MyProperties { private String username; private String password; // setter getter }
在UserService中,把MyProperties当作⼀个Bean⽤即可
@Component public class UserService { @Autowired private MyProperties myProperties; public String test() { return myProperties.getUsername() + ":" + myProperties.getPassword(); } }
在Spring Boot⾃身的源码中,通常不会在MyProperties上加@Component,⽽是在配置类上加上 @EnableConfigurationProperties(MyProperties.class)
@Configuration @ConditionalOnClass(MyApplication.class) @EnableConfigurationProperties(MyProperties.class) public class AppConfig { }
这样,就可以控制只有在符合指定的条件时,才会使得MyProperties成为Bean 另外还可以使⽤@ConfigurationPropertiesScan来进⾏扫描
@SpringBootApplication @ConfigurationPropertiesScan("com.zhouyu.service") public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
这样,照样可以是MyProperties⽣效。 有时,如果我们想利⽤我们⾃⼰的properties来构造第三⽅提供的Bean,那就可以利⽤ @Bean+@ConfigurationProperties
8、Spring Boot中的外部配置
Spring Boot虽然会⾃动给我们做⼀些配置,当有些配置肯定是得我们⾃⼰来配的,⽐如数据库的连接地 址,⽤户名,密码等。 我们可以通过Java properties files, YAML files, environment variables, and command-line arguments来进⾏配置。 application.properties中配置
password=zhouyu123456
UserService类是这么写:
@Component @ConfigurationProperties public class UserService { private String password; public String test() { return password; } // setter getter }
最后userName属性就为zhouyu。
删掉application.properties⽂件,新建application.yml
password: zhouyu123456yml
重启项⽬,得到的是zhouyu123456yml
如果同时存在application.properties和application.yml,application.properties的优先级更⾼
假如在JVM环境变量中配置,如果是通过java命令运⾏,就是-Dpassword=zhouyu123456jvm
最后password取到的值为JVM环境变量中的值 我们在操作系统的环境变量中设置,设置完IDEA重启才能⽣效
最后结果仍然是JVM环境变量中的值,那如果把JVM中的环境变量中配置的删除掉,就能获取掉操作系统环境变量中所配置的值了
所以,JVM环境变量优先级 > 操作系统环境变量 > application.properties > application.yml
还可以通过命令⾏参数来设置值
相当于:java -jar ZhouyuSpringBoot-Teacher-1.0-SNAPSHOT.jar --password=1111
这种⽅式的优先级⾼于JVM环境变量优先级
所以 命令⾏参数 > VM环境变量优先级 > 操作系统环境变量 > application.properties > application.yml
除了这⼏种配置参数之外,还有很多其他⽅式,优先级从低到⾼为: 1. SpringBoot默认值,通过SpringApplication.setDefaultProperties所设置的 2. 在@Configuration配置类上通过@PropertySource注解引⼊的properties⽂件,注意在Spring容 器刷新之前这种配置是不会⽣效的,所以通过这种⽅式所配置的loggin.*,spring.main.*的配 置项在容器启动过程中是不会⽣效的。 3. Config data a. 同⼀个Jar包内的application.properties和YAML b. 同⼀个Jar包内的application-{profile}.properties and YAML c. jar包外的application.properties and YAML d. jar包外的application-{profile}.properties and YAML 4. 操作系统环境变量 5. JVM环境变量
6. ServletContext初始化参数 7. ServletConfig初始化参数 8. 嵌⼊在环境变量中的⾏内JSON,⽐如Dspring.application.json={"password":"zhouyu123456json"}
9. 命令⾏参数 10. 测试环境 11. Devtools全局设置
对于application.properties and application.yaml⽂件,Spring Boot会(优先级从低到⾼): 1. 先从classpath下找 a. classpath根⽬录 b. classpath下的config⽬录 2. 应⽤启动时的当前⽬录 a. 当前⽬录 b. 当前⽬录下的/config⼦⽬录 c. /config⼦⽬录下的⼦⽬录(这个⼦⽬录名字随便叫,只要⾥⾯有application.properties and application.yaml⽂件就可以)
默认会找名字叫做application的⽂件,我们可以通过--spring.config.name=myproject换⼀个名字
9、Spring Boot中的Profiles
Spring Boot⽀持某个Bean、某个配置类、某个@ConfigurationProperties在某个特定环境下才⽣效。
⽐如
@SpringBootApplication public class MyApplication { @Bean @Profile("dev") public UserService userServiceDev(){ return new UserService("zhouyudev"); } @Bean @Profile("prod") public UserService userServiceProd(){ return new UserService("zhouyuprod"); } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
public class UserService { private String password; public UserService(String password) { this.password = password; } public String test() { return password; } }
application.properties:
spring.profiles.active=prod
此时userServiceProd⽣效。 我们可以通过其他各种⽅式来配置spring.profiles.active。 我们还可以
@ConfigurationProperties @Profile("dev") @Component public class UserService { private String password; public String test() { return password; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
application.properties:
password=zhouyu
application-dev.properties:
password=zhouyudev
application-prod.properties:
password=zhouyuprod
通过命令⾏的⽅式指定profile:
我们可以在properties⽂件中定义⼀个profile groups
spring.profiles.group.production[0]=proddb spring.profiles.group.production[1]=prodmq
这样可以通过--spring.profiles.active=production,来激活proddb和prodmq这两种profile
10、Spring Boot中的⽇志
日志格式
默认的⽇志格式为
2022-03-21 14:09:30.465 INFO 15156 --- [ main] com.zhouyu.MyApplication : No active profile set, falling back to default profiles: default 2022-03-21 14:09:32.133 INFO 15156 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-03-21 14:09:32.141 INFO 15156 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-03-21 14:09:32.141 INFO 15156 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.0.16] 2022-03-21 14:09:32.251 INFO 15156 --- [ main] o.a.c.c.C. [Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-03-21 14:09:32.251 INFO 15156 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1717 ms 2022-03-21 14:09:32.834 INFO 15156 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path 2022-03-21 14:09:32.834 INFO 15156 --- [ main] com.zhouyu.MyApplication : Started MyApplication in 2.917 seconds (JVM running for 3.373)
-
⽇期和时间:精确到毫秒 ⽇志级别:ERROR, WARN, INFO, DEBUG, or TRACE 进程ID --- 分隔符 线程名 Logger名:通常就是类名(经常是缩写) ⽇志消息
控制台打印
默认⽇志配置在会将消息回显到控制台。默认情况下,会记录 ERROR 级别、WARN 级别和 INFO 级别的消息。可以通过使⽤ --debug 在启动应⽤程序时启⽤调试模式。
也可以在application.properties配置debug=true
调试模式下会打印更多的信息。
文件输出
默认情况下Spring Boot只会把⽇志输出到控制台,不会写⼊到⽂件中。我们可以在 application.properties中配置logging.file.name或logging.file.path指定⽇志⽂件路径,从⽽可以额外 的将⽇志写⼊⽂件中。
⽇志⽂件达到10MB时就会rotate,默认会记录ERROR、WARN、INFO级别的⽇志。
11、Spring Boot中的异步和任务调度
如果我们没有配置⼀个Executor的Bean,Spring Boot会默认给我们配置⼀个具有合理默认值的 ThreadPoolTaskExecutor,⽤来在@EnableAsync时进⾏异步执⾏。
默认线程池会使⽤8个核⼼线程,并可以根据负载增⻓和收缩,可以通过spring.task.execution.*进⾏配置,⽐如:
spring.task.execution.pool.max-size=16 spring.task.execution.pool.queue-capacity=100 spring.task.execution.pool.keep-alive=10s
默认情况下在@EnableScheduling时会配置⼀个ThreadPoolTaskScheduler,这个线程池默认只会有⼀个线程,可以通过spring.task.scheduling*来进⾏配置,⽐如:
spring.task.scheduling.thread-name-prefix=schedulingspring.task.scheduling.pool.size=2
@Component public class UserService { private static Log log = LogFactory.getLog(UserService.class); @Async public void test() { log.info("test"); } }
debug=true spring.task.execution.threadNamePrefix=zhouyu
可以利⽤TaskExecutorBuilder来快速的⾃定义⼀个线程池,利⽤部分默认值。
@Bean public ThreadPoolTaskExecutor taskExecutor(TaskExecutorBuilder builder){ ThreadPoolTaskExecutor taskExecutor = builder.build(); taskExecutor.setThreadNamePrefix("zhouyu123--"); return taskExecutor; }
12、Spring Boot中的SpringApplicationRunListener
SpringApplicationRunListener
public interface SpringApplicationRunListener { /** * 应⽤刚启动时调⽤. */ default void starting() { } /** * Environment准备好之后调⽤ */ default void environmentPrepared(ConfigurableEnvironment environment) { } /** * 创建完ApplicationContext对象后调⽤ */ default void contextPrepared(ConfigurableApplicationContext context) { } /** * 将Main类注册为BeanDefinition之后调⽤ */ default void contextLoaded(ConfigurableApplicationContext context) { } /** * ApplicationContext完成了刷新,应⽤已经启动,但是CommandLineRunner和 ApplicationRunner还没调⽤ */ default void started(ConfigurableApplicationContext context) { } /** * CommandLineRunner和ApplicationRunner调⽤完 */ default void running(ConfigurableApplicationContext context) { } /** * 容器启动出现异常后调⽤ */ default void failed(ConfigurableApplicationContext context, Throwable exception) { } }
13、ApplicationRunner和CommandLineRunner
这两个接⼝只有定义上有点区别,在功能和执⾏时机上都⼀样。
public interface CommandLineRunner { void run(String... args) throws Exception; }
public interface ApplicationRunner { void run(ApplicationArguments args) throws Exception; }
可以发现,区别在于run⽅法所接收的⼊参不同,所以这⾥的重点就是ApplicationArguments表示什么。
定义⼀个ZhouyuApplicationRunner:
@Component public class ZhouyuApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("nonOptionArgs: " + args.getNonOptionArgs()); System.out.println("optionNames: " + args.getOptionNames()); for (String sourceArg : args.getSourceArgs()) { System.out.println("sourceArg: " + sourceArg); } } }
启动Spring Boot时,增加⼀下参数
@SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, "--k1=v1", "--k2=v2", "t3", "t4"); } }
runner中打印的结果为:
nonOptionArgs: [t3, t4] optionNames: [k1, k2] sourceArg: --k1=v1 sourceArg: --k2=v2 sourceArg: t3 sourceArg: t4
可以发现,加了"--"的都是option, 没有加的就是nonOption,sourceArg就是原始的参数。⽽ CommandLineRunner中接收的就是sourceArg
14、Spring Boot中的@WebServlet注解
我们知道,在⼀个Java Web应⽤中,我们可以通过@WebServlet来定义⼀个Servlet,Tomcat会负责扫描项⽬中哪些类上⾯添加了@WebServlet,从⽽把相对应的Servlet对象添加到Tomcat中去。
但是在Spring Boot中,我们除开照样可以通过@WebServlet来定义⼀个Servlet之外,还需要额外在配置类上添加⼀个@ServletComponentScan注解,顾名思义,这个注解是负责去扫描Servlet的。
也就是在⽤Spring Boot时,@WebServlet的扫描⼯作从Tomcat中转移到了Spring Boot。
那底层是如何⼯作的呢?
ServletRegistrationBean
⾸先我们得学习⼀下ServletRegistrationBean,很明显,这个是⼀个Bean,这个Bean可以⽤来注册Servlet。
我们可以通过BeanDefinition来定义⼀个ServletRegistrationBean,并指定我们想要注册的Servlet,⽐如:
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ServletRegistrationBean.class); builder.addPropertyValue("initParameters", servlet的初始化参数); builder.addPropertyValue("loadOnStartup", loadOnStartup的值); builder.addPropertyValue("name", servlet的名字); builder.addPropertyValue("servlet", servlet对象); builder.addPropertyValue("urlMappings", servlet的urlMappings); registry.registerBeanDefinition(name, builder.getBeanDefinition()); // 把 BeanDefinition注册到Spring容器中
那ServletRegistrationBean是如何⼯作的呢?
ServletRegistrationBean继承了ServletContextInitializer接⼝,⽽ServletContextInitializer是Spring Boot所提供的⼀个函数式接⼝。
在Spring Boot启动流程中,会⽣成⼀个TomcatStarter对象,TomcatStarter实现了 ServletContainerInitializer接⼝,注意,这个接⼝是Servlet规范中所定义的接⼝。
-
ServletContextInitializer:这是Spring Boot定义的 ServletContainerInitializer:这是Servlet规范定义的
并且Tomcat在启动之前会把TomcatStarter添加到Tomcat中去,从⽽Tomcat在启动过程中,就会调⽤ServletContainerInitializer接⼝的onStartup()⽅法,也就是会执⾏TomcatStarter的onStartup()⽅法。
⽽TomcatStarter的onStartup()⽅法会从Spring容器中找到所有的ServletContextInitializer类型的 Bean,然后循环执⾏ServletContextInitializer的onStartup(ServletContext servletContext)⽅法,从 ⽽就会执⾏到某个ServletRegistrationBean的onStartup()。
⽽ServletRegistrationBean的onStartup⽅法就会把⾃⼰所拥有的Servlet对象添加到ServletContext中去,从⽽完成了Servlet的添加的。
@ServletComponentScan
⽽@ServletComponentScan注解的作⽤就是去指定路径下扫描@WebServlet,扫描到了⽣成 ServletRegistrationBean。
⽽核⼼是会使⽤⼀个ClassPathScanningCandidateComponentProvider来进⾏扫描,这个类并不是Spring Boot中的,⽽是Spring Framework中的,Spring Boot只是利⽤它,当然Spring Boot会做⼀些设置,⽐如向ClassPathScanningCandidateComponentProvider中添加IncludeFilter。
⽐如会添加三个IncludeFilter: 1. new AnnotationTypeFilter(WebServlet.class); 2. new AnnotationTypeFilter(WebFilter.class); 3. new AnnotationTypeFilter(WebListener.class);
就是有了这三个Filter,扫描时才能扫描到这三个注解,并⽣成对应的BeanDefinition,⽐如: 1. @WebServlet对应ServletRegistrationBean 2. @WebFilter对应FilterRegistrationBean 3. @WebListener对应ServletListenerRegistrationBean
总结
Spring Boot启动时: 1. 先扫描@WebServlet,⽣成ServletRegistrationBean 2. 向Tomcat中添加TomcatStarter 3. 启动Tomcat时执⾏TomcatStarter的onStartup() 4. 从⽽执⾏ServletRegistrationBean的onStartup(ServletContext servletContext) 5. 从⽽将Servlet对象注册到Tomcat中