SpringBoot官方文档翻译(二十七):远程应用

20.5 Remote Applications(远程应用)

1
2
3
4
5
The Spring Boot developer tools are not limited to local development.     
You can also use several features when running applications remotely.
Remote support is opt-in. To enable it, you need to make sure that 
devtools is included in the repackaged archive, as shown in the following
listing:

Spring Boot的开发者工具不仅仅局限于本地开发,您同样可以使用一些远程应用运行时的功能。远程支持为可选项。去开启它,您需要确认devtools已经被引入到重新打包的包中,如下:

1
2
3
4
5
6
7
8
9
10
11
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>

1
2
Then you need to set a spring.devtools.remote.secret property,     
as shown in the following example:

然后您需要设置spring.devtools.remote.secret属性,如下:

1
spring.devtools.remote.secret=mysecret

1
2
Enabling spring-boot-devtools on a remote application is a security     
risk. You should never enable support on a production deployment.

远程开启spring-boot-devtools是一项安全风险。您在生产环境应该永远不要开启这项功能。

1
2
3
4
5
Remote devtools support is provided in two parts: a server-side     
endpoint that accepts connections and a client application that
you run in your IDE. The server component is automatically enabled when
the spring.devtools.remote.secret property is set. The client component
must be launched manually.

远程devtools通过两部分来提供支持:一个是服务端的断点接受连接,一个客户端应用能够运行于您的IDE。spring.devtools.remote.secret如果被设置,服务端的组件会自动的开启。客户端组件必须手动启动。

20.5.1 Running the Remote Client Application(运行远程客户端应用)

1
2
3
4
The remote client application is designed to be run from within your IDE.     
You need to run org.springframework.boot.devtools.RemoteSpringApplication with
the same classpath as the remote project that you connect to. The application’s
single required argument is the remote URL to which it connects.

远程客户端的设计是为了能够从您本地IDE运行。您需要设置org.springframework.boot.devtools.RemoteSpringApplication与您连接的远程应用同样的类路径。您连接应用唯一需要的参数即远程URL。

1
2
For example, if you are using Eclipse or STS and you have a project     
named my-app that you have deployed to Cloud Foundry, you would do the following:

举个例子,如果您使用Eclipse或者STS并且您有一个名为my-app的工程,您部署在云端,您需要做如下的操作:

1
2
3
4
5
•	Select Run Configurations…  from the Run menu.
• Create a new Java Application “launch configuration”.
• Browse for the my-app project.
• Use org.springframework.boot.devtools.RemoteSpringApplication as the main class.
• Add https://myapp.cfapps.io to the Program arguments (or whatever your remote URL is).

一个运行成功的远程客户端应用应该如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
 .   ____          _                                              __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: Spring Boot Remote :: 2.1.0.BUILD-SNAPSHOT

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)

1
2
3
4
Because the remote client is using the same classpath as the real     
application it can directly read application properties. This is how
the spring.devtools.remote.secret property is read and passed to the
server for authentication.

因为远程客户端正在使用与实际相同的类路径,它可以直接读取应用程序的属性.这是spring.devtools.remote.secret如何被服务端读取并通过的授权的方式。

1
2
It is always advisable to use https:// as the connection protocol,      
so that traffic is encrypted and passwords cannot be intercepted.

应用一直是以https://作为连接协议,所以报文是被加密并且密码不能够被拦截的。

1
2
3
If you need to use a proxy to access the remote application,configure    
the spring.devtools.remote.proxy.host and spring.devtools.remote.proxy.port 
properties.

如果您需要使用代理进入远程应用,配置spring.devtools.remote.proxy.hostspring.devtools.remote.proxy.port属性。

20.5.2 Remote Update(远程更新)

1
2
3
4
5
6
The remote client monitors your application classpath for changes in the     
same way as the local restart. Any updated resource is pushed to the
remote application and (if required) triggers a restart. This can be
helpful if you iterate on a feature that uses a cloud service that you
do not have locally. Generally, remote updates and restarts are much quicker
than a full rebuild and deploy cycle.

