0x02 Spring Boot 2.x之如何使用

这一篇博文主要讲解Spring Boot 2.x 如何使用的问题,覆盖了如何构建项目,自动配置和如何运行你的应用程序。其中也包括一些最佳实践,学会这些会使得我们开发应用程序更加容易。

在学习这篇文章之前,最好学过上一篇博文 Spring Boot 2.x 之入门指南

1. 项目构建工具

我们所熟知的项目构建工具有Ant, Maven,Gradle.
尽管Spring Boot 也支持Ant 构建Spring Boot应用程序,但是支持的不是特别好,强烈建议我们使用Maven 或者Gradle 构建我们的项目。

1.1 依赖管理

Spring Boot 帮助我们管理了很多依赖,一般我们不需要对这些依赖指定版本,Spring Boot 会帮我们做这些。当升级Spring Boot 后,这些对应的依赖也会跟随着升级。

比如Spring Boot 和Spring Framework 的版本升级对应关系如下表所示:

Spring Boot 版本Spring Framework 版本
1.5.19.RELEASE4.3.22.RELEASE
2.0.8.RELEASE5.0.12.RELEASE
2.1.2.RELEASE5.1.4.RELEASE

1.2 Maven

Spring Boot 通过 spring-boot-starter-parent 引入了一些默认配置

1.2.1使用 Starter Parent

要将项目配置为继承spring-boot-starter-parent,pom.xml 中添加如下配置:

<!-- Inherit defaults from Spring Boot -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.2.RELEASE</version>
</parent>

要针对当前Spring Boot 升级其中某一个依赖项,只需要添加如下即可:

<properties> 
	<spring-data- releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> 
 </ properties>

1.2.2 不使用 Starter Parent

如果您不想使用spring-boot-starter-parent可以通过使用scope=import依赖项来保持依赖项管理。

<dependencyManagement>
		<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.2.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.2.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

1.2.3 使用 Spring Boot Maven 插件

如果我们想打包成可执行jar ,那么需要添加这个插件

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

1.3 Gradle

要学习如何使用Spring Boot 和Gradle集成,请参考 Spring Boot’s Gradle 插件文档

参考文档(HTMLPDF)
API

1.4 Ant

这个感觉几乎没人会用,就不写了。

1.5 Starters

Starters 功能,相当于按照功能分组打包了一些依赖配置
举个例子,比如当你想要使用Spring Boot 和Spring Data JPA 集成,那么使用这个spring-boot-starter-data-jpa 即可,它包含了集成所需要的所有依赖,就不再需要一个一个添加依赖项了。

怎么样? 是不是感觉方便了许多?

而且 所有官方的Starters 都遵循一个格式规范:

    spring-boot-starter-*

而第三方的一些Starters 则遵循这样的格式规范:

thirdpartyproject-spring-boot-starter

下面这些starters 都属于org.springframework.boot 分组里面

Spring Boot 应用程序 starters

