From f3a392165e1ae6c49c6317b30b7c6590c6cf9bcc Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Mon, 23 Mar 2015 23:46:20 +0800 Subject: [PATCH] Update 62.2. Customize the Environment or ApplicationContext before it starts.md --- ...the Environment or ApplicationContext before it starts.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/IX. ‘How-to’ guides/62.2. Customize the Environment or ApplicationContext before it starts.md b/IX. ‘How-to’ guides/62.2. Customize the Environment or ApplicationContext before it starts.md index e69de29..75a31e5 100644 --- a/IX. ‘How-to’ guides/62.2. Customize the Environment or ApplicationContext before it starts.md +++ b/IX. ‘How-to’ guides/62.2. Customize the Environment or ApplicationContext before it starts.md @@ -0,0 +1,9 @@ +### 62.2. 启动前自定义Environment或ApplicationContext + +每个`SpringApplication`都有`ApplicationListeners`和`ApplicationContextInitializers`,用于自定义上下文(context)或环境(environment)。Spring Boot从`META-INF/spring.factories`下加载很多这样的内部使用的自定义。有很多方法可以注册其他的自定义: + +* 以编程方式为每个应用注册自定义,通过在SpringApplication运行前调用它的`addListeners`和`addInitializers`方法来实现。 +* 以声明方式为每个应用注册自定义,通过设置`context.initializer.classes`或`context.listener.classes`来实现。 +* 以声明方式为所有应用注册自定义,通过添加一个`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)可以获取一个完整列表。