1.简介
SpringBoot是一个脚手架,为了快速搭建spring项目JAVAEE应运而生,因为spring配置繁琐,所以,为了使开发者快速上手spring,springboot带来了全新的自动化配置解决方案,使用SpringBoot可以快速的创建基于Spring生产级的对立应用程序。
SpringBoot中对一些常用的第三方库提供了默认的自动化配置方案,使得开发者只需要很少的spring配置就能运行一个完整的javaee应用
2.使用SpringBoot的优势
1.提供了一个快速的Spring项目搭建渠道
2.开箱即用,很少的Spring配置就能运行一个JavaEE项目
3.提供了生产级的服务监控方案
4.内嵌服务器,可以快速部署
5.提供了一系列非功能性的通用配置
6.纯java配置,没有代码生成,也不需要XML配置
3.相关解释
GroupId: 组织id(项目的包名)
ArtifactId: (项目名称或模块名称)
4.项目构建
4.1:添加依赖
4.1.1:首先添加spring-boot-starter-parent依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
spring-boot-starter-parent 是一 特殊的 starter 提供了一些 maven 默认配置 ,同时还提供了 ependency-management ,可 以便开发者在引入其他依赖时不必输入版本号,方便依赖管理。 Spring oot 中提供的 Starter 非常 ,这些 Starter 要为第三方库提供自动配置,例如要开发一 Web 项目,就可以先引入 Web Starter ,代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4.1.2:我们可以追踪一下spring-boot-starter-web的源码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<artifactId>spring-boot-starter-web</artifactId>
<name>Spring Boot Web Starter</name>
<description>Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
</project>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<artifactId>spring-boot-starter-tomcat</artifactId>
<name>Spring Boot Tomcat Starter</name>
<description>Starter for using Tomcat as the embedded servlet container. Default
servlet container starter used by spring-boot-starter-web</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</dependency>
</dependencies>
</project>
我们可以看出,web启动器里面已经封装了mysql,web,MVC等依赖,根据这些依赖就做一些相关的配置,所以自动化配置和这些启动器相关
4.2:编写启动类
接下来创建项目的入口类,在 Maven 工程的 java 目录下创建项目的包,创建 启动类
@EnableAutoConfiguration
public class App {
public static void main(String[] args){
SpringApplication.run(ApiPosRun.class, args);
}
}
@EnableAutoConfiguration 注解表示开启自动化配置, 由于项目中 加了 spring-boot-starterweb 依赖 因此在开启了自动化配置之后会自动进行 Spring SpringMVC 的配置; Java 项目的 main 方法中,通过 SpringApplication 中的 方法启动项目 一个参数传入 App.class ,告诉 Spring 哪个是主要组件。第二个参数是运行时输入的其他参数
接下来创建 SpringMVC 中的控制器一-HelloContro ller ,代码如下:
@RestController
public class HelloController {
@GetMapping ("/hello")
public String hello () {
return "hello spring boot !”;
}
}
在控制器中提供了 个“ hello ”接口,此时需要配置包扫描才能将 Hello Controller 注册到 Spring MVC 中,因此在 App 类上面再添 个注解@ComponentScan 进行包扫描,代码如下:
@EnableAutoConfiguration
@ComponentScan
public class App {
public static void main(String[] args){
SpringApplication.run(ApiPosRun.class, args);
}
}
也可以直接使用组合注解@SpringBootApplication 来代替@EnableAutoConfiguration @ComponentScan 代码如下
@SpringBootApplication
public class GunsApplication {
private final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);
public static void main(String[] args) {
SpringApplication.run(GunsApplication.class, args);
logger.info("GunsApplication is success!");
}
}
我们可以看一下他的源码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "exclude"
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "excludeName"
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
可以看到底层已经进行了封装