名字描述POM
spring-boot-starterStarter核心,包括自动配置支持,日志和yaml文件Pom
spring-boot-starter-activemqApache ActiveMQ 消息中间件Pom
spring-boot-starter-amqpSpring AMQP 和Rabbit MQPom
spring-boot-starter-aop面向切面编程 Spring AOP 和 AspectJPom
spring-boot-starter-artemis消息中间件Apache ArtemisPom
spring-boot-starter-batchSpring BatchPom
spring-boot-starter-cacheSpring Framework的缓存支持Pom
spring-boot-starter-cloud-connectorsCloud Foundry和Heroku等云平台中的服务连接Pom
spring-boot-starter-data-cassandraCassandra 分布式数据库和Spring Data CassandraPom
spring-boot-starter-data-cassandra-reactiveCassandra 分布式数据库 和Spring Data Cassandra ReactivePom
spring-boot-starter-data-couchbaseCouchbase面向文档的数据库和Spring Data CouchbasePom
spring-boot-starter-data-couchbase-reactiveCouchbase 面向文档的数据库和Spring Data Couchbase ReactivePom
spring-boot-starter-data-elasticsearchElasticsearch 搜索和分析引擎 和 Spring Data ElasticsearchPom
spring-boot-starter-data-jdbcSpring Data JDBCPom
spring-boot-starter-data-jpaSpring Data JPA (Hibernate)Pom
spring-boot-starter-data-ldapSpring Data LDAPPom
spring-boot-starter-data-mongodbMongoDB 面向文档数据库和Spring Data MongoDBPom
spring-boot-starter-data-mongodb-reactiveMongoDB 面向文档数据库和 Spring Data MongoDB ReactivePom
spring-boot-starter-data-neo4jNeo4j 图表数据库 和Spring Data Neo4jPom
spring-boot-starter-data-redisRedis key-value 数据存储 和 Spring Data Redis 和 Lettuce clientPom
spring-boot-starter-data-redis-reactiveRedis key-value 数据存储 和Spring Data Redis reactive 和 Lettuce clientPom
spring-boot-starter-data-rest使用Spring Data REST在REST上公开Spring Data存储库Pom
spring-boot-starter-data-solr使用Spring Data Solr的Apache Solr搜索平台Pom
spring-boot-starter-freemarker构建 MVC web 应用程序使用 FreeMarker 视图Pom
spring-boot-starter-groovy-templates使用Groovy模板视图构建MVC Web应用程序Pom
spring-boot-starter-hateoas使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序Pom
spring-boot-starter-integrationSpring 集成Pom
spring-boot-starter-jdbcHikariCP JDBC 数据库连接池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或spring-boot-starter-jdbc的替代品Pom
spring-boot-starter-json可读写的 jsonPom
spring-boot-starter-jta-atomikos使用 Atomikos JTA 事务Pom
spring-boot-starter-jta-bitronix使用 Bitronix JTA 事务Pom
spring-boot-starter-mail使用Java Mail和Spring Framework的电子邮件发送支持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使用Quartz调度程序Pom
spring-boot-starter-security使用Spring SecurityPom
spring-boot-starter-test使用JUnit,Hamcrest和Mockito等库来测试Spring Boot应用程序Pom
spring-boot-starter-thymeleaf使用Thymeleaf视图构建MVC Web应用程序Pom
spring-boot-starter-validation使用Java Bean Validation和Hibernate ValidatorPom
spring-boot-starter-web使用Spring MVC构建Web,包括RESTful应用程序。 使用Tomcat作为默认嵌入式容器Pom
spring-boot-starter-web-services使用Spring Web ServicesPom
spring-boot-starter-webflux使用Spring Framework的Reactive Web支持构建WebFlux应用程序Pom
spring-boot-starter-websocket使用Spring Framework的WebSocket支持构建WebSocket应用程序Pom

除应用程序启动器外,还可以使用以下启动器添加生产就绪功能:

名字描述POM
spring-boot-starter-actuator使用Spring Boot的Actuator,它提供生产就绪功能,帮助您监控和管理您的应用程序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使用Reactor Netty作为嵌入式响应式HTTP服务器POM
spring-boot-starter-tomcat使用Tomcat作为嵌入式servlet容器。 spring-boot-starter-web使用的默认servlet容器启动器POM
spring-boot-starter-undertow使用Undertow作为嵌入式servlet容器。 spring-boot-starter-tomcat的替代品POM

2. 构建你的代码

Spring Boot 不需要任何特殊的代码布局,但是有一些最佳实践。

2.1 不要使用默认包名

当一个java类不包含在任何包名下的时候,会有一个默认包名,这个默认包名的使用应该尽可能避免,因为在Spring Boo 应用程序使用@ComponentScan, @EntityScan 或者 @SpringBootApplication 注解的时候,扫描包可能会出现问题。

强烈推荐使用Java 推荐使用的包名规范:com.example.project

2.2 主应用程序类放在其他类之上的根包中

强烈建议将主应用程序类放在其他类之上的根包中

类似这样的代码结构:

com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

Application.java 内容如下:

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);
	}
}

3. Configuration 类

Spring Boot 虽然也支持 XML来配置Spring Bean ,但是通常建议使用@Configuration注解进行配置。

3.1 导入其他Configuration类:

打个比方,A 类中要使用到B的实例方法,
可以使用@Import 注解

3.2 导入XML 配置

