1. Spring Boot概述

1.1. 为什么使用Spring Boot?

随着动态语言的流行,比如groovy,scala等,java的开发显得格外的笨重,例如:繁多的配置、低下的开发效率,复杂的部署流程以及第三方集成难度大。

在上述环境下,Spring Boot应运而生。它使用“习惯优于配置”(项目中存在大量的配置,此外内置一个习惯性的配置,让你无须手动进行配置)的理念让你的项目快速运行起来。Spring Boot给我们的开发减少了众多配置,使得开发更加便捷。

1.2. Spring Boot的核心功能

1、独立运行的Spring项目

Spring Boot可以以jar包的形式独立运行,运行一个Spring Boot项目只需要通过java -jar xx.jar。

2、内置Servlet容器

Spring Boot可选择内嵌Tomcat、Jetty或者Undertow,这样无须以war包形式部署。

3、提供starter简化maven配置

Spring提供了一系列的starter pom来简化maven依赖加载,例如:当你使用了spring-boot-starter-web时,会自动加入相关依赖,无需你手动一个一个的添加坐标依赖。

4、自动配置Spring

Spring Boot会根据在类路径中的jar包、类,为jar包里的类自动配置Bean,这样会极大地减少我们要使用的配置。当然,Spring Boot只是考虑了大多数的开发场景,并不是所有场景,若在实际开发中,我们需要自动配置bean,而Spring Boot没有提供支持,则可以自定义自动配置。

5、准生产的应用监控

Spring Boot提供基于http、ssh、telnet对运行时的项目进行监控。

6、无代码生成和xml配置

Spring Boot的神奇的不是借助于代码生成来实现的,而是通过条件注解来实现的,这是Spring 4.x提供的新特性,Spring 4.x提倡使用java配置和注解配置相结合,而Spring Boot不需要任何xml配置即可实现Sping Boot的所有配置。

1.3. Spring Boot的优点缺点

优点:

l 快速构建项目

l 对主流框架的无配置集成

l 项目可独立运行,无需依赖外部Servlet容器,比如JBoss、Tomcat、Weblogic等

l 提供运行时的应用监控

l 极大地提高了开发、部署效率

l 与云计算的天然集成

缺点:

l 需要结合Spring框架

1.4. Maven手工构建

1.4.1. Maven项目构建

在eclipse或者IntelliJ IDEA集成工具下新建一个maven项目,以eclipse作为IDE演示

springboot集成flowable的RuntimeService为空 springboot:run_spring

1.4.2. 修改pom.xml

(1)添加Spring Boot的父级依赖,这样当前的项目就属于Spring Boot项目。spring-boot-starter-parent是一个特殊的starter,它用来提供相关的maven默认依赖(就如在pom.xml文件中<dependencyManagement>先声明依赖坐标,在子项目中就可以直接不需要通过version来下载依赖包),使用它之后,常用的包依赖可以省去version标签。

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.4.2.RELEASE</version>
</parent>



(2)dependencies添加Web支持的starter pom

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.4.2.RELEASE</version>
</parent>

(3)添加Spring Boot的编译插件

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>



1.5. 简单Spring Boot使用

springboot集成flowable的RuntimeService为空 springboot:run_spring_02



2. Spring Boot核心


2.1. 基本配置

2.1.1. 入口类和@SpringBootApplication

Spring Boot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法。在main方法中使用SpringApplication.run(Object source, args)启动Spring Boot项目。

@SpringBootApplication是Spring Boot的核心注解,它是一个组合注解,如下:

springboot集成flowable的RuntimeService为空 springboot:run_spring_03


@SpringBootApplication注解组合了@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan;若不使用@SpringBootApplicatio注解,则可以在入口类上直接使用@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan。

其中,@EnableAutoConfiguration用于让Spring Boot根据类路径中的jar包依赖当前项目进行自动配置。例如,添加了spring-boot-starter-web依赖,会自动添加tomcat和Spring MVC的依赖,那么Spring Boot会对tomcat和Spring MVC进行自动配置,@ComponentScan用于自动扫描所有Spring组件。

2.1.2. 关闭特定的自动配置

关闭特定的自动配置可以使用@SpringBootApplication注解的exclude参数,例如:


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

也可以通过EnableAutoConfiguration进行关闭

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})



2.1.3. 定制Banner

1.修改Banner

(1)在Spring Boot启动的时候会有一个默认启动图案,如下图所示:



springboot集成flowable的RuntimeService为空 springboot:run_spring_04


(2)我们在src/main/resources下新建一个banner.txt

(3)在banner.txt中自定义banner

springboot集成flowable的RuntimeService为空 springboot:run_jar包_05

2.关闭banner

(1)main里的内容修改为:

SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);

(2)或使用fluent API,修改为:


new SpringApplicationBuilder(Application.class).bannerMode(Mode.OFF).run(args);



2.1.4. Spring Boot配置文件

Spring Boot使用一个全局的配置文件application.properties或application.yml,放置在src/main/resources目录或者类路径的config下。