远程客户端监视您的类路径下的改变与本地重启相似。任何资源的额更新将会被推到远程应用中,并且(如果需要的话0触发重启。这个对于您本地没有,而在云服务端运行的服务很有帮助。通常,远程更新和重启比整个重新编译部署生命周期更短。

1
2
3
Files are only monitored when the remote client is running. If you     
change a file before starting the remote client, it is not pushed
to the remote server.

文件仅在远程客户端运行的时候才会被监控到。如果您在启动远程客户端之前改变文件,它将不会被推送到远程服务器上。

分享到

SpringBoot官方文档翻译(二十六):已知限制&LiveReload&全局设置

20.2.7 Known Limitations(已知限制)

1
2
3
4
Restart functionality does not work well with objects that are     
deserialized by using a standard ObjectInputStream. If you need
to deserialize data, you may need to use Spring’s ConfigurableObjectInputStream 
in combination with Thread.currentThread().getContextClassLoader().

重新启动功能不适用于那些通过使用标准的ObjectInputStream进行反序列化的对象。如果您需要反序列化数据,你可能需要使用Spring的ConfigurableObjectInputStream结合使用Thread.currentThread().getContextClassLoader()

1
2
3
Unfortunately, several third-party libraries deserialize without     
considering the context classloader. If you find such a problem,
you need to request a fix with the original authors.

不幸的是,一些第三方的库反序列化的时候未考虑容器类加载器。如果您发现了这个问题,您需要请求作者做相应的修改。

20.3 LiveReload

1
2
3
4
The spring-boot-devtools module includes an embedded LiveReload server     
that can be used to trigger a browser refresh when a resource is changed.
LiveReload browser extensions are freely available for Chrome, Firefox
and Safari from livereload.com.

spring-boot-devtools模块引入了内置LiveReload服务器,当一个资源发生改变时,能够触发浏览器自动刷新,LiveReload浏览器扩展对于Chrome和Firefox还有Safari提供免费支持(livereload.com)。

1
2
If you do not want to start the LiveReload server when your application     
runs, you can set the spring.devtools.livereload.enabled property to false.

如果您子在您的应用运行的时候不想启动LiveReload服务器,您可以通过spring.devtools.livereload.enabled属性来设置成false。

1
2
3
4
You can only run one LiveReload server at a time. Before starting your    
application, ensure that no other LiveReload servers are running. If
you start multiple applications from your IDE, only the first has
LiveReload support.

您一次只能运行一个LiveReload服务器。在启动您的应用之前,确保没有其他的LiveReload服务器在运行。如果您在IDE中运行多个应用,仅仅第一个运行的应用能够有LiveReload的效果。

20.4 Global Settings(全局设置)

1
2
3
4
5
6
You can configure global devtools settings by adding a file named     
.spring-boot-devtools.properties to your $HOME folder (note that
the filename starts with “.”). Any properties added to this file
apply to all Spring Boot applications on your machine that use
devtools. For example, to configure restart to always use a trigger
file, you would add the following property:

您可以通过增加名为.spring-boot-devtools.properties(注意文件名以.开头的)的文件到您的$HOME文件夹来配置全局的devtools设置。任何属性被增加到这个文件将会被应用生效到所有的您机器上使用devtools的Spring Boot应用中。举个例子,去配置重启使用一个触发文件,您可以增加如下属性:

~/.spring-boot-devtools.properties.

1
spring.devtools.reload.trigger-file=.reloadtrigger

分享到

SpringBoot官方文档翻译(二十五):使用触发文件&定制“重启”类加载器

20.2.5 Using a Trigger File(使用触发文件)

1
2
3
4
5
6
7
If you work with an IDE that continuously compiles changed files,     
you might prefer to trigger restarts only at specific times. To do
so, you can use a “trigger file”, which is a special file that must
be modified when you want to actually trigger a restart check.
Changing the file only triggers the check and the restart only occurs
if Devtools has detected it has to do something. The trigger file can
be updated manually or with an IDE plugin.

如果您使用持续编译更改文件的IDE,您可能更愿意仅在特定时间触发重新启动。如果那样的话,您可以使用一个“触发文件”,当您需要触发重启的时候,您必须通过修改这个特殊的文件触发。这个文件的改变仅仅触发检查并且只是重启Devtools已经发现需要做的一些事情。这个触发文件能够被手动更新或者通过IDE的插件更新。

1
2
To use a trigger file, set the spring.devtools.restart.trigger-file     
property to the path of your trigger file.

想使用这个触发文件,设置spring.devtools.restart.trigger-file属性到您的触发文件。

1
2
You might want to set spring.devtools.restart.trigger-file as     
a global setting, so that all your projects behave in the same way.

您也许想要设置set spring.devtools.restart.trigger-file为一个全局的设置,那样的话您所有的项目都能通过这样的方式统一行为。

20.2.6 Customizing the Restart Classloader(自定义重启类加载器)

1
2
3
4
As described earlier in the Restart vs Reload section, restart     
functionality is implemented by using two classloaders. For most
applications, this approach works well. However, it can sometimes
cause classloading issues.

如前重启和重载章节所述,重启功能是由两个类加载器来实现的。对于大多数应用,这种方式工作正常。然而,它有时候也会导致一些类加载问题。

1
2
3
4
5
By default, any open project in your IDE is loaded with the “restart”     
classloader, and any regular .jar file is loaded with the “base”
classloader. If you work on a multi-module project, and not every
module is imported into your IDE, you may need to customize things.
To do so, you can create a META-INF/spring-devtools.properties file.

默认情况下,任何您使用IDE打开的项目都是被“重启”类加载器加载,并且任何常规的.jar文件都会被“base”类加载器加载。如果您在一个多模块工作的项目中,并且并不是所有的模块都是导入到您的IDE,您也许需要定制一些东西。为此,您可以创建一个META-INF/spring-devtools.properties文件。

1
2
3
4
5
6
7
The spring-devtools.properties file can contain properties     
prefixed with restart.exclude and restart.include. The include 
elements are items that should be pulled up into the “restart”
classloader, and the exclude elements are items that should be
pushed down into the “base” classloader. The value of the property
is a regex pattern that is applied to the classpath, as shown in
the following example:

spring-devtools.properties文件能够包含前缀为restart.excluderestart.include属性。include元素项目将会被拉取到“重启”类加载器中,“exclude”元素项目将会提交至“base”类加载器。这些属性的值是一个应用于类路径的正则表达式模式,如下:

1
2
restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar

1
2
All property keys must be unique. As long as a property     
starts with restart.include. or restart.exclude. it is considered.

所有的属性keys必须是唯一的。只要属性的前置是restart.include或者restart.exclude就会被考虑到。

1
2
3
All META-INF/spring-devtools.properties from the classpath are loaded.     
You can package files inside your project, or in the libraries that
the project consumes.

所有类路径下的META-INF/spring-devtools.properties都将会被加载。您可以打包文件进入您的项目或者在项目依赖的lib库中。

分享到

SpringBoot官方文档翻译(二十三):条件日志&资源排除

20.2.1 Logging changes in condition evaluation(记录条件评估中的更改)

1
2
3
4
By default, each time your application restarts, a report showing     
the condition evaluation delta is logged. The report shows the
changes to your application’s auto-configuration as you make changes
such as adding or removing beans and setting configuration properties.

默认情况下,每次您重启您的应用,一个条件评估的增量日志将被打印在控制台。这个日志展示了您应用自动配置部分修改得内容,注入增加,删除beans或者设置配置属性。

1
To disable the logging of the report, set the following property:

禁用以上的日志报告,如下设置:

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

20.2.2 Excluding Resources(排除资源)

1
2
3
4
5
6
7
8
Certain resources do not necessarily need to trigger a restart      
when they are changed. For example, Thymeleaf templates can be
edited in-place. By default, changing resources in /META-INF/maven, 
/META-INF/resources, /resources, /static, /public, or /templates 
does not trigger a restart but does trigger a live reload. If you
want to customize these exclusions, you can use
the spring.devtools.restart.exclude property. For example, to exclude
only /static and /public you would set the following property:

某些资源没有必要在修改得时候出发重启。举个例子,Thymeleaf模板能够就地编辑。默认情况下,改变/META-INF/maven,
/META-INF/resources, /resources, /static, /public, 或者 /templates
这些路径下的内容,并不需要触发重启,但会触发重载。如果您想自定义排除这些,您可以使用spring.devtools.restart.exclude 属性。举个例子,仅仅排除/static 和 /public,您可以设置以下属性:

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

1
2
If you want to keep those defaults and add additional exclusions,    
use the spring.devtools.restart.additional-exclude property instead.

如果您想保留这些默认配置,并增加一些额外的排除项目,使用spring.devtools.restart.additional-exclude属性代替。

分享到

SpringBoot官方文档翻译(二十四):监控额外的路径&禁用重启

20.2.3 Watching Additional Paths(监控额外的路径)

1
2
3
4
5
6
7
You may want your application to be restarted or reloaded when you make     
changes to files that are not on the classpath. To do so, use
the spring.devtools.restart.additional-paths property to configure
additional paths to watch for changes. You can use
the spring.devtools.restart.exclude property described earlier to
control whether changes beneath the additional paths trigger a full
restart or a live reload.

有时候,当您修改的文件不在类路径下的时候,您可能也需要您的应用重启或者重载。这样做,使用spring.devtools.restart.additional-paths属性去配置额外的路径去监控它是否发生了改变,并根据这个改变去重启。您可以使用spring.devtools.restart.exclude属性,如前所述
控制额外路径下的更改是否触发完整重新启动或实时重新加载。

20.2.4 Disabling Restart(禁用重启)

1
2
3
4
5
If you do not want to use the restart feature, you can disable     
it by using the spring.devtools.restart.enabled property. In
most cases, you can set this property in your application.properties 
(doing so still initializes the restart classloader, but it does not
watch for file changes).

如果您不喜欢用重启功能,您可以通过spring.devtools.restart.enabled属性禁用它。在大部分情况下,您可以在您的application.properties中设置它。(如果设置了它,始终会初始化“重启”类加载器,但是不监控文件的修改)。

1
2
3
4
If you need to completely disable restart support (for example,     
because it does not work with a specific library), you need to set
the spring.devtools.restart.enabled System property to false before
calling SpringApplication.run(… ), as shown in the following example:

如果您想完全禁止重启功能(举个例子,它不能用于某个特殊的库),您需要在调用SpringApplication.run(… )之前设置spring.devtools.restart.enabled系统属性为fasle,如下:

1
2
3
4
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);
}

