artifacts
。咱们建议您选择Maven
或Gradle
。Spring Boot能够与其余构建系统(例如Ant)一块儿工做,可是它们之间的兼容性并非特别好。Pom.xml
文件中)的任何这些依赖项提供一个版本(也就是能够省略version
标签),由于Spring Boot正在为您进行管理(也就是Spring Boot 已经帮咱们配置了)。当您升级Spring Boot自己(也就是切换到更高版本的Spring Boot)时,这些依赖关系也将以一致的方式进行升级。
若是你以为这是必要的,你仍然能够指定一个版本并覆盖Spring Boot的建议。html
spring-boot-dependencies
)提供,也能够为Maven和Gradle提供额外的专用支持。
Spring Boot的每一个版本都与一个Spring Framework的基础版本相关联,所以咱们强烈建议您不要自行指定其版本。java
spring-boot-starter-parent
项目中继承,以得到相应的默认值。 父项目提供如下功能:
<version>
标签,进而从spring-boot-dependencies POM
中继承。application.properties
和application.yml
中的配置进行资源过滤(其实就是maven
打包时指定哪些文件要过滤,不参与打包),包括特定于配置文件的文件(例如application-foo.properties
和application-foo.yml
)spring-boot-starter-parent
中继承,只需设置<parent>
:<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
复制代码
您只须要在上面
parent
依赖项上指定Spring Boot版本号。 若是您导入更多的starter
,则能够安全地省略版本号(即<version>
)。git
pom.xml
中。<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
复制代码
检查
spring-boot-dependencies pom
以获取支持的属性列表。github
spring-boot-starter-parent POM
的方式来使用 Spring Boot,可能你有本身的标准(也就是自定义配置)或者你单单就喜欢显式地声明全部的maven
配置。spring-boot-starter-parent
,你仍然能够经过使用scope = import dependency
来保留依赖关系管理(但不是插件管理):<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
复制代码
spring-boot-dependencies
标签(也就是pom
文件的dependencies
标签)以前在项目的dependencyManagement
中添加一个条目。 例如,要升级到另外一个Spring Data
发行版,您须要将如下内容添加到您的pom.xml
中。<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>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
复制代码
在上面的例子中,咱们指定了一个BOM(物料清单,这里指的是
pom.xml
),可是任何依赖类型均可以被覆盖。web
spring-boot-starter-parent
选择至关保守的Java
兼容性(指的是上面说的 Java 6
)。 若是您想遵循咱们的建议并使用较新的Java版本,则能够添加一个java.version属性:<properties>
<java.version>1.8</java.version>
</properties>
复制代码
<plugins>
部分:<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
复制代码
若是你使用Spring Boot的起始
parent POM
,你只须要添加插件,除非你想改变在父代中定义的配置,不然不须要进行配置。正则表达式
dependencies
部分导入starters
。 与Maven
不一样的是,没有super parent
能够导入来共享某些配置。repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.5.9.RELEASE")
}
复制代码
spring-boot-gradle-plugin
也是可用的,并提供了建立可执行jar
和从源项目运行项目的任务。 它还提供依赖管理,除其余功能外,还容许您省略由Spring Boot
管理的任何依赖项的版本号:plugins {
id 'org.springframework.boot' version '1.5.9.RELEASE'
id 'java'
}
repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
复制代码
starters
是一系列能够引进你项目的依赖描述符(dependency descriptors我以为其实就是一些jar
包,里边的pom.xml
文件帮咱们作了某些配置而已)。您能够获得所须要的全部关于Spring
及其相关技术的一站式服务,无需搜索示例代码,也不须要粘贴大量依赖描述符(传统咱们pom.xml
都要引入不少的<dependency/>
)。例如,若是你想开始使用Spring
和JPA
来访问数据库,只需在你的项目中加入spring-boot-starter-data-jpa
依赖项,你就能够开始编码了哟~starters
包含了许多你须要快速运行一个项目并传递依赖项的依赖。(即它会自动传递依赖)。
为何叫
starters
这个名字呢?全部的官方
starter
都遵循这个spring-boot-starter-*
命名规范,*
表明某一具体的应用(好比web应用程序)。这种命名结构旨在帮助您找到某一具体的starter
。许多IDE中的Maven集成容许您按名称搜索依赖项。例如,安装某一具体的Eclipse``STS
插件后,只需在POM
编辑器中点击ctrl-space
,而后键入“spring-boot-starter”
获取完整列表。正如建立本身的starter中所解释的,第三方starter
不该该以spring-boot
为开始,由于它是为官方Spring Boot工件(artifacts)保留的。一个表示acme
应用的第三方starter
的命名可能就是acme-spring-boot-starter
。redis
org.springframework.boot
包下定义的一些starter
: starters
名称 | 描述 | Pom |
---|---|---|
spring-boot-starter |
核心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进行面向方面编程的starter |
POM |
spring-boot-starter-artemis |
使用Apache Artemis 进行JMS 消息传递 |
POM |
spring-boot-starter-batch |
使用Spring Batch |
POM |
spring-boot-starter-cache |
使用Spring框架的缓存支持 | POM |
spring-boot-starter-cloud-connectors |
使用Spring Cloud链接器的starter ,可简化Cloud Foundry和Heroku等云平台中的服务链接 |
POM |
spring-boot-starter-data-cassandra |
使用Cassandra分布式数据库和Spring Data Cassandra | POM |
spring-boot-starter-data-couchbase |
使用面向文档的数据库Couchbase和Spring Data Couchbase | POM |
spring-boot-starter-data-elasticsearch |
使用搜索、分析引擎Elasticsearch和Spring Data Elasticsearch | POM |
spring-boot-starter-data-gemfire |
使用分布式数据存储GemFire和Spring Data GemFire | POM |
spring-boot-starter-data-jpa |
使用带有Hibernate的Spring Data JPA | POM |
spring-boot-starter-data-ldap |
使用Spring Data LDAP | POM |
spring-boot-starter-data-mongodb |
使用面向文档的数据库MongoDB和Spring Data MongoDB | POM |
spring-boot-starter-data-neo4j |
使用图形数据库Neo4j和Spring Data Neo4j | POM |
spring-boot-starter-data-redis |
使用带有Spring Data Redis和Jedis客户端的Redis键值数据存储 | POM |
spring-boot-starter-data-rest |
Starter for exposing Spring Data repositories over REST using Spring Data REST | POM |
spring-boot-starter-data-solr |
结合Spring Data Solr使用Apache 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与Tomcat JDBC链接池配合使用 | POM |
spring-boot-starter-jersey |
使用JAX-RS和Jersey构建RESTful Web应用程序的starter 。是Spring-Boot-Starter-Web 的另外一种选择 |
POM |
spring-boot-starter-jooq |
使用jOOQ访问SQL数据库的starter 。 spring-boot-starter-data-jpa 或spring-boot-starter-jdbc 的替代方案 |
POM |
spring-boot-starter-jta-atomikos |
使用Atomikos的JTA事务 | POM |
spring-boot-starter-jta-bitronix |
使用Bitronix的JTA事务 | POM |
spring-boot-starter-jta-narayana |
Spring Boot Narayana JTA Starter |
POM |
spring-boot-starter-mail |
使用Java Mail和Spring Framework支持的电子邮件发送 | POM |
spring-boot-starter-mobile |
使用Spring Mobile构建Web应用程序 | POM |
spring-boot-starter-mustache |
使用Mustache视图构建MVC Web应用程序 | POM |
spring-boot-starter-security |
使用Spring Security | POM |
spring-boot-starter-social-facebook |
使用Spring社交Facebook | POM |
spring-boot-starter-social-linkedin |
使用Spring社交LinkedIn | POM |
spring-boot-starter-social-twitter |
使用Spring社交twitter | POM |
spring-boot-starter-test |
Starter用于测试包含JUnit,Hamcrest和Mockito等库的Spring Boot应用程序 | POM |
spring-boot-starter-thymeleaf |
使用Thymeleaf视图构建MVC Web应用程序 | POM |
spring-boot-starter-validation |
经过Hibernate Validator使用Java Bean验证 | POM |
spring-boot-starter-web |
用于构建Web的starter ,包括使用Spring MVC的RESTful应用程序。 使用Tomcat做为默认的嵌入容器 |
POM |
spring-boot-starter-web-services |
使用Spring Web Services | POM |
spring-boot-starter-websocket |
使用Spring Framework的WebSocket支持构建WebSocket应用程序 | POM |
starter
以外,还可使用如下starter
来作好相应的生产准备: starters
名称 | 描述 | POM |
---|---|---|
spring-boot-starter-actuator |
使用具备生产准备功能的Spring Boot的执行器,能够帮助您监控和管理您的应用程序 | POM |
spring-boot-starter-remote-shell |
使用CRaSH远程shell经过SSH监视和管理您的应用程序。 自1.5以来已弃用 | POM |
starters
: starters
名称 | 描述 | POM |
---|---|---|
spring-boot-starter-jetty |
使用Jetty做为嵌入式servlet容器的starter 。 spring-boot-starter-tomcat 的替代方案 |
POM |
spring-boot-starter-log4j2 |
使用Log4j2进行日志记录的starter 。spring-boot-starter-logging 的替代方法 |
POM |
spring-boot-starter-logging |
使用Logback进行日志记录。 默认日志starter |
POM |
spring-boot-starter-tomcat |
使用Tomcat做为嵌入式servlet容器的starter 。 spring-boot-starter-web 使用的默认servlet容器starter |
POM |
spring-boot-starter-undertow |
使用Undertow做为嵌入式servlet容器的starter 。 spring-boot-starter-tomcat 的替代方案 |
POM |
有关其余社区贡献者的列表,请参阅GitHub上的
spring-boot-starters
模块中的README文件。spring
package
时,咱们通常会认为这个类位于“默认包”中。一般不鼓励使用“默认软件包”,且应该避免使用“默认软件包”。对于使用@ComponentScan
,@EntityScan
或@SpringBootApplication
注释的Spring Boot应用程序来讲,使用“默认包”可能会致使特定的问题,由于每一个jar的每一个类都将被读取。(这里其实说的是Spring Boot会进行组件、实体、包的扫描@ComponentScan
)
咱们建议您遵循Java推荐的软件包命名约定,并使用反向域名(例如com.example.project)。mongodb
@EnableAutoConfiguration
注释一般放在主类上,而且它隐含地为某些项目定义了一个基本的“搜索包”。例如,若是您正在编写JPA应用程序,则@EnableAutoConfiguration
注释类的包将用于搜索@Entity
条目。@ComponentScan
注释而不须要指定basePackage
属性。若是您的主类位于根包中,也可使用@SpringBootApplication
注释。com
+- example
+- myproject
+- Application.java
+- domain
+- Customer.java
+- CustomerRepository.java
+- service
+- CustomerService.java
+- web
+- CustomerController.java
复制代码
Application.java
文件将声明main
方法以及基本的@Configuration
。package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
复制代码
SpringApplication.run()
,咱们一般建议您的主要来源是@Configuration
类。一般,定义main
方法的类也是@Configuration
的一个首选。
在互联网上已经发布了许多使用XML配置的Spring配置示例。 若是可能,请始终尝试使用基于Java的等效配置。 搜索
Enable*
注释能够是一个很好的起点。shell
@Configuration
放到一个类中。@Import
注解可用于导入其余配置类。或者,您可使用@ComponentScan
自动获取全部Spring组件,包括@Configuration
类。@Configuration
类开头。而后您可使用额外的@ImportResource
注解来加载XML配置文件。@EnableAutoConfiguration
或@SpringBootApplication
注释添加到其中一个@Configuration
类来选择自动配置。
您应该只添加一个
@EnableAutoConfiguration
注释。 咱们一般建议您将其添加到您的主要@Configuration
类。
DataSource
Bean,则默认的嵌入式数据库支持将失效。--debug
参数启动您的应用程序(也就是debug模式启动应用程序)。 这将启用选择核心记录器的调试日志,并将自动配置报告记录到控制台。@EnableAutoConfiguration
的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 {
}
复制代码
spring.autoconfigure.exclude
属性(这个主要在property
文件中声明)来控制自动配置类的列表。
你能在注解级别和
property
配置文件级别设置警用自动相应的配置。(注:一个类声明了@Configuration
注解则表示该类是一个配置类,能够被禁用)
bean
及其注入的依赖关系。 为了简单起见,咱们常常发现使用@ComponentScan
来查找bean
,并结合使用@Autowired
构造函数注入效果最好。@ComponentScan
而不带任何参数。 全部的应用程序组件(@Component
,@Service
,@Repository
,@Controller
等)都将被自动注册为Spring Bean
。@Service Bean
,它使用构造函数注入来得到必需的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;
}
// ...
}
复制代码
bean
有一个构造函数,你能够省略@Autowired
。@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
//此处省略了@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
复制代码
riskAssessor
字段被标记为final
,代表它不能被随后更改。@Configuration
,@EnableAutoConfiguration
和@ComponentScan
注解其主类。因为这些注释常常一块儿使用(特别是若是您遵循以上最佳实践),Spring Boot提供了一种更方便的@SpringBootApplication
替代方法。@SpringBootApplication
注释等价于使用@Configuration
,@EnableAutoConfiguration
和@ComponentScan
及其默认属性:package com.example.myproject;
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
的属性。
本节仅介绍基于jar的打包。若是您选择将应用程序打包为war文件,则应参考您的服务器和IDE文档。
Maven
项目,例如在Eclipse
的File -> Import -> Existing Maven Projects
。若是您不当心运行了两次Web应用程序,则会看到“端口已被使用Port already in use”错误。 STS用户可使用
"Relaunch"
按钮而不是"Run"
来确保关闭任何现有的实例。
若是您使用Spring Boot Maven或Gradle插件建立可执行jar
,则可使用java -jar
运行应用程序。 例如:
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
也能够运行打包的应用程序并启用远程调试支持。 这使您能够将调试器附加到打包的应用程序中:(主要用于远程调试) $ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \ -jar target/myproject-0.0.1-SNAPSHOT.jar
Spring Boot Maven插件包含一个可用于快速编译和运行应用程序的run
目标。 应用程序以分解形式运行,就像在IDE中同样。
$ mvn spring-boot:run
您可能还想使用有用的操做系统环境变量:
$ export MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=128M
Spring Boot Gradle插件还包含一个bootRun
任务,可用于以分解形式运行您的应用程序。 不管什么时候导入spring-boot-gradle-plugin
,都会添加bootRun
任务:
$ gradle bootRun
您可能还想使用有用的操做系统环境变量:
$ export JAVA_OPTS=-Xmx1024m -XX:MaxPermSize=128M
Spring Boot包含一组额外的工具,可使应用程序开发体验更愉快。 spring-boot-devtools模块能够包含在任何项目中以提供额外的开发时间功能。 要包含devtools支持,只需将模块依赖关系添加到您的版本:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
复制代码
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}
复制代码
运行完整打包的应用程序时,开发者工具会自动禁用。 若是您的应用程序是使用
java -jar
方式启动的,或者若是它是使用特殊的类加载器启动的,那么它就被认为是“生产应用程序”。 将依赖关系标记为可选的是一种最佳实践,能够防止devtools经过项目传递到其余模块。 Gradle不支持optional
的依赖关系,所以您可能但愿在此期间看看propdeps插件。
从新打包的
archives
在默认状况下不包含devtools。 若是您想使用某些远程devtools功能,则须要禁用excludeDevtools
构建属性以包含它。 该属性支持Maven和Gradle插件。
application.properties
文件中的设置进行配置。 例如,Thymeleaf提供了spring.thymeleaf.cache
属性。 而不须要手动设置这些属性,spring-boot-devtools
模块将自动应用合理的开发配置。
有关应用的属性的完整列表,请参阅DevToolsPropertyDefaultsPostProcessor。
使用spring-boot-devtools
的应用程序将在类路径上的文件发生更改时自动重启。 在IDE中工做时,这是一个很是有用的功能,由于它为代码更改提供了一个很是快速的反馈循环。 默认状况下,将监视指向文件夹的类路径中的任何条目以进行更改。 请注意,某些资源(如静态资产和视图模板)不须要从新启动应用程序。
触发重启
因为DevTools监视类路径资源,触发重启的惟一方法是更新类路径。 致使类路径更新的方式取决于您使用的IDE。 在Eclipse中,保存修改后的文件将致使类路径更新并触发重启。 在IntelliJ IDEA中,构建项目(
Build - > Make Project
)将具备相同的效果。
您也能够经过受支持的构建插件(即Maven和Gradle)启动您的应用程序,只要启用了分叉功能,由于DevTools须要隔离的应用程序类加载器才能正常运行。 当Gradle和Maven在类路径中检测到DevTools时,默认会这样作。
与LiveReload一块儿使用时,自动从新启动的效果很是好。 详情请参阅下文。 若是使用JRebel,自动从新启动将被禁用,以支持动态类从新加载。 其余devtools功能(如LiveReload和属性覆盖)仍然可使用。
DevTools依靠应用程序上下文的关闭挂钩在从新启动期间关闭它。 若是您禁用了关闭挂钩(
SpringApplication.setRegisterShutdownHook(false)
),它将没法正常工做。
当肯定类路径上的条目在更改时会触发从新启动时,DevTools会自动忽略名为spring-boot,
spring-boot-devtools
,spring-boot-autoconfigure
,spring-boot-actuator
和spring-boot-starter
的项目。
DevTools须要自定义
ApplicationContext
使用的ResourceLoader
:若是你的应用程序已经提供了一个,它将被打包。 不支持直接覆盖ApplicationContext
上的getResource
方法。
Spring Boot提供的重启技术经过使用两个类加载器来工做。 不改变的类(例如来自第三方jar的类)被加载到基类加载器中。 您正在开发的类将加载到重启类加载器中。 当应用程序从新启动时,重启类加载器将被丢弃,并建立一个新的。 这种方法意味着应用程序从新启动一般比“冷启动”快得多,由于基类加载器已经可用而且已经被填充了。
若是您发现从新启动对于您的应用程序来讲不够快,或者遇到类加载问题,则能够考虑从ZeroTurnaround中获取从新加载的技术,例如JRebel。 这些工做经过重写类,由于他们被加载,使他们更容易从新加载。 Spring Loaded提供了另外一种选择,可是它不支持许多框架,而且没有商业支持。
某些资源不必定须要在更改时触发从新启动。 例如,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.additional-exclude
属性。
spring.devtools.restart.additional-paths
属性来配置其余路径以监视更改。 您可使用上述的spring.devtools.restart.exclude
属性来控制额外路径下的更改是否会触发彻底从新启动或仅实时从新加载。spring.devtools.restart.enabled
属性将其禁用。 在大多数状况下,你能够在你的application.properties
中设置它(这将仍然初始化重启类加载器,但它不会监视文件的变化,也就是只要你从新启动了Spring Boot应用程序,都会冲洗初始化一个新的重启类加载器,上文也提到了)。(注意从新启动
和关闭后启动
的区别)SpringApplication.run(...)
以前设置System
属性。 例如:public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);
}
复制代码
spring.devtools.restart.trigger-file
属性。
您可能须要将
spring.devtools.restart.trigger-file
设置为全局设置,以便全部项目的行为方式相同
正如上面“从新启动vs从新加载”一节所述,从新启动功能是经过使用两个类加载器(一个基类,一个重启类)来实现的。 对于大多数应用程序来讲,这种方法运行良好,但有时候会致使类加载问题。
默认状况下,IDE中的任何打开的项目都将使用“重启”类加载器加载,任何常规的.jar
文件都将使用“基本”类加载器加载。 若是您使用多模块项目,而不是将每一个模块导入到IDE中,则可能须要自定义项目。 要作到这一点,你能够建立一个META-INF/spring-devtools.properties
文件。
spring-devtools.properties
文件能够包含restart.exclude
和restart.include
前缀属性。include
元素是应该被拉入到“重启”类加载器中的项目,排除元素是应该被下推到“基本”类加载器中的项目。 该属性的值是一个将应用于类路径的正则表达式模式。
好比:
restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
复制代码
全部的属性
key
(这里指上面的companycommonlibs
)必须是惟一的。 只要属性以restart.include
或restart.exclude
开头的都将被考虑。
全部类路径中的
META-INF/spring-devtools.properties
都将被加载。 您能够将文件打包到项目中,也能够打包到项目使用的库中。
ObjectInputStream
进行反序列化的对象,从新启动功能没法正常工做。 若是须要反序列化数据,则可能须要将Spring的ConfigurableObjectInputStream
与Thread.currentThread().getContextClassLoader()
一块儿使用。spring-boot-devtools
模块包含一个嵌入式LiveReload
服务器,当资源发生变化时,可用于触发浏览器刷新。 LiveReload
浏览器扩展可从livereload.com的Chrome,Firefox和Safari免费得到。spring.devtools.livereload.enabled
属性设置为false
。
一次只能运行一个LiveReload服务器。 在开始您的应用程序以前,请确保没有其余
LiveReload
服务器正在运行。 若是您从IDE启动多个应用程序,则只有第一个应用程序支持LiveReload
。
您能够经过将一个名为.spring-boot-devtools.properties
的文件添加到您的$HOME
文件夹来配置全局devtools设置(请注意,文件名以"."开头)。 添加到此文件的任何属性都将应用于使用devtools的计算机上的全部Spring Boot应用程序。 例如,要将从新启动配置为始终使用触发器文件,能够添加如下内容:
spring.devtools.reload.trigger-file=.reloadtrigger
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
属性,例如:
spring.devtools.remote.secret=mysecret
在远程应用程序上启用
spring-boot-devtools
存在安全风险。 您不该该在生产部署上启用支持。
远程devtools支持分为两部分, 即有一个接受链接的服务器端点以及您在IDE中运行的客户端应用程序。 当设置了spring.devtools.remote.secret
属性时,服务器组件会自动启用。 客户端组件必须手动启动。
org.springframework.boot.devtools.RemoteSpringApplication
。 传递给应用程序的非选项(non-option
)参数应该是您要链接到的远程URL。my-app
的项目部署到了Cloud Foundry,则能够执行如下操做:
Run
菜单中选择Run Configurations…
。Java Application
"launch configuration
"。my-app
项目。org.springframework.boot.devtools.RemoteSpringApplication
做为主类。https://myapp.cfapps.io
添加到Program arguments
(或任何远程URL)。. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) ) ' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: Spring Boot Remote :: 1.5.9.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.host
和spring.devtools.remote.proxy.port
属性。只有远程客户端正在运行时才会监视文件。 若是在启动远程客户端以前更改文件,则更新操做不会将其推送到远程服务器。
Java远程调试在诊断远程应用程序的问题时很是有用。 不幸的是,当您的应用程序部署在数据中心以外时,并不老是能够启用远程调试。 若是您使用基于容器的技术(例如Docker),则远程调试也可能会很是棘手。
为了帮助解决这些限制,devtools支持经过HTTP隧道传输远程调试流量。 远程客户端在端口8000
上提供本地服务器,您能够将其附加到远程调试器。 创建链接后,调试流量将经过HTTP发送到远程应用程序。 若是你想使用不一样的端口,你可使用spring.devtools.remote.debug.local-port
属性。
您须要确保您的远程应用程序在启用远程调试的状况下启动。 这一般能够经过配置JAVA_OPTS
来实现。 例如,使用Cloud Foundry
,您能够将如下内容添加到您的manifest.yml
中:
---
env:
JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n"
复制代码
请注意,您不须要将
address=NNNN
选项传递给-Xrunjdwp
。 若是省略,Java将简单地选取一个随机空闲端口。
经过Internet调试远程服务可能会很慢,您可能须要增长IDE中的超时时间。 例如,在Eclipse中,您能够选择
Java
→Preferences…``Debug
并将debug timeout(ms)
更改成更合适的值(在大多数状况下,60000
的效果很好)。
在IntelliJ IDEA中使用远程调试通道时,必须将全部断点配置为挂起线程而不是VM。 默认状况下,IntelliJ IDEA中的断点会暂停整个虚拟机,而不是仅挂起遇到断点的线程。 这具备暂停管理远程调试通道的线程的不良反作用,致使您的调试会话冻结。 在IntelliJ IDEA中使用远程调试通道时,应将全部断点配置为挂起线程而不是VM。 请参阅IDEA-165769了解更多详情。
jars
可用于生产部署。 因为它们是独立的,所以它们也很是适合基于云的部署。spring-boot-actuator
。 有关详细信息,请参阅第五部分“spring-boot-actuator:生产就绪功能” 。