从0-1使用SpringBoot应用(版本2.X)-第三天

版权声明:如有转载-请加微信457556886告知下就可以 知识都是分享的 https://blog.csdn.net/wolf_love666/article/details/90451484

前言:

如果以任务开发为目的,则按照上篇的内容直接去开发以及根据指南去操作springboot开发就可以。如果想要参考一些开源的集成例子可以参考这个GitHub库的内容,如果不想看如何使用,那么可以直接看第四天的内容SpringBoot的特点

大纲内容:

  • 构建系统
  • 自动配置
  • 怎么运行应用
  • 构建系统
    SpringBoot推荐使用maven或者gradle,如果你喜欢使用ant等也可以。但是有时候可能会出问题哦。因为在SpringBoot不同版本的时候都会默认集成不同的第三方依赖版本*(你不想用可以指定自己的),但是更加好的支持是用SpringBoot本身自己集成的版本。
  • 1)Maven
    Maven用户可以从spring-boot-starter-parent项目继承,以获得合理的默认值。父项目提供以下功能:
  • Java 1.8作为默认编译器级别
  • utf - 8编码源
  • 依赖项管理部分,继承自spring-boot-dependencies pom,用于管理公共依赖项的版本。这种依赖关系管理允许您在自己的pom中使用这些依赖关系时省略version标记
  • 默认配置是重新打包repackage如果不需要需要去父项目中排除掉
  • 合理的资源过滤
  • 合理的插件配置(exec plugin, Git commit ID, and shade)
  • 应用程序的合理资源筛选。属性和应用程序。包括特定于概要文件的文件(例如,application-dev.properties和application-dev.yml)

注意,自从应用程序。属性和应用程序。yml文件接受Spring样式的占位符(${…}),Maven过滤改为使用@…@占位符。(可以通过设置Maven属性resource.delimiter来覆盖它)

  • 1.1)继承启动父级
    只需要配置下父级依赖
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.5.RELEASE</version>
</parent>

如果额外需要依赖可以直接导入启动器,可以忽略版本默认与父版本一致。
如果要升级另外的spring数据发布:需要添加如下代码(类型参考

<properties>
	<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
  • 1.2)不使用父pom来使用SpringBoot。
    如果你不想要用父pom,SpringBoot的parent那么可以用依赖使用SpringBoot的依赖:
<dependencyManagement>
	<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.5.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

什么时候使用呢?比如这种情况

<dependencyManagement>
	<dependencies>
		<!-- Override Spring Data release train provided by Spring Boot -->
		<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.5.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
  • 1.3)使用SpringBoot的maven插件
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>
如果使用Spring Boot starter父pom,则只需要添加插件。
不需要配置它,除非您想更改在父类中定义的设置
  • 3)Ant
    可以使用Apache Ant+Ivy构建Spring引导项目。spring-boot-antlib“AntLib”模块也可用来帮助Ant创建可执行jar
<ivy-module version="2.0">
	<info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
	<configurations>
		<conf name="compile" description="everything needed to compile this module" />
		<conf name="runtime" extends="compile" description="everything needed to run this module" />
	</configurations>
	<dependencies>
		<dependency org="org.springframework.boot" name="spring-boot-starter"
			rev="${spring-boot.version}" conf="compile" />
	</dependencies>
</ivy-module>

如下是较全的内容:

<project
	xmlns:ivy="antlib:org.apache.ivy.ant"
	xmlns:spring-boot="antlib:org.springframework.boot.ant"
	name="myapp" default="build">

	<property name="spring-boot.version" value="2.1.5.RELEASE" />

	<target name="resolve" description="--> retrieve dependencies with ivy">
		<ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
	</target>

	<target name="classpaths" depends="resolve">
		<path id="compile.classpath">
			<fileset dir="lib/compile" includes="*.jar" />
		</path>
	</target>

	<target name="init" depends="classpaths">
		<mkdir dir="build/classes" />
	</target>

	<target name="compile" depends="init" description="compile">
		<javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
	</target>

	<target name="build" depends="compile">
		<spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
			<spring-boot:lib>
				<fileset dir="lib/runtime" />
			</spring-boot:lib>
		</spring-boot:exejar>
	</target>
</project>

