From c4dd313eac013c9eea68b4d083225b91bf945826 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 11 Jan 2016 16:44:50 +0000 Subject: [PATCH] Add an intializer as a workaround for poor choice in Boot We can take this out once the issue is resolved in Boot. Meanwhile it makes sense that Spring Cloud apps prefer their local application name to one that is provided in vcap.application.name. Fixes https://github.com/spring-cloud/spring-cloud-config/issues/316 --- ...ontextIdApplicationContextInitializer.java | 44 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + 2 files changed, 46 insertions(+) create mode 100644 spring-cloud-bus/src/main/java/org/springframework/cloud/bus/context/ContextIdApplicationContextInitializer.java diff --git a/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/context/ContextIdApplicationContextInitializer.java b/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/context/ContextIdApplicationContextInitializer.java new file mode 100644 index 0000000..bd6efb5 --- /dev/null +++ b/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/context/ContextIdApplicationContextInitializer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2015 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.bus.context; + +import org.springframework.core.Ordered; + +/** + * Workaround for poor choice of ordering in the Spring Boot context id initializer. This + * one prefers a locally provided application name, rather than one provided by the + * platform. Since Spring Cloud apps send each other events on the Bus addressed by + * context id, this can be important when the application name doesn't match the one + * provided in local config. + * + * @author Dave Syer + * + */ +public class ContextIdApplicationContextInitializer + extends org.springframework.boot.context.ContextIdApplicationContextInitializer { + + /** + * Placeholder pattern to resolve for application name. + */ + private static final String NAME_PATTERN = "${spring.application.name:${spring.config.name:${vcap.application.name:application}}}"; + + public ContextIdApplicationContextInitializer() { + super(NAME_PATTERN); + setOrder(Ordered.LOWEST_PRECEDENCE - 5); + } + +} diff --git a/spring-cloud-bus/src/main/resources/META-INF/spring.factories b/spring-cloud-bus/src/main/resources/META-INF/spring.factories index 086e276..a050491 100644 --- a/spring-cloud-bus/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-bus/src/main/resources/META-INF/spring.factories @@ -1,3 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.bus.BusAutoConfiguration,\ org.springframework.cloud.bus.jackson.BusJacksonAutoConfiguration +org.springframework.context.ApplicationContextInitializer=\ +org.springframework.cloud.bus.context.ContextIdApplicationContextInitializer