分享到

SpringBoot官方文档翻译(二十二):自动重启

20.2 Automatic Restart(自动重启)

1
2
3
4
5
6
7
Applications that use spring-boot-devtools automatically restart     
whenever files on the classpath change. This can be a useful
feature when working in an IDE, as it gives a very fast feedback
loop for code changes. By default, any entry on the classpath
that points to a folder is monitored for changes. Note that
certain resources, such as static assets and view templates, 
do not need to restart the application.

如果类路径下的文件被改动了,您的应用将会使用spring-boot-devtools自动重启。这将会是一个作用于您的IDE的非常有用的功能,它将会以最快的速度给您的代码修改做出反馈。默认情况下,任何类路径下的东西和文件夹中的东西都将会被监控是否被修改。注意某些资源(如静态资产和视图模板)不需要重新启动应用程序。

1
2
3
4
5
6
7
8
Triggering a restart
As DevTools monitors classpath resources, the only way to
trigger a restart is to update the classpath. The way in
which you cause the classpath to be updated depends on the
IDE that you are using. In Eclipse, saving a modified file
causes the classpath to be updated and triggers a restart.
In IntelliJ IDEA, building the project (Build -> Build Project)
has the same effect.

触发重启
由于DevTools监控类路径下的资源,所以只有更新类路径资源才能触发重启。如何触发类路径资源变更从而触发重启取决于您使用的IDE。在Eclipse中,保存修改文件将会触发重启,在IDEA中,构建项目会产生同样的效果。(Build -> Build Project)