springboot集成flowable的RuntimeService为空 springboot:run_spring_06



Spring Boot不仅支持常规的properties文件,还支持yaml语言的配置文件。Yaml是以数据为中心的语言,在配置数据的时候具有面向对象的特征。

Spring Boot的全局配置文件的作用是对一些默认配置的配置值进行修改。

1.简单示例

将tomcat的默认端口号8080修改成9090,并将默认的访问路径/修改为/helloboot,可以在application.properties中添加:

server.port=9090
server.context-path=/helloboot



或在application.yml文件中添加:

server:

   port:9090

   contextPath:/helloboot



2.1.5. starter pom

Spring Boot为我们提供了简化企业级开发绝大多数场景的starter pom,只要使用了应用场景所需要的starter pom,相关的技术配置将会消除,就可以得到Spring Boot为我们提供的自动配置的Bean

名称

描述

spring-boot-starter

Spring Boot核心starter,包含自动配置、日志、yaml配置文件的支持

spring-boot-starter-thymeleaf

对Thymeleaf模板引擎,包含于Spring整合的配置

spring-boot-starter-ws

对Spring web Services支持

spring-boot-starter-data-couchbase

对文档数据或者Spring data支持

spring-boot-starter-artemis

 

众多starter pom详见链接地址:

http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-external-config-random-values


2.2. 外部配置

Spring Boot允许使用properties文件,yaml文件或者命令行参数作为外部配置

2.2.1. 命令行参数配置

Spring Boot可以是基于jar包运行,打成jar包的程序可以直接通过下面命令运行:

java -jar xx.jar

可以通过以下命令修改tomcat端口号:


java -jar xx.jar --server.port=9090



2.2.2. 常规属性配置

在常规Spring环境下,注入properties文件里的值方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入值。在Spring Boot里,我们只需要在application.properties定义属性,直接使用@Value注入即可。

(1)在application.properties中添加如下属性:

book.author=zhangsan
book.name=spring boot



(2)注入值:

springboot集成flowable的RuntimeService为空 springboot:run_spring_07

2.2.3. 类型安全配置

上例中使用@Value注入每个配置在实际项目中会显得格外麻烦,因为我们的配置通常会是许多个,若用上例的方式要使用@Value很多次。

Spring Boot提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个Bean及其属性关联,从而实现类型安全的配置。

(1)添加配置,即在application.properties上添加:

author.name=lly
author.age=34



当然,我们也可以新建一个properties文件,这就需要我们在@ConfigurationProperties注解中添加属性locations。

(2)类型安全的类,如下:


springboot集成flowable的RuntimeService为空 springboot:run_jar包_08

2.3. 日志配置

Spring Boot支持java Util Logging、log4j、log4j2和logback作为日志框架,无论使用哪种日志框架,Spring Boot已为当前使用日志框架的控制台输出及文件输出路径做好了配置。

默认情况下,Spring Boot使用logback作为日志框架。

1)在application.properties文件中配置日志级别,格式logging.level.包名=级别:

logging.level.org.springframework.web=DEBUG

2)配置跟日志记录器级别:

logging.level.root=WARN

不仅可以修改上述这些,还能修改日志文件路径,日志输出格式等。

2.4. Profile配置

Profile是Spring用来针对不同环境对不同配置提供支持的,全局Profile配置使用application-{profile}.properties,通过在application.properties中设置spring.profile.active=prod来指定活动的Profile

下面做一个简单的演示:

(1)如我们分为生产(prod)和开发环境(dev),生产环境下端口号为80,开发环境为9090

(2)生产和开发环境下的配置文件如下:


springboot集成flowable的RuntimeService为空 springboot:run_spring_09

application-prod.properties

server.port=80

application-dev.properties

server.port=9090



(3)运行

①application.properties配置为:spring.profiles.active=dev时

springboot集成flowable的RuntimeService为空 springboot:run_tomcat_10

②application.properties配置为:spring.profiles.active=prod时:



springboot集成flowable的RuntimeService为空 springboot:run_jar包_11

3. 内置Servlet容器配置

Spring Boot默认的内嵌tomcat为Servlet容器。

3.1. 配置访问日志

配置Tomcat或者undertow via的访问日志,在application.properties中做配置即可。

①例如,下面就是tomcat的日志访问配置

server.tomcat.basedir=my-tomcat
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)



②undertow via可以以tomcat类似的方式配置,如下:

server.undertow.accesslog.enabled=true
server.undertow.accesslog.pattern=%t %a "%r" %s (%D ms)



如果你想使用规定版本的tomcat,可以在maven 的pom文件中添加如下依赖即可:



<properties>
    <tomcat.version>7.0.59</tomcat.version>
</properties>
<dependencies>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-juli</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    ...
</dependencies>

3.3. 使用jetty或者undertow替换tomcat

在spring-boot-starter-web中默认使用的是Tomcat,如果你希望使用jetty或者undertow,你可以把pom文件修改成如下这样:

1)使用jetty替换tomcat:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>



2)使用undertow替换tomcat配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>