1、使用背景
首先说下我们为什么使用springboot,原因有以下几点
1、快速创建独立运行的spring项目以及与主流框架继承
2、使用嵌入式的Servlet容器,无需打成war包
3、starters自动依赖于版本控制
4、大量的自动配置,简化开发,也可修改默认值
5、无需配置xml,无代码生成,开箱即用
6、生产环境的运行时应用监控,配合springcloud全家桶
7、与云计算天然继承
缺点
入门容易,精通难,对spring的掌握要求较高
2、创建过程
第一步:
第二步
第三步
第四步
最后一步
创建完成之后等待maven加载依赖完成。
3、基本配置说明
pom.xml依赖说明
<?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>
<!--springboot默认内嵌配置,如果你有其他的parent继承,可以不使用通过下面这段来代替-->
<!--<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.swk.springboot</groupId>
<artifactId>springboot-swk</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-swk</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Add typical dependencies for a web application web应用的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Package as an executable jar 可以将应用打成一个可执行的jar,通过java -jar,下面我会演示 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们跟踪一下parent的依赖会发现他管理springboot所有依赖的配置与版本
<properties>
<activemq.version>5.15.8</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.68</appengine-sdk.version>
<artemis.version>2.6.3</artemis.version>
<aspectj.version>1.9.2</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.9.5</byte-buddy.version>
<caffeine.version>2.6.2</caffeine.version>
<cassandra-driver.version>3.6.0</cassandra-driver.version>
<classmate.version>1.4.0</classmate.version>
<commons-codec.version>1.11</commons-codec.version>
<commons-dbcp2.version>2.5.0</commons-dbcp2.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.6.0</commons-pool2.version>
<couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>
<couchbase-client.version>2.7.1</couchbase-client.version>
<derby.version>10.14.2.0</derby.version>
<dom4j.version>1.6.1</dom4j.version>
<dropwizard-metrics.version>4.0.3</dropwizard-metrics.version>
<ehcache.version>2.10.6</ehcache.version>
<ehcache3.version>3.6.2</ehcache3.version>
<elasticsearch.version>6.4.3</elasticsearch.version>
<embedded-mongo.version>2.1.1</embedded-mongo.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<flatten-maven-plugin.version>1.0.1</flatten-maven-plugin.version>
<flyway.version>5.2.3</flyway.version>
<freemarker.version>2.3.28</freemarker.version>
<git-commit-id-plugin.version>2.2.5</git-commit-id-plugin.version>
<glassfish-el.version>3.0.0</glassfish-el.version>
<glassfish-jaxb.version>2.3.1</glassfish-jaxb.version>
<groovy.version>2.5.4</groovy.version>
<gson.version>2.8.5</gson.version>
<h2.version>1.4.197</h2.version>
<hamcrest.version>1.3</hamcrest.version>
....
</properties>
spring-boot-starter-web帮我们导入了web模块正常运行所依赖的组件
主程序如下:
package com.swk.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootSwkApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSwkApplication.class, args);
}
}
我们跟踪一下@SpringBootApplication注解如下:
@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 {}
我们会发现springboot的自动配置都是通过@EnableAutoConfiguration来实现的,继续跟
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
他通过@AutoConfigurationPackage来自动配置包,他的底层注解是通过@Import,给容器中导入一个组件;导入的组件由
Registrar.class,将主配置类&#