如果我们应用程序中必须使用XML配置,虽然也可以使用这种方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 在这里添加新的Spring Bean 配置文件 -->
	<import resource="classpath:spring-config/application-mp-context.xml"/>
</beans>

那么仍然建议使用Java 配置类来实现。
定义范例如下:
XML 文件位置如下存放
在这里插入图片描述
定义一个配置类注解来引入

@ImportResource(locations = {
  "classpath:spring-config/application-context.xml",
  "classpath:spring-config/application-bean-context.xml" })
@Configuration
public class MyConfig {
      
}

4. 自动配置

Spring Boot自动配置尝试根据我们添加的jar依赖项自动配置Spring应用程序。
比如,比如 添加HSQLDB依赖后,我们虽然没有手动配置任何数据库连接,
Spring Boot也会帮我们自动配置内存数据库。

要启用这个功能,需要在启动类上添加注解

@EnableAutoConfiguration 或 @SpringBootApplication

这两个注解只需要配置在一个带有@Configuration注解的类中就可以了,不需要多次配置。

4.1 逐步更换自动配置

前面已经说到,Spring Boot 会替我们做一些默认的配置,如果不想使用默认的配置,也没有关系,只要我们添加了自己的配置,默认的配置就会失效。

4.2 禁用某些自动配置

如果不想用某一个依赖中的默认配置,可以添加禁用,禁用方法很简单。
只需要使用exclude 去掉类配置即可
范例如下:

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

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

5. Spring Beans 和 依赖注入

@ComponentScan

Spring Boot 仍然友好支持Spring 的一些依赖注入技术
通常来讲,通过 @ComponentScan 注解来扫描查找我们的Spring Bean.

@Autowired

@Autowired 构造方法注入

类级别的注解

@Component, @Service, @Repository, @Controller 等等

@Service 和 @Autowired 注解组合使用

用法如下:

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;
	}

	// ...

}

如果一个Bean 中只有一个构造方法,那么可以@Autowired 省略

@Service
public class DatabaseAccountService implements AccountService {

	private final RiskAssessor riskAssessor;

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

	// ...

}

但是要注意我们这里使用了final 关键词来确保这个对象不会被改变

6. 使用 @SpringBootApplication 注解

许多Spring Boot开发人员喜欢他们的应用程序使用自动配置,组件扫描等功能。
我们可以通过一个@SpringBootApplication注解开启以下三个注解的功能

  • @EnableAutoConfiguration: 启用Spring Boot的自动配置机制
  • @ComponentScan: @Component在应用程序所在的包上启用扫描
  • @Configuration: 允许在上下文中注册额外的bean或导入其他配置类

总之呢,使用@SpringBootApplication这一个注解就相当于同时开启了@EnableAutoConfiguration,@ComponentScan,@Configuration这三个注解

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);
	}
}

如果我们只想使用@EnableAutoConfiguration@Configuration这两个注解,而不想使用@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);
	}

}

在这个例子中,如果使用@Component注解将不再有效,不会启用包扫描功能即@ComponentScan注解 的功能,而且只有@Import 注解引入的几个类有效

7. 运行你的应用程序

将应用程序打包为jar并使用内嵌的HTTP容器的最大优势之一是可以像运行任何其他服务器一样运行应用程序。调试Spring Boot应用程序也很容易。也不需要任何特殊的IDE插件或扩展。

7.1 从IDE 运行

从IDE像使用简单的Java应用程序一样运行Spring Boot应用程序。

但是我们一般需要导入项目,大多数IDE可以直接导入Maven项目。

例如,Eclipse用户可以从Menu中选择Import…​→ Existing Maven Projects File
---->pom.xml

如果无法将项目直接导入IDE,则可以使用插件生成。

Maven 包括Eclipse 插件IDEA 插件

Gradle 也提供了插件

7.2 运行打包好的可执行Jar

如果我们使用Maven 或者Gradle 打包好了可执行的Jar,那么可以输入命令来运行我们的Spring Boot 应用

java -jar target/myapplication-0.0.1-SNAPSHOT.jar

7.3 使用Maven 插件

Spring Boot Maven 插件包含一个run 命令帮助快速启动我们的应用程序

