From 13178d8b20eed706179b975c86c417c5119b70b8 Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Mon, 23 Mar 2015 23:40:58 +0800 Subject: [PATCH] Update 62.1. Troubleshoot auto-configuration.md --- .../62.1. Troubleshoot auto-configuration.md | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/IX. ‘How-to’ guides/62.1. Troubleshoot auto-configuration.md b/IX. ‘How-to’ guides/62.1. Troubleshoot auto-configuration.md index 818b8c9..7fda985 100644 --- a/IX. ‘How-to’ guides/62.1. Troubleshoot auto-configuration.md +++ b/IX. ‘How-to’ guides/62.1. Troubleshoot auto-configuration.md @@ -5,6 +5,7 @@ Spring Boot自动配置总是尝试尽最大努力去做正确的事,但有时 在每个Spring Boot `ApplicationContext`中都存在一个相当有用的`ConditionEvaluationReport`。如果开启`DEBUG`日志输出,你将会看到它。如果你使用`spring-boot-actuator`,则会有一个`autoconfig`的端点,它将以JSON形式渲染该报告。可以使用它调试应用程序,并能查看Spring Boot运行时都添加了哪些特性(及哪些没添加)。 通过查看源码和javadoc可以获取更多问题的答案。以下是一些经验: + 1. 查找名为`*AutoConfiguration`的类并阅读源码,特别是`@Conditional*`注解,这可以帮你找出它们启用哪些特性及何时启用。 将`--debug`添加到命令行或添加系统属性`-Ddebug`可以在控制台查看日志,该日志会记录你的应用中所有自动配置的决策。在一个运行的Actuator app中,通过查看`autoconfig`端点(`/autoconfig`或等效的JMX)可以获取相同信息。 2. 查找是`@ConfigurationProperties`的类(比如[ServerProperties](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java))并看下有哪些可用的外部配置选项。`@ConfigurationProperties`类有一个用于充当外部配置前缀的`name`属性,因此`ServerProperties`的值为`prefix="server"`,它的配置属性有`server.port`,`server.address`等。在运行的Actuator应用中可以查看`configprops`端点。 @@ -21,11 +22,3 @@ Spring Boot自动配置总是尝试尽最大努力去做正确的事,但有时 3. 以声明方式为所有应用注册自定义,通过添加一个`META-INF/spring.factories`并打包成一个jar文件(该应用将它作为一个库)来实现。 `SpringApplication`会给监听器(即使是在上下文被创建之前就存在的)发送一些特定的`ApplicationEvents`,然后也会注册监听`ApplicationContext`发布的事件的监听器。查看Spring Boot特性章节中的[Section 22.4, “Application events and listeners” ](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-events-and-listeners)可以获取一个完整列表。 - -* 构建ApplicationContext层次结构(添加父或根上下文) - -你可以使用`ApplicationBuilder`类创建父/根`ApplicationContext`层次结构。查看'Spring Boot特性'章节的[Section 22.3, “Fluent builder API” ](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-fluent-builder-api)获取更多信息。 - -* 创建一个非web(non-web)应用 - -不是所有的Spring应用都必须是web应用(或web服务)。如果你想在main方法中执行一些代码,但需要启动一个Spring应用去设置需要的底层设施,那使用Spring Boot的`SpringApplication`特性可以很容易实现。`SpringApplication`会根据它是否需要一个web应用来改变它的`ApplicationContext`类。首先你需要做的是去掉servlet API依赖,如果不能这样做(比如,基于相同的代码运行两个应用),那你可以明确地调用`SpringApplication.setWebEnvironment(false)`或设置`applicationContextClass`属性(通过Java API或使用外部配置)。你想运行的,作为业务逻辑的应用代码可以实现为一个`CommandLineRunner`,并将上下文降级为一个`@Bean`定义。