Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@
* "application.properties".
*
* @author Dave Syer
* @author Biju Kunjummen
*
*/
public class BootstrapApplicationListener
Expand All @@ -77,6 +78,7 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
true)) {
return;
}

// don't listen to events in a bootstrap context
if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
return;
Expand Down Expand Up @@ -133,6 +135,7 @@ private ConfigurableApplicationContext bootstrapServiceContext(
}
sources.add(cls);
}
sources.add(BootstrapMarkerConfiguration.class);
AnnotationAwareOrderComparator.sort(sources);
builder.sources(sources.toArray(new Class[sources.size()]));
final ConfigurableApplicationContext context = builder.run();
Expand Down Expand Up @@ -289,11 +292,26 @@ public void initialize(ConfigurableApplicationContext context) {
while (context.getParent() != null && context.getParent() != context) {
context = (ConfigurableApplicationContext) context.getParent();
}
if (isBootstrapContext(context)) {
return;
}
reorderSources(context.getEnvironment());
new ParentContextApplicationContextInitializer(this.parent)
.initialize(context);
}

/**
* Determines if a context is a Bootstrap context - by the presence of a Marker bean with a specific name
* @param context
* @return if the context is really a bootstrap context
*/
private boolean isBootstrapContext(ConfigurableApplicationContext context) {
if (context.containsBeanDefinition(BootstrapMarkerConfiguration.MARKER_BEAN_NAME)) {
return true;
}
return false;
}

private void reorderSources(ConfigurableEnvironment environment) {
PropertySource<?> removed = environment.getPropertySources()
.remove(DEFAULT_PROPERTIES);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.bootstrap;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* This purpose of this @Configuration is to register a Marker configuration just to determine if the Bootstrap
* related configuration is already loaded up and if so not to load it up once more.
*
* @author Biju Kunjummen
*/
@Configuration
class BootstrapMarkerConfiguration {

static final String MARKER_BEAN_NAME = "bootstrap:bootstrapMarker";

class BootstrapMarker {}

@Bean(MARKER_BEAN_NAME)
public BootstrapMarker bootstrapMarker() {
return new BootstrapMarker();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.bootstrap;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.After;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Integration tests for Bootstrap Listener's functionality of adding a bootstrap context
* as the root Application Context
*
* @author Biju Kunjummen
*/
public class BootstrapListenerHierarchyIntegrationTests {

@Test
public void shouldAddInABootstrapContext() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.sources(BasicConfiguration.class).web(false).run();

assertThat(context.getParent()).isNotNull();
}

@Test
public void shouldAddInOneBootstrapForABasicParentChildHierarchy() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.sources(RootConfiguration.class).web(false)
.child(BasicConfiguration.class).web(false).run();

// Should be RootConfiguration based context
ConfigurableApplicationContext parent = (ConfigurableApplicationContext) context
.getParent();
assertThat(parent.getBean("rootBean", String.class)).isEqualTo("rootBean");

// Parent should have the bootstrap context as parent
assertThat(parent.getParent()).isNotNull();

ConfigurableApplicationContext bootstrapContext = (ConfigurableApplicationContext) parent
.getParent();

// The Bootstrap Marker bean should be present
assertThat(bootstrapContext
.containsLocalBean(BootstrapMarkerConfiguration.MARKER_BEAN_NAME))
.isTrue();

// Bootstrap should be the root, there should be no other parent
assertThat(bootstrapContext.getParent()).isNull();
}

@Test
public void shouldAddInOneBootstrapForSiblingsBasedHierarchy() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.sources(RootConfiguration.class).web(false)
.child(BasicConfiguration.class).web(false)
.sibling(BasicConfiguration.class).web(false).run();

// Should be RootConfiguration based context
ConfigurableApplicationContext parent = (ConfigurableApplicationContext) context
.getParent();
assertThat(parent.getBean("rootBean", String.class)).isEqualTo("rootBean");

// Parent should have the bootstrap context as parent
assertThat(parent.getParent()).isNotNull();

ConfigurableApplicationContext bootstrapContext = (ConfigurableApplicationContext) parent
.getParent();

// The Bootstrap Marker bean should be present
assertThat(bootstrapContext
.containsLocalBean(BootstrapMarkerConfiguration.MARKER_BEAN_NAME))
.isTrue();

// Bootstrap should be the root, there should be no other parent
assertThat(bootstrapContext.getParent()).isNull();
}

@Configuration
static class BasicConfiguration {
}

@Configuration
static class RootConfiguration {

@Bean
public String rootBean() {
return "rootBean";
}
}
}