参考手册

  • 4)启动器
    启动器这里统一的命名规则:spring-boot-starter-*,*是应用功能的特殊类型,比如spring-boot-starter-activemq。统一命名是为了方便能够直接ctrl+空格获取启动器的信息。
    启动器也可以根据自己的需要自己制定自己的启动器,但是需要按照如下命名方法:第三方项目-spring-boot-starter(thirdpartyproject-spring-boot-starter)
    目前官方的启动器列表,以下应用程序启动程序由org.springframework下的Spring Boot提供
启动器名称 功能描述 pom文件
spring-boot-starter 核心的启动器,包括自动配置支持,日志和yaml语法支持 pom文件
spring-boot-starter-activemq 使用Apache ActiveMQ启动JMS消息传递 pom文件
spring-boot-starter-amqp 用于使用Spring AMQP和Rabbit MQ的启动程序 pom文件
spring-boot-starter-aop 使用Spring AOP和AspectJ进行面向切面编程的启动器 pom文件
spring-boot-starter-artemis 使用Apache Artemis启动JMS消息传递 pom文件
spring-boot-starter-batch spring的批处理启动器 pom文件
spring-boot-starter-cache 启动器,用于使用Spring框架的缓存支持 pom文件
spring-boot-starter-cloud-connectors 用于使用Spring云连接器的初学者,该连接器简化了与Cloud Foundry和Heroku等云平台中的服务的连接 pom文件
spring-boot-starter-data-cassandra 启动使用Cassandra分布式数据库和Spring数据Cassandra pom文件
spring-boot-starter-data-cassandra-reactive Cassandra 分布式数据库和spring数据Cassandra 的响应式编程 pom文件
spring-boot-starter-data-couchbase 面向文档数据库和spring数据库的启动器 pom文件
spring-boot-starter-data-couchbase-reactive 面向文档数据库和spring数据库的响应式编程启动器 pom文件
spring-boot-starter-data-elasticsearch 启动器用于使用Elasticsearch搜索和分析引擎以及Spring数据弹性搜索 pom文件
spring-boot-starter-data-jdbc spring数据jdbc的启动器 pom文件
spring-boot-starter-data-jpa 启动器,用于在Hibernate中使用Spring Data JPA pom文件
spring-boot-starter-data-ldap 用于使用Spring数据LDAP的启动程序 pom文件
spring-boot-starter-data-mongodb 使用MongoDB面向文档的数据库和Spring Data MongoDB的启动器 pom文件
spring-boot-starter-data-mongodb-reactive 使用MongoDB面向文档的数据库和Spring Data MongoDB的响应式编程启动器 pom文件
spring-boot-starter-data-neo4j 用于使用Neo4j图形数据库和Spring Data Neo4j的启动器 pom文件
spring-boot-starter-data-redis 启动器,用于使用Redis键值数据存储与Spring数据Redis和Lettuce 客户端 pom文件
spring-boot-starter-data-redis-reactive 启动器,用于使用Redis键值数据存储与Spring数据Redis和Lettuce 客户端的响应式编程 pom文件
spring-boot-starter-data-rest 启动器,用于使用Spring Data REST在REST之上公开Spring数据存储库 pom文件
spring-boot-starter-data-solr 启动器,用于使用Apache Solr搜索平台和Spring Data Solr pom文件
spring-boot-starter-freemarker 用于使用FreeMarker视图构建MVC web应用程序的启动器 pom文件
spring-boot-starter-groovy-templates 用于使用Groovy模板视图构建MVC web应用程序的启动器 pom文件
spring-boot-starter-hateoas 用于使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful web应用程序 pom文件
spring-boot-starter-integration 用于使用Spring集成的启动器 pom文件
spring-boot-starter-jdbc 用于使用JDBC和HikariCP连接池的启动器 pom文件
spring-boot-starter-jersey 用于使用JAX-RS和Jersey构建RESTful web应用程序的启动器和 spring-boot-starter-web作用一致 pom文件
spring-boot-starter-jooq 用于使用jOOQ访问SQL数据库是 spring-boot-starter-data-jpa or spring-boot-starter-jdbc的一种选择 pom文件
spring-boot-starter-json 用于读取和编写json的启动器 pom文件
spring-boot-starter-jta-atomikos 使用Atomikos启动JTA事务 pom文件
spring-boot-starter-jta-bitronix 使用Bitronix启动JTA事务 pom文件
spring-boot-starter-mail 用于使用Java邮件和Spring框架的电子邮件发送支持的启动器 pom文件
spring-boot-starter-mustache 用于使用Mustache视图构建web应用程序 pom文件
spring-boot-starter-oauth2-client 用于使用Spring Security的OAuth2/OpenID连接客户端特性的启动器 pom文件
spring-boot-starter-oauth2-resource-server 启动器,用于使用Spring Security的OAuth2资源服务器特性 pom文件
spring-boot-starter-quartz 用于定时任务的启动器 pom文件
spring-boot-starter-security 用于使用Spring安全性的启动器 pom文件
spring-boot-starter-test 用于测试带有JUnit、Hamcrest和Mockito等库的Spring启动器 pom文件
spring-boot-starter-thymeleaf 用于使用Thymeleaf视图构建MVC web应用程序的启动器 pom文件
spring-boot-starter-validation 启动器,用于使用Hibernate验证器进行Java Bean验证 pom文件
spring-boot-starter-web 用于使用Spring MVC构建web(包括RESTful)应用程序的启动器。使用Tomcat作为默认的嵌入式容器 pom文件
spring-boot-starter-web-services Spring Web Services的启动器 pom文件
spring-boot-starter-webflux 用于使用Spring Framework的反应性Web支持构建WebFlux应用程序的启动器 pom文件
spring-boot-starter-websocket 启动器,用于使用Spring Framework的WebSocket支持构建WebSocket应用程序 pom文件