1
2
3
4
5
As long as forking is enabled, you can also start your     
application by using the supported build plugins (Maven
and Gradle), since DevTools needs an isolated application
classloader to operate properly. By default, Gradle and Maven
do that when they detect DevTools on the classpath.

只要使用分叉,您可以使用插件(Maven和Gradle)启动您的应用,因为DevTools需要隔离应用类加载器去操作属性。默认情况下,当他们在类路径中检测到DevTools时,Gradle和Maven会这样做。

1
2
3
4
5
Automatic restart works very well when used with LiveReload.     
See the LiveReload section for details. If you use JRebel,
automatic restarts are disabled in favor of dynamic class
reloading. Other devtools features (such as LiveReload and
property overrides) can still be used.

与LiveReload一起使用时,自动重启的效果非常好。可以查看20.3-LiveReload章节了解更多细节。如果你使用JRebel,自动重新启动被禁用,以支持动态类重装。 其他devtools功能(如LiveReload和属性覆盖)仍然可以使用。

1
2
3
DevTools relies on the application context’s shutdown hook to     
close it during a restart. It does not work correctly if you have
disabled the shutdown hook (SpringApplication.setRegisterShutdownHook(false)).

重启的时候,DevTools依赖应用上下文的关闭钩子去关闭它。如果您禁用了关闭钩子(SpringApplication.setRegisterShutdownHook(false)),它将无法正常工作。

