Spring Boot 是一个框架,一种全新的编程规范,他的产生简化了框架的使用,所谓简化是指简化了 Spring 众多框架中所需的大量且繁琐的配置文件,所以 Spring Boot 是一个服务于框架的框架,服务范围是简化配置文件。所以从本质上来说,Spring Boot 其实就是 Spring框架的另一种表现形式。
SNAPSHOT:快照版,即开发版。
CURRENT:最新版,但是不一定是稳定版。
GA:General Availability,正式发布的版本。
官网:https://spring.io/projects/spring-boot
选择overview
往下找,在底端找到如下界面,点击Spring initializr
然后选择下面的generate进行下载
下载后把项目解压并放到合适的位置,打开idea
完成后,如果本地的maven仓库中没有对应的依赖,则会花费一段时间去下载。
下载完毕后项目结构为
创建一个无骨架的maven项目,修改pom文件
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qt</groupId>
<artifactId>springbootdemo3</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 应为springboot项目依赖于官方的父工程,因此需要添加该依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- java版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test启动器 -->
<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>
<plugins>
<!-- springboot打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Spring Boot 的父级依赖,只有继承它项目才是 Spring Boot 项目。
spring-boot-starter-parent 是一个特殊的 starter,它用来提供相关的 Maven 默认依赖。使用它之后,常用的包依赖可以省去 version 标签。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
启动器依赖,当然也可以使用第三方启动器依赖
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
spring-boot-maven-plugin 插件是将 springboot 的应用程序打包成 jar 包的插件。将所有应用启动运行所需要的 jar 包都包含进来,从逻辑上将具备了独立运行的条件。当运行"mvnpackage"进行打包后,使用"java -jar"命令就可以直接运行。
Spring Boot 的启动类的作用是启动 Spring Boot 项目,是基于 Main 方法来运行的。
注意:启动类在启动时会做注解扫描(@Controller、@Service、@Repository…),扫描位置为同包或者子包下的注解,所以启动类的位置应放于包的根下。
通过官网和IEDA的手脚架工具创建时,该类会自动生成。如果是通过修改maven项目的pom文件而创建的springBoot项目,则需要创建该类。
/**
* SpringBoot启动类
*/
@SpringBootApplication
public class Springbootdemo2Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo2Application.class, args);
}
}
Spring Boot 将所有的功能场景都抽取出来,做成一个个的 starter(启动器),只需要在项目里面引入这些 starter 相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景,在 jar 包管理上非常方便,最终实现一站式开发。
Spring Boot 提供了多达 44 个启动器。
spring-boot-starter
这是 Spring Boot 的核心启动器,包含了自动配置、日志和 YAML。
spring-boot-starter-actuator
帮助监控和管理应用。
spring-boot-starter-web
支持全栈式 Web 开发,包括 Tomcat 和 spring-webmvc。
spring-boot-starter-amqp
通过 spring-rabbit 来支持 AMQP 协议(Advanced Message Queuing Protocol)。
spring-boot-starter-aop
支持面向方面的编程即 AOP,包括 spring-aop 和 AspectJ。
spring-boot-starter-artemis
通过 Apache Artemis 支持 JMS 的 API(Java Message Service API)。
spring-boot-starter-batch
支持 Spring Batch,包括 HSQLDB 数据库。
spring-boot-starter-cache
支持 Spring 的 Cache 抽象。
spring-boot-starter-cloud-connectors
支持 Spring Cloud Connectors,简化了在像 Cloud Foundry 或 Heroku 这样的云平台上连接服务。
spring-boot-starter-data-elasticsearch
支持 ElasticSearch 搜索和分析引擎,包括 spring-data-elasticsearch。
spring-boot-starter-data-gemfire支持 GemFire 分布式数据存储,包括 spring-data-gemfire。
spring-boot-starter-data-jpa
支持 JPA(Java Persistence API),包括 spring-data-jpa、spring-orm、Hibernate。
spring-boot-starter-data-mongodb
支持 MongoDB 数据,包括 spring-data-mongodb。
spring-boot-starter-data-rest
通过 spring-data-rest-webmvc,支持通过 REST 暴露 Spring Data 数据仓库。
spring-boot-starter-data-solr
支持 Apache Solr 搜索平台,包括 spring-data-solr。
spring-boot-starter-freemarker
支持 FreeMarker 模板引擎。
spring-boot-starter-groovy-templates
支持 Groovy 模板引擎。
spring-boot-starter-hateoas
通过 spring-hateoas 支持基于 HATEOAS 的 RESTful Web 服务。
spring-boot-starter-hornetq
通过 HornetQ 支持 JMS。
spring-boot-starter-integration
支持通用的 spring-integration 模块。
spring-boot-starter-jdbc
支持 JDBC 数据库。
spring-boot-starter-jersey
支持 Jersey RESTful Web 服务框架。
spring-boot-starter-jta-atomikos
通过 Atomikos 支持 JTA 分布式事务处理。
spring-boot-starter-jta-bitronix
通过 Bitronix 支持 JTA 分布式事务处理。
spring-boot-starter-mail
支持 javax.mail 模块。
spring-boot-starter-mobile
支持 spring-mobile。
spring-boot-starter-mustache
支持 Mustache 模板引擎。
spring-boot-starter-redis
支持 Redis 键值存储数据库,包括 spring-redis。
spring-boot-starter-security
支持 spring-security。
spring-boot-starter-social-facebook
支持 spring-social-facebook
spring-boot-starter-social-linkedin
支持 pring-social-linkedin
spring-boot-starter-social-twitter
支持 pring-social-twitter
spring-boot-starter-test
支持常规的测试依赖,包括 JUnit、Hamcrest、Mockito 以及 spring-test 模块。
spring-boot-starter-thymeleaf
支持 Thymeleaf 模板引擎,包括与 Spring 的集成。
spring-boot-starter-velocity
支持 Velocity 模板引擎。
spring-boot-starter-websocket
支持 WebSocket 开发。
spring-boot-starter-ws
支持 Spring Web Services。
spring-boot-starter-actuator
增加了面向产品上线相关的功能,比如测量和监控。
spring-boot-starter-remote-shell
增加了远程 ssh shell 的支持。
spring-boot-starter-jetty
引入了 Jetty HTTP 引擎(用于替换 Tomcat)。
spring-boot-starter-log4j
支持 Log4J 日志框架。
spring-boot-starter-logging
引入了 Spring Boot 默认的日志框架 Logback。
spring-boot-starter-tomcat
引入了 Spring Boot 默认的 HTTP 引擎 Tomcat。
spring-boot-starter-undertow
引入了 Undertow HTTP 引擎(用于替换 Tomcat)。
Spring Boot 提供一个名称为 application 的全局配置文件,支持两种格式 properteis 格式与 YAML 格式。
配置 Tomcat 监听端口
server.port=8888
YAML 格式配置文件的扩展名可以是 yaml 或者 yml。
基本格式要求
如:配置 Tomcat 监听端口和host(二者都在server层下)
server:
port: 8888
host: dasfad
当前项目根目录中
当前项目根目录下的一个/config 子目录中
项目的 resources 即 classpath 根路径中
项目的 resources 即 classpath 根路径下的/config 目录中
是 SpringBoot 的启动类。此注解等同于@Configuration+@EnableAutoConfiguration+@ComponentScan 的组合。
@SpringBootConfiguration 注解是@Configuration 注解的派生注解,跟@Configuration注解的功能一致,标注这个类是一个配置类,只不过@SpringBootConfiguration 是 springboot的注解,而@Configuration 是 spring 的注解
通过对 bean 对象的操作替代 spring 中 xml 文件
Spring Boot 自动配置(auto-configuration):尝试根据你添加的 jar 依赖自动配置你的Spring 应用。是@AutoConfigurationPackage 和@Import(AutoConfigurationImportSelector.class)注解的组合。
@AutoConfigurationPackage 注解,自动注入主类下所在包下所有的加了注解的类(@Controller,@Service 等),以及配置类(@Configuration)
直接导入普通的类
导入实现了 ImportSelector 接口的类
导入实现了 ImportBeanDefinitionRegistrar 接口的类
组件扫描,可自动发现和装配一些 Bean。
@ConfigurationPropertiesScan 扫描配置属性。@EnableConfigurationProperties 注解的作
用是使用 @ConfigurationProperties 注解的类生效。