23.4 Fluent builder API

如果需要创建一个分层的ApplicationContext(多个具有父子关系的上下文,子ApplicationContext的getBean可以从父ApplicationContext获取,我们可以通过xml,@Configuration来配置ApplicationContext-应用上下文-用来管理bean的),或只是喜欢使用流式(fluent)构建API,那你可以使用SpringApplicationBuilderSpringApplicationBuilder允许你以链式方式调用多个方法,包括parent和child方法,这样就可以创建多层次结构,例如:

new SpringApplicationBuilder()
        .sources(Parent.class)
        .child(Application.class)
        .bannerMode(Banner.Mode.OFF)
        .run(args);

以上的做法与实例化SpringApplication后进行设置是一样的run(args)就启动了应用

创建ApplicationContext层次时有些限制,比如,Web组件必须包含在子上下文中,并且父上下文和子上下文使用相同的Environment,具体参考SpringApplicationBuilder javadoc

results matching ""

    No results matching ""