mvn spring-boot:run

7.4 使用Gradle 插件

Spring Boot Gradle 插件提供了bootRun 命令

gradle bootRun

8. 热部署

有时候我们对我们的应用程序做了一点点的修改,但是重启应用程序却需要花费较长的时间,因此Spring Boot 提供了一个插件,支持应用程序的快速重启。

要添加这个功能,我们只需要添加这个依赖即可
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")
}

8.1 热部署默认属性配置

Spring Boot 支持的很多库都有缓存功能,通过使用缓存来提高性能。
Spring Boot支持的几个库使用缓存来提高性能。例如,模板引擎缓存已编译的模板以避免重复解析模板文件。此外,Spring MVC可以在提供静态资源时为响应添加HTTP缓存标头。

虽然缓存在生产中非常有用,但在开发过程中可能会适得其反,使您无法看到刚刚在应用程序中进行的更改。因此,spring-boot-devtools默认禁用缓存选项。

缓存选项通常由application.properties文件中的设置配置。例如,Thymeleaf提供该spring.thymeleaf.cache 属性。该spring-boot-devtools模块不需要手动设置这些属性,而是自动应用合理的开发时配置。
application.properties

spring.thymeleaf.cache=false

因为在开发Spring MVC和Spring WebFlux应用程序时需要很多有关Web请求的信息,所以开发人员工具将启用DEBUG日志web 记录组的日志记录。这将为您提供有关传入请求,处理程序正在处理它,响应结果等的信息。如果您希望记录所有请求详细信息(包括可能的敏感信息),则可以打开 spring.http.log-request-details配置属性。
application.properties

spring.http.log-request-details=true

其他默认配置属性

        Map<String, Object> properties = new HashMap<>();
		properties.put("spring.thymeleaf.cache", "false");
		properties.put("spring.freemarker.cache", "false");
		properties.put("spring.groovy.template.cache", "false");
		properties.put("spring.mustache.cache", "false");
		properties.put("server.servlet.session.persistent", "true");
		properties.put("spring.h2.console.enabled", "true");
		properties.put("spring.resources.cache.period", "0");
		properties.put("spring.resources.chain.cache", "false");
		properties.put("spring.template.provider.cache", "false");
		properties.put("spring.mvc.log-resolved-exception", "true");
		properties.put("server.error.include-stacktrace", "ALWAYS");
		properties.put("server.servlet.jsp.init-parameters.development", "true");
		properties.put("spring.reactor.stacktrace-mode.enabled", "true");

8.2 自动重启

当我们使用了spring-boot-devtools 插件之后,一旦classpath 下的任何文件有改动,应用程序就会自动重启,这在IDE中开发的时候非常有用,可以非常快速及时地显示出代码做的改动。默认情况下,会检测classpath 下所有文件的改动,但是某些静态资源或者视图模板则不需要重启应用程序。

触发重启
正如刚才提到,当classpath 文件夹下任何文件有了改动就会触发一次重启,不过这个不同的IDE可能稍微有差别。Eclipse中保存修改的文件后就会自动触发重启,但是在IDEA 中需要点击 Build -> Build Project才得行。

  • 默认情况下,Gradle和Maven在类路径上检测到DevTools时会这样做。
  • 与LiveReload一起使用时,自动重启非常有效。
  • 如果使用JRebel,则禁用自动重新启动以支持动态类重新加载。其他devtools功能(例如LiveReload和属性覆盖)仍然可以使用。
  • SpringApplication.setRegisterShutdownHook(false)如果添加了这句话自动重启就失效了
  • DevTools自动忽略命名的项目spring-boot, spring-boot-devtools,spring-boot-autoconfigure,spring-boot-actuator,和 spring-boot-starter,这些类Spring 框架类库,一般不会改变,所以DevTools不会检测这些类的改动。
  • 如果感觉重启速度不够快,可以考虑JRebel 等技术

8.2.1 自动重启日志记录

默认情况下,每次自动重启改动都会被日志记录下来,如果想要关闭,可配置如下属性

spring.devtools.restart.log-condition-evaluation-delta=false

8.2.2 排除触发文件路径监控

默认情况下这几个文件夹下的改动不会触发自动重启

  • /META-INF/maven
  • /META-INF/resources
  • /resources
  • /static
  • /public
  • /templates

如果想取消默认配置添加自定义排除路径可做改动如下:

application.properties

spring.devtools.restart.exclude=static/**,public/**

如果想保留默认的排除路径,新增加排除路径,使用这个属性

spring.devtools.restart.additional-exclude=views/**,pages/**

8.2.3 添加触发文件路径监控

当您对不在classPath中的文件进行更改时,也希望重新启动或重新加载应用程序,那么配置如下所示即可:

spring.devtools.restart.additional-paths

8.2.4 禁用重启

如果想要禁用可以通过这个属性来配置

spring.devtools.restart.enabled=false

这么设置好后,虽然不会触发监控,但是类加载器还会重新加载。
要彻底禁用需要配置如下:

 System.setProperty("spring.devtools.restart.enabled", "false");

启动配置类完整代码如下:

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) {
	    System.setProperty("spring.devtools.restart.enabled", "false");
		SpringApplication.run(Application.class, args);
	}
}

8.2.5 使用限制

对于使用标准反序列化的对象,重新启动功能不起作用ObjectInputStream。

9. LiveReload

该spring-boot-devtools模块包括一个嵌入式LiveReload服务器,可用于在更改资源时触发浏览器刷新。

LiveReload浏览器扩展程序可免费用于Chrome,Firefox和Safari。

如果不想在应用程序运行时启动LiveReload服务器,则可以将spring.devtools.livereload.enabled属性设置为false

一次只能运行一个LiveReload服务器。在启动应用程序之前,请确保没有其他LiveReload服务器正在运行。如果从IDE启动多个应用程序,则只有第一个具有LiveReload支持。

10. 远程应用程序

注意:这个功能使用场景目前还不太了解,今后遇到了再好好研究吧~

Spring Boot开发人员工具不仅限于本地开发。远程运行应用程序时,您还可以使用多个功能。远程支持是选择加入。要启用它,您需要确保它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=mysecret

spring-boot-devtools在远程应用程序上启用存在安全风险。
永远不应该在生产部署上启用支持。

远程devtools支持分为两部分:接受连接的服务器端端点和在IDE中运行的客户端应用程序。spring.devtools.remote.secret设置属性后,将自动启用服务器组件。必须手动启动客户端组件。

10.1 运行远程客户端应用程序

远程客户端应用程序旨在从IDE中运行。您需要org.springframework.boot.devtools.RemoteSpringApplication使用与连接到的远程项目相同的类路径运行 。应用程序的单个必需参数是它连接的远程URL。

例如,我们使用的是Eclipse或STS,并且您有一个名为my-app已部署到Cloud Foundry的项目,那么您将执行以下操作:

  • 选择Run Configurations…​从Run菜单。
  • Create a new Java Application “launch configuration”.
  • 浏览my-app项目。
  • 用org.springframework.boot.devtools.RemoteSpringApplication作主类。
  • 添加https://myapp.cfapps.io 到Program arguments(或任何远程URL)。
    正在运行的远程客户端可能类似于以下列表:
  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote :: 2.1.2.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
2015-06-10 18:25:07.130  INFO 14938 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)
  • 因为远程客户端使用与真实应用程序相同的类路径,所以它可以直接读取应用程序属性。这是如何spring.devtools.remote.secret
    读取属性并将其传递到服务器以进行身份​​验证。
  • 始终建议使用https://连接协议,以便加密流量并且不会截获密码。
  • 如果需要使用代理来访问远程应用程序,请配置 spring.devtools.remote.proxy.hostspring.devtools.remote.proxy.port属性

10.2 远程更新

远程客户端以与本地重新启动相同的方式监视应用程序类路径以进行更改 。任何更新的资源都会被推送到远程应用程序,并且(如果需要)会触发重新启动。如果您迭代使用本地没有的云服务的功能,这将非常有用。通常,远程更新和重新启动比完全重建和部署周期快得多。

仅在远程客户端运行时监视文件。如果在启动远程客户端之前更改文件,则不会将其推送到远程服务器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值