spring_reference/IV. Spring Boot features/22.3. Fluent builder API.md

12 lines
874 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 22.3. 流畅的构建API
如果你需要创建一个分层的ApplicationContext多个具有父子关系的上下文或你只是喜欢使用流畅的构建API你可以使用SpringApplicationBuilder。SpringApplicationBuilder允许你以链式方式调用多个方法包括可以创建层次结构的parent和child方法。
```java
new SpringApplicationBuilder()
.showBanner(false)
.sources(Parent.class)
.child(Application.class)
.run(args);
```
**注**创建ApplicationContext层次时有些限制比如Web组件(components)必须包含在子上下文(child context)中且相同的Environment即用于父上下文也用于子上下文中。具体参考[SpringApplicationBuilder javadoc](http://docs.spring.io/spring-boot/docs/1.2.2.BUILD-SNAPSHOT/api/org/springframework/boot/builder/SpringApplicationBuilder.html)