除了应用程序启动程序之外,还可以使用以下启动程序添加生产就绪的特性

启动器名称 功能描述 pom依赖
spring-boot-starter-actuator 用于使用Spring Boot的执行器,该执行器提供了生产就绪的特性,可以帮助您监视和管理应用程序 pom文件

最后,Spring Boot还包括以下启动程序,如果想排除或替换上面指定的的技术方面,可以使用这些启动程序

启动器名称 功能描述 pom依赖
spring-boot-starter-jetty 使用Jetty作为嵌入式servlet容器的启动程序也可以替换spring-boot-starter-tomcat pom文件
spring-boot-starter-log4j2 log4j2的日志配置启动器,可以替换配置依赖spring-boot-starter-logging pom文件
spring-boot-starter-logging 使用Logback进行日志记录的启动程序。默认的日志起动器 pom文件
spring-boot-starter-reactor-netty 启动器,用于使用反应器Netty作为嵌入式反应性HTTP服务器 pom文件
spring-boot-starter-tomcat 默认配置被spring-boot-starter-web依赖使用 pom文件
spring-boot-starter-undertow 使用Undertow作为servlet容器,可替换spring-boot-starter-tomcat pom文件

有关其他社区贡献的启动程序列表,请参阅GitHub上spring-boot-starter模块中的描述文件。比如dubbo的启动器等。

  • 构建你的代码
  • 使用默认的包
    如果没有声明包的话,可能会造成特殊的问题在使用@ComponentScan, @EntityScan, or @SpringBootApplication这些注解的时候,因为每个jar中的每个类都被读取,当然为了避免这种问题,还是按照约定的规范走:遵循Java推荐的包命名约定,并使用相反的域名(例如com.xiaochengxinyizhan.project)
  • 定位主应用程序类
    SpringBoot官方通常建议将主应用程序类定位在根包中,高于其他类。@SpringBootApplication注释通常放在主类上,它隐式地为某些项定义了一个基本的“搜索包”。例如,如果正在编写JPA应用程序,@SpringBootApplication注释类的包将用于搜索@Entity项。使用根包还允许组件扫描仅应用于项目。
    如果不想使用@SpringBootApplication,那么可以通过这两个注解组合来代替@EnableAutoConfiguration 和@ComponentScan
    下面的清单显示了一个典型我们应用项目的代码层次结构布局
    在这里插入图片描述
    声明注解的主类可以如下格式:
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
  • 配置类
    Spring Boot支持基于java的配置。虽然可以将SpringApplication与XML源一起使用,但是我们通常建议您的主要源是一个@Configuration类。通常,定义main方法的类很适合作为主@Configuration
  • 导入另外的配置类
    您不需要将所有@Configuration放入一个类中。@Import注释可用于导入其他配置类。或者,您可以使用@ComponentScan自动获取所有Spring组件,包括@Configuration类
  • 导入XML配置
    如果您绝对必须使用基于XML的配置,我们建议您仍然从@Configuration类开始。然后可以使用@ImportResource注释来加载XML配置文件
    比如这种样子:
    在这里插入图片描述
  • 自动配置
    Spring Boot auto-configuration尝试根据添加的jar依赖项自动配置Spring应用程序。例如,如果HSQLDB在您的类路径上,并且您没有手动配置任何数据库连接bean,那么Spring Boot将自动配置内存中的数据库
    所以你需要通过将@EnableAutoConfiguration或@SpringBootApplication注释添加到您的@Configuration类之一来选择自动配置
    您应该只添加一个@SpringBootApplication或@EnableAutoConfiguration注释。官方通常建议只向主@Configuration类添加一个或另一个
  • 逐渐取代的自动配置
    自动配置非侵入性。在任何时候,都可以开始定义自己的配置来替换自动配置的特定部分。例如,如果添加自己的DataSource bean,默认的嵌入式数据库支持就会后退。
    如果需要了解当前应用的是什么自动配置,以及为什么,可以使用—debug开关启动您的应用程序。这样做可以为选择的核心日志记录器启用调试日志,并将条件报告记录到控制台
    在这里插入图片描述
    运行,输出结果如下图:
    在这里插入图片描述
  • 禁止某个自动配置的类
    如果指定的配置类和内部自动配置类冲突了。可以通过如下排除掉自动配置类:
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