1
2
3
4
When deciding if an entry on the classpath should trigger a     
restart when it changes, DevTools automatically ignores
projects named spring-boot,spring-boot-devtools, spring-boot-
autoconfigure, spring-boot-actuator, and spring-boot-starter.

当决策类路径下某个条目改变了是否应该触发重启的时候,DevTools自动忽略那些以spring-boot,spring-boot-devtools, spring-boot-autoconfigure, spring-boot-actuator, and spring-boot-starter命名的项目名称。

1
2
3
4
DevTools needs to customize the ResourceLoader used by the     
ApplicationContext. If your application provides one already,
it is going to be wrapped. Direct override of the getResource 
method on the ApplicationContext is not supported.

DevTools需要自定义ApplicationContext中使用的资源加载器(ResourceLoader)。如果您的应用已经提供了一个,他将被代理包裹起来。不允许直接重写ApplicationContext中的getResource方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
Restart vs Reload
The restart technology provided by Spring Boot works by using
two classloaders. Classes that do not change (for example, those
from third-party jars) are loaded into a base classloader. Classes
that you are actively developing are loaded into a restart classloader.
When the application is restarted, the restart classloader is thrown
away and a new one is created. This approach means that application
restarts are typically much faster than “cold starts”, since the base 
classloader is already available and populated.
If you find that restarts are not quick enough for your applications
or you encounter classloading issues, you could consider reloading
technologies such as JRebelfrom ZeroTurnaround. These work by rewriting
classes as they are loaded to make them more amenable to reloading.

