Spring boot 大大简化了Spring,用Spring MVC之前的架构,用了Spring Boot那真的是不可同日而语,一个词 :简单,主要是依靠Spring Boot的起步依赖和自动装配两个核心的原理。
起步依赖
spring boot之所以能大大简化了开发的难度,很大一部分便是起步依赖。起步边将所有需要的依赖加入。
本质就是Maven的对象模型–依赖传递。使用 spring-boot-starter-* 开头。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
点进去之后,会发现在引入了很多包。这样包之间也不会有版本冲突便于版本控制。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>2.4.5</version>
</dependency>
自动装配
Spring Boot还有一个特性就是自动装配,减少了很多不必要的配置,方便一些参数的配置。xml文件配置几乎没有。
使用application、bootstrap的properties以及yml就可以配置自己的参数。
启动类的 @SpringBootApplication 中 @EnableAutoConfiguration属性会将在配置文件中配置的属性配置在启动的参数中。
配置文件的属性会转化在实体类中。比如server.port配置的属性最终会转化在ServerProperties这个类中。
server.port=8088
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
/**
* Server HTTP port.
*/
private Integer port;