或者如果类不在类路径上,则可以使用注释的exclude ename属性并指定完全限定名。最后,还可以使用spring.autoconfigure控制要排除的spring.autoconfigure.exclude属性
在注释级别和使用属性定义排除SpringBoot都是支持的

  • spring的beans实例和依赖的注入项
    定义bean及其注入的依赖项,SpringBoot非常友好,通过@ComponentScan 来查找扫描注入的实体类,使用@Autowired 来构造注入。
    如果是在扫描的路径下,凡是声明为@Component, @Service, @Repository, @Controller等注解的统统会自动注册到spring容器中。比如下面的例子,使用构造器注入获取RiskAssessor的bean。
package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatabaseAccountService implements AccountService {

	private final RiskAssessor riskAssessor;

	@Autowired
	public DatabaseAccountService(RiskAssessor riskAssessor) {
		this.riskAssessor = riskAssessor;
	}

	// ...

}
那么这里由于是构造器,所以我们可以省略@Autowired注解
@Service
public class DatabaseAccountService implements AccountService {

	private final RiskAssessor riskAssessor;

	public DatabaseAccountService(RiskAssessor riskAssessor) {
		this.riskAssessor = riskAssessor;
	}

	// ...

}
使用final修饰是因为之后他不会被修改。
  • 使用@SpringBootApplication注解
    相当于@Configuration @EnableAutoConfiguration @ComponentScan
    如下示例
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
@SpringBootApplication还提供别名来定制
@EnableAutoConfiguration和@ComponentScan的属性
这些特性都不是强制性的,您可以选择用它支持的任何特性替换这个注释。
例如,您可能不希望在应用程序中使用组件扫描
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {

	public static void main(String[] args) {
			SpringApplication.run(Application.class, args);
	}

}
在本例中,应用程序与任何其他Spring引导应用程序一样,
只是没有自动检测到@Component-annotated类,
并且显式地导入了用户定义的bean(请参见@Import)
作用是一样的
  • 怎么运行应用
    SpringBoot可以根据jar运行也可以通过war运行
  • IDE运行
    例如,Eclipse用户可以从文件菜单中选择Import…→现有Maven项目。
    Intellij idea可以从文件菜单中选择open导入。
    如果你本地没有项目想要从GitHub上clone一个项目可以使用git clone xxxx(git链接) 到本地。然后导入
    在这里插入图片描述
    导入项目以后需要配置maven,就如同上节讲过的先配置java环境和maven环境,然后idea中引入maven,对于maven的setting文件不了解和不了解maven的可以参考之前的文章或者直接拿去使用。在这里插入图片描述
    如果不小心触发两次运行,则会报端口冲突,这个时候停掉一个就可以了。
  • 作为打包的应用程序运行
    如果像上篇文章那样已经构建好jar包,则可以直接java -jar target/myapplication-0.0.1-SNAPSHOT.jar,如果不能理解可以参考上篇文章
    还可以在启用远程调试支持的情况下运行打包的应用程序。这样做可以将调试器附加到打包的应用程序中,如下面的示例所示
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myapplication-0.0.1-SNAPSHOT.jar
  • 使用maven插件
    运行SpringBoot应用:mvn spring-boot:run
    设置系统环境内存大小:export MAVEN_OPTS=-Xmx1024m
  • 使用gradle插件
    运行SpringBoot应用:gradle bootRun
    设置系统环境内存大小:export JAVA_OPTS=-Xmx1024m
  • 热插拔
    由于Spring引导应用程序只是普通的Java应用程序,所以JVM热交换应该是开箱即用的。JVM热交换在某种程度上受到可以替换的字节码的限制。对于更完整的解决方案,可以使用JRebel
    spring-boot-devtools模块还支持快速重启应用程序。可以参考开发工具了解如何热插拔。
  • 开发工具
    maven引入:
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
</dependencies>