重启和重载
Spring Boot是通过使用2个类加载器来提供重启技术的。不被修改得类(例如第三方jar)将会被加载到一个基础的类加载器中。那些您真实要修改的类将会被加载到一个“重启”类加载器中。当应用重启的时候,“重启”类加载器将会被抛弃并重新创建一个新的“重启”类加载器。这种方法意味着应用程序重新启动通常比“冷启动”快得多,因为基类加载器已经可用并且已被填充。
如果您发现重启对于您的应用程序来说不够快,或者遇到类加载问题,则可以考虑从ZeroTurnaround中重新加载技术,例如JRebel。 这些工作通过在加载类时重写类来使它们更易于重新加载。

分享到

SpringBoot官方文档翻译(二十):开发者工具

20. Developer Tools(开发者工具)

1
2
3
4
5
6
Spring Boot includes an additional set of tools that can     
make the application development experience a little more
pleasant. The spring-boot-devtools module can be included
in any project to provide additional development-time features.
To include devtools support, add the module dependency to
your build, as shown in the following listings for Maven and Gradle:

Spring Boot引入了额外的一些工具,让应用的开发体验变得更加愉快。spring-boot-devtools模块能够被任何项目引入提供“开发-时间”功能。如果需要支持devtools功能,增加如下模块到您的构建李,如下:
Maven:

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

Gradle:

1
2
3
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}

1
2
3
4
5
6
7
Developer tools are automatically disabled when running a      
fully packaged application. If your application is launched
from java -jar or if it is started from a special classloader,
then it is considered a “production application”. Flagging the
dependency as optional in Maven or using compileOnly in Gradle
is a best practice that prevents devtools from being transitively
applied to other modules that use your project.

当运行一个完整的应用jar包的时候,开发者工具集将会被自动禁用。如果您的应用是通过java -jar启动或者是通过一些特殊的类加载器启动的,它会被当做一个“产品级别的应用”。在Maven中标记devtools的依赖为可选的,在Gradle中使用compileOnly是防止devtools被传递依赖到您的其他模块的的最佳实践。

1
2
3
4
Repackaged archives do not contain devtools by default. If     
you want to use a certain remote devtools feature, you need
to disable the excludeDevtoolsbuild property to include it.
The property is supported with both the Maven and Gradle plugins.

重新打包的档案在默认情况下不包含devtools。如果您想使用某个远程的devtools功能,您需要禁用excludeDevtoolsbuild属性去支持它。这个属性既支持Maven也支持Gradle。

分享到

SpringBoot官方文档翻译(二十一):默认属性

20.1 Property Defaults(默认属性)

1
2
3
4
5
Several of the libraries supported by Spring Boot use     
caches to improve performance. For example, template
engines cache compiled templates to avoid repeatedly
parsing template files. Also, Spring MVC can add HTTP
caching headers to responses when serving static resources.

Spring Boot支持几个库使用缓存来提升性能。比如,模板引擎缓存编译模板以避免重复
解析模板文件。同样的,Spring MVC在服务静态资源的时候,能将HTTP的header缓存到应答当中。

1
2
3
4
5
While caching is very beneficial in production, it can be     
counter-productive during development, preventing you from
seeing the changes you just made in your application.
For this reason, spring-boot-devtools disables the caching
options by default.

虽然缓存在生产中非常有益,但在开发阶段有时候会适得其反,阻止您看到您应用最新的改动。基于此,spring-boot-devtools会默认禁用缓存。

1
2
3
4
5
6
Cache options are usually configured by settings in     
your application.properties file. For example, Thymeleaf
offers the spring.thymeleaf.cache property. Rather than
needing to set these properties manually, the 
spring-boot-devtools module automatically applies sensible
development-time configuration.

缓存选项通常会被配置于您的application.properties文件。举个例子,Thymeleaf提供了spring.thymeleaf.cache属性。spring-boot-devtools将会自动应用合理的配置,而不是通过手动设置这些属性。

1
2
For a complete list of the properties that are applied     
by the devtools, see DevToolsPropertyDefaultsPostProcessor.

想要查看devtools完整的属性,请参考“https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java”

分享到

SpringBoot官方文档翻译(十九):运行您的应用

19. Running Your Application(运行您的应用)

1
2
3
4
5
One of the biggest advantages of packaging your application as     
a jar and using an embedded HTTP server is that you can run your
application as you would any other. Debugging Spring Boot
applications is also easy. You do not need any special IDE plugins
or extensions.

将您的应用打包成一个jar并且使用内置的HTTP服务器有一个巨大的优势就是您可以直接运行您的应用。调试Spring Boot应用也是非常简单的。您不需要任何特殊的IDE插件或者扩展。

1
2
3
This section only covers jar based packaging. If you choose     
to package your application as a war file, you should refer
to your server and IDE documentation.

本节只讲述基于jar包打包的方式。如果你选择将您的应用打包成一个war包,您应该参考
到您的服务器和IDE文档。

19.1 Running from an IDE(从一个IDE钟运行)

1
2
3
4
5
You can run a Spring Boot application from your IDE as a simple     
Java application. However, you first need to import your project.
Import steps vary depending on your IDE and build system. Most
IDEs can import Maven projects directly. For example, Eclipse
users can select Import…  → Existing Maven Projects from the File menu.

您可以从您的IDE将Spring Boot应用作为一个简单的java应用来运行。然而,您首先需要导入您的工程。导入步骤根据您的IDE构建系统的不同而变化。大部分IDE能够直接导入Maven工程。举个例子,Eclipse用户可以选择File-Import-Existing Maven Projects菜单。

1
2
3
4
If you cannot directly import your project into your IDE,     
you may be able to generate IDE metadata by using a build plugin.
Maven includes plugins for Eclipse and IDEA. Gradle offers plugins
for various IDEs.

如果您不能直接从IDE中导入您的工程,您可以通过构建插件生成IDE metadata。Maven包含Eclipse和IDEA的插件。Gradle为大部分IDE提供了插件

1
2
3
4
If you accidentally run a web application twice, you see     
a “Port already in use” error. STS users can use the Relaunch 
button rather than the Run button to ensure that any existing
instance is closed.

如果您偶然的运行了2次web应用,您会发现“Port already in use”端口已经被使用的报错.STS使用者,能够使用Relaunch按钮来避免这个问题,如果使用Run按钮请确认任何实例都已经处于关闭状态。

19.2 Running as a Packaged Application(作为一个大包应用运行)

1
2
3
If you use the Spring Boot Maven or Gradle plugins to create     
an executable jar, you can run your application using java -jar,
as shown in the following example:

如果您使用Spring Boot Maven或者Gradle插件创建了一个可执行的jar,您可以通过java -jar命令来运行它,如下例子:

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

1
2
3
It is also possible to run a packaged application with remote     
debugging support enabled. Doing so lets you attach a debugger
to your packaged application, as shown in the following example:

同样可以通过远程调试模式运行一个已经打包的应用,如果远程调试开启的情况下。这样做的话您可以链接一个断点到您的已经打包的应用中,如下例子:

1
2
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myapplication-0.0.1-SNAPSHOT.jar

19.3 Using the Maven Plugin(使用Maven插件)

1
2
3
4
The Spring Boot Maven plugin includes a run goal that can     
be used to quickly compile and run your application. Applications
run in an exploded form, as they do in your IDE. The following
example shows a typical Maven command to run a Spring Boot application:

Spring Boot的Maven插件包含了一个运行目标可以快速编译和运行您的应用。应用像在您的IDE中以分解的形式运行。接下去的例子展示了一个典型的Maven命令运行Spring Boot 应用。

1
$ mvn spring-boot:run

1
2
You might also want to use the MAVEN_OPTS operating system     
environment variable, as shown in the following example:

您也许也想使用MAVEN_OPTS操作系统环境变量,如下例子:

1
$ export MAVEN_OPTS=-Xmx1024m