gradle引入:

configurations {
	developmentOnly
	runtimeClasspath {
		extendsFrom developmentOnly
	}
}
dependencies {
	developmentOnly("org.springframework.boot:spring-boot-devtools")
}

当运行完全打包的应用程序时,开发工具将自动禁用。如果应用程序是从java -jar启动的,或者是从一个特殊的类加载器启动的,那么它就被认为是一个“生产应用程序”。如果这不适用于(例如,如果在容器中运行应用程序),请考虑排除devtools或设置-Dspring.devtools.restart.enabled=false system属性
在Maven中将依赖项标记为可选的,或者在Gradle中使用定制的“只开发”配置(如上所示),这是一种最佳实践,可以防止devtools过渡地应用于使用您的项目的其他模块。
默认情况下,重新打包的归档文件不包含devtools。如果希望使用某个远程devtools特性,需要禁用excludeDevtools build属性来包含它。Maven和Gradle插件都支持该属性

  • 默认属性
    Spring Boot支持的几个库使用缓存来提高性能。例如,模板引擎缓存已编译的模板,以避免重复解析模板文件。此外,在提供静态资源时,Spring MVC可以将HTTP缓存头添加到响应中
    虽然缓存在生产中非常有用,但在开发过程中可能会适得其反,使您无法看到刚才在应用程序中所做的更改。因此,spring-boot-devtools默认情况下禁用缓存选项。
    缓存选项通常由应用程序中的application.properties配置,例如,Thymeleaf提供spring.thymeleaf。缓存属性。spring-boot-devtools模块不需要手动设置这些属性,而是自动应用合理的开发时配置。
    由于在开发Spring MVC和Spring WebFlux应用程序时需要更多关于web请求的信息,开发人员工具将为web日志组启用调试日志。这将为您提供有关传入请求、正在处理它的处理程序、响应结果等信息。如果希望记录所有请求细节(包括潜在的敏感信息),可以打开spring.http.log-request-details配置属性
    如果你不想要默认属性,则可以在application.properties设置spring.devtools.add-properties为false。了解更多完整的属性列表可以参考这个文章
  • 自动重新启动
    当类路径上的文件发生更改时,使用spring-boot-devtools的应用程序将自动重新启动。当在IDE中工作时,这可能是一个有用的特性,因为它为代码更改提供了一个非常快的反馈循环。默认情况下,指向文件夹的类路径上的任何条目都将被监视以进行更改。注意,某些资源,如静态资产和视图模板,不需要重新启动应用程序
    由于DevTools监视类路径资源,触发重启的惟一方法是更新类路径。更新类路径的方式取决于使用的IDE。在Eclipse中,保存一个修改后的文件将导致更新类路径并触发重启。在IntelliJ IDEA中,构建项目(Build ->构建项目)具有相同的效果。
    当与LiveReload一起使用时,自动重启非常有效。有关详细信息,请参阅LiveReload部分。如果使用JRebel,则禁用自动重新启动,以便动态类重新加载。其他devtools特性(如LiveReload和属性覆盖)仍然可以使用
    DevTools依赖于应用程序上下文的shutdown钩子在重启期间关闭它。如果您禁用了关机钩子(SpringApplication.setRegisterShutdownHook(false)),它将无法正常工作。
    当决定类路径上的一个条目更改时是否应该触发重启时,DevTools会自动忽略名为spring-boot、spring-boot- DevTools、spring-boot-autoconfigure、spring-boot-致动器和spring-boot-starter的项目
    DevTools需要定制ApplicationContext使用的ResourceLoader。如果您的应用程序已经提供了一个,那么它将被包装。不支持直接覆盖ApplicationContext上的getResource方法。
    重启VS重新加载
    Spring Boot提供的重启技术使用两个类加载器。不更改的类(例如,来自第三方jar的类)被加载到基类加载器中。正在开发的类将加载到restart类加载器中。当应用程序重新启动时,将丢弃restart类加载器并创建一个新的类加载器。这种方法意味着应用程序重启通常比冷启动快得多,因为基本类加载器已经可用并已填充。如果发现重新启动对于应用程序不够快,或者遇到了类加载问题,可以考虑重新加载技术,比如ZeroTurnaround的JRebel。这些方法的工作原理是在加载类时重写它们,使它们更易于重新加载。
  • 记录条件评估中的变化
    默认情况下,每次应用程序重新启动时,都会记录一个显示条件评估增量的报告。当您进行更改时,例如添加或删除bean和设置配置属性,该报告将显示对应用程序自动配置的更改
    若要禁用报表的日志记录,请设置以下属性