19.4 Using the Gradle Plugin(使用Gradle插件)

1
2
3
4
The Spring Boot Gradle plugin also includes a bootRun task that can     
be used to run your application in an exploded form. The bootRun 
task is added whenever you apply the org.springframework.boot and 
java plugins and is shown in the following example:

Spring Boot的Gradle插件同样映入了bootRun任务,能够运用分解的方式运行您的应用。bootRun任务将会被自动引入,如果您引入了org.springframework.boot和java插件。如下例子:

1
$ gradle bootRun

1
2
You might also want to use the JAVA_OPTS operating system     
environment variable, as shown in the following example:

您也许想使用JAVA_OPTS操作系统环境变量,如下例子:

1
$ export JAVA_OPTS=-Xmx1024m

19.5 Hot Swapping(热交换)

1
2
3
4
Since Spring Boot applications are just plain Java applications,     
JVM hot-swapping should work out of the box. JVM hot swapping is
somewhat limited with the bytecode that it can replace. For a
more complete solution, JRebel can be used.

由于Spring Boot应用程序只是普通的Java应用程序,所以JVM热插拔应该可以开箱即用。JVM热插拔是
在可以替代的字节码方面有所限制。需要更完整的解决方案,可以使用JRebel。

1
2
3
4
The spring-boot-devtools module also includes support for     
quick application restarts. See the Chapter 20, Developer
Tools section later in this chapter and theHot swapping
“How-to” for details.

spring-boot-devtools模块也宝行了支持快速重启应用的功能。查看章节20,开发者工具章节接下去会讲这点,已经热插拔相关细节。

分享到

SpringBoot官方文档翻译(十八):使用@SpringBootApplication注解

18. Using the @SpringBootApplication Annotation(使用@SpringBootApplication注解)

1
2
3
4
Many Spring Boot developers like their apps to use auto-configuration,     
component scan and be able to define extra configuration on their
"application class". A single@SpringBootApplication annotation
can be used to enable those tree features, that is:

很多Spring Boot的开发者想要他们的应用能够使用自动配置,组件扫描和定义额外的配置在他们的“主类”里。一个单独的@SpringBootApplication注解即可将这3个功能都实现,也就是:

1
2
3
* @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
* @ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
* @Configuration: allow to register extra beans in the context or import additional configuration classes
  • @EnableAutoConfiguration: 开启Spring Boot的自动配置机制
  • @ComponentSca:开启主类路径包下的组件自动扫描
  • @Configuration :允许注册额外的beans到context容器中,或者导入更多的配置类
    1
    2
    3
    4
    The @SpringBootApplication annotation is equivalent     
    to using @Configuration, @EnableAutoConfiguration,
    and @ComponentScan with their default attributes,
    as shown in the following example:

@SpringBootApplication注解等同于使用@Configuration, @EnableAutoConfiguration,
和 @ComponentScan
这3个注解,并且使用了他们默认的属性配置,如下例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}

}

1
2
@SpringBootApplication also provides aliases to customize the     
attributes of @EnableAutoConfiguration and @ComponentScan.

@SpringBootApplication注解同样提供别名去定制化@EnableAutoConfiguration@EnableAutoConfiguration属性。

1
2
3
4
None of these features are mandatory and you may chose to     
replace this single annotation by any of the features that it
enables. For instance, you may not want to use component scan
in your application:

这些功能都不是强制性的,您可以选择通过任何其他的开启的功能去替换这个单一的注解。
例如,您可能不想使用组件扫描在你的应用中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}

}

1
2
3
4
In this example, Application is just like any other Spring Boot     
application except that @Component-annotated classes are not
detected automatically and the user-defined beans are imported
explicitly (see @Import).

在上面这个例子中,应用如同其他Spring Boot应用一样,
除了组件注解的类不是被自动导入以及用户自定义beans被明确导入(见@Import)。

分享到