spring.devtools.restart.log-condition-evaluation-delta=false
  • 排除资源
    某些资源在被更改时不一定需要重新启动。例如,Thymeleaf模板可以就地编辑。默认情况下,更改/META-INF/maven、/META-INF/resources、/resources、/static、/public或/templates中的资源不会触发重启,但会触发实时重新加载。如果希望自定义这些排除,可以使用spring.devtools.restart.exclude属性。例如,要仅排除/static和/public,需要设置以下属性
spring.devtools.restart.exclude=static/**,public/**
如果希望保留这些默认值并添加额外的排除,
可以使用spring.devtools.restart.add -exclude属性
  • 看更多的路径
    当更改不在类路径上的文件时,可能希望重新启动或重新加载应用程序。为此,使用spring.devtools.restart.additional-paths属性配置其他路径,以监视更改。可以使用前面描述的spring.devtools.restart.exclude属性来控制附加路径下的更改是触发完全重启还是实时重新加载。
  • 禁用重启
    如果不希望使用restart特性,可以使用spring.devtools.restart.enabled属性禁用它。在大多数情况下,可以在应用程序中设置此属性。属性(这样做仍然初始化restart类加载器,但它不监视文件更改)
    如果需要完全禁用restart支持(例如,因为它不能与特定的库一起工作),则需要在调用SpringApplication.run(…)之前将spring.devtools.restart.enabled System属性设置为false,如下面的示例所示
public static void main(String[] args) {
	System.setProperty("spring.devtools.restart.enabled", "false");
	SpringApplication.run(MyApp.class, args);
}
  • 使用触发器文件
    如果使用的IDE不断编译更改的文件,那我们可能更愿意只在特定的时间触发重启。所以可以使用“触发器文件”,这是一个特殊的文件,如果想要实际触发重启检查时,必须修改它。只有当Devtools检测到文件必须执行某些操作时,才会触发检查并重新启动文件。触发器文件可以手动更新,也可以使用IDE插件更新
  • 自定义重新启动类加载器
    正如前面在Restart vs Reload小节中描述的,重启功能是通过使用两个类加载器实现的。对于大多数应用程序,这种方法工作得很好。然而,它有时会导致类加载问题。
    默认情况下,IDE中任何打开的项目都是用“restart”类加载器加载的,而任何常规.jar文件都是用“base”类加载器加载的。如果您处理一个多模块项目,并且不是每个模块都导入到IDE中,那么您可能需要自定义一些东西。为此,您可以创建一个META-INF/spring-devtools。属性文件。
    spring-devtools。属性文件可以包含带restart前缀的属性。排除和restart.include。include元素是应该拉入“restart”类加载器的项,而exclude元素是应该拉入“base”类加载器的项。属性的值是应用于类路径的regex模式,如下面的示例所示
restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
所有属性键必须是唯一的。
只要属性以start.include开始。或restart.exclude。默认是。
所有META-INF/spring-devtools.properties加载类路径中的属性。可以在项目内部或项目使用的库中打包文件
  • 已知的限制
    对于使用标准ObjectInputStream反序列化的对象,Restart功能不能很好地工作。如果需要反序列化数据,可能需要结合使用Spring的ConfigurableObjectInputStream和Thread.currentThread(). getcontextclassloader()。
    不幸的是,一些第三方库反序列化时没有考虑上下文类加载器。如果发现这样的问题,需要向官方原始作者请求修复。
  • 重新加载
    spring-boot-devtools模块包含一个嵌入式LiveReload服务器,当资源发生更改时,可以使用该服务器触发浏览器刷新。LiveReload浏览器扩展可以从livereload.com免费使用Chrome、Firefox和Safari。
    如果您不想在应用程序运行时启动LiveReload服务器,可以将spring.devtools.livereload.enabled属性设置为false。
    一次只能运行一个LiveReload服务器。在启动应用程序之前,请确保没有运行其他LiveReload服务器。如果您从IDE启动多个应用程序,那么只有第一个应用程序支持LiveReload。
  • 全局设置
    通过添加一个名为.spring-boot-devtools的文件来配置全局devtools设置。属性到$HOME文件夹(注意文件名以“.”开头)。添加到此文件的任何属性都适用于您机器上使用devtools的所有Spring引导应用程序。例如,要将restart配置为始终使用触发器文件,您需要添加以下属性:
    ~/.spring-boot-devtools.properties.
spring.devtools.reload.trigger-file=.reloadtrigger
配置文件在.spring-boot-devtools中激活。
属性不会影响加载特定于概要文件的配置文件。
  • 远程应用
    Spring Boot开发人员工具并不局限于本地开发。在远程运行应用程序时,还可以使用几个特性。远程支持是opt-in。要启用它,您需要确保重新打包的归档文件中包含devtools,如下面的清单所示
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<excludeDevtools>false</excludeDevtools>
			</configuration>
		</plugin>
	</plugins>
</build>
然后需要设置spring.devtools.remote.secret属性,如下面的示例所示:
spring.devtools.remote.secret=mysecret
在远程应用程序上启用spring-boot-devtools存在安全风险。永远不要在生产部署上启用支持
远程devtools支持由两部分提供:
接受连接的服务器端端点和在IDE中运行的客户机应用程序。
当设置spring.devtools.remote.secret属性时,服务器组件将自动启用。
客户端组件必须手动启动
  • 运行远程客户端应用
    远程客户机应用程序设计为在IDE中运行。您需要运行org.springframework.boot.devtools.RemoteSpringApplication,其类路径与您连接到的远程项目相同。应用程序唯一需要的参数是它连接到的远程URL
    运行步骤如下:
  • 从“运行”菜单中选择“运行配置”。
  • 创建一个新的Java应用程序“启动配置”。
  • 浏览my-app项目。
  • 使用org.springframework.boot.devtools.RemoteSpringApplication作为主类。
  • 添加https://myapp.cfapps。io到程序参数(或任何远程URL)
运行结果
 .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote :: 2.1.5.RELEASE

2015-06-10 18:25:06.632  INFO 14938 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code/spring-boot-samples/spring-boot-sample-devtools)
2015-06-10 18:25:06.671  INFO 14938 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a17b7b6: startup date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy
2015-06-10 18:25:07.043  WARN 14938 --- [           main] o.s.b.d.r.c.RemoteClientConfiguration    : The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2015-06-10 18:25:07.074  INFO 14938 --- [           main] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729

因为远程客户机使用与实际应用程序相同的类路径,所以它可以直接读取应用程序属性。这是读取spring.devtools.remote.secret属性并将其传递给服务器进行身份验证的方法
通常建议使用https://作为连接协议,这样就可以对通信进行加密,并且不能拦截密码。
如果需要使用代理访问远程应用程序,请配置spring.devtools.remote.proxy.host和spring.devtools.remote.proxy.port属性

  • 远程更新
    远程客户机以与本地重启相同的方式监视应用程序类路径的更改。任何更新的资源都会被推送到远程应用程序,并(如果需要的话)触发重启。如果您对使用本地没有的云服务的特性进行迭代,这将非常有用。通常,远程更新和重新启动要比完整的重新构建和部署周期快得多
    只有在远程客户机运行时才监视文件。如果在启动远程客户机之前更改文件,则不会将其推送到远程服务器
  • 打包应用到生产
    可执行jar可用于生产部署。由于它们是自包含的,所以也非常适合基于云的部署对于其他“生产准备”特性,如健康状况、审计和度量REST或JMX端点,可以考虑添加spring-boot-actuator看第五天的内容了解更多特性吧。

猜你喜欢

转载自blog.csdn.net/wolf_love666/article/details/90451484