From 912d5cf40b19b9744758b3eb9b72515b563eeff4 Mon Sep 17 00:00:00 2001 From: Ramnivas Laddad Date: Thu, 31 Jul 2014 11:34:10 -0700 Subject: [PATCH] Add auto-configuration for Spring Cloud Enable if all of the following are true: - spring-cloud is on the classpath - There is no Cloud bean present (usually done by extending AbstractCloudConfig) - The "cloud" profile is active Fixes gh-1302 Autoconfigure spring-cloud --- spring-boot-autoconfigure/pom.xml | 5 ++ .../cloud/CloudAutoConfiguration.java | 60 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../TestAutoConfigurationSorter.java | 32 ++++++++++ .../cloud/CloudAutoConfigurationTests.java | 56 +++++++++++++++++ spring-boot-dependencies/pom.xml | 8 ++- 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/TestAutoConfigurationSorter.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfigurationTests.java diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 89f18db8c55..4886f212650 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -240,6 +240,11 @@ spring-rabbit true + + org.springframework.cloud + spring-cloud-spring-service-connector + true + org.springframework.mobile spring-mobile-device diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfiguration.java new file mode 100644 index 00000000000..7a78f6f4dd2 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfiguration.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2014 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.boot.autoconfigure.cloud; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.Cloud; +import org.springframework.cloud.app.ApplicationInstanceInfo; +import org.springframework.cloud.config.java.CloudScan; +import org.springframework.cloud.config.java.CloudScanConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Profile; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Spring Cloud. + *

+ * Activates when there is no bean of type {@link Cloud} is configured in the context, the + * {@link Cloud} type (this spring-cloud) is on the classpath, and the "cloud" profile is + * active. + *

+ * Once in effect, the auto-configuration is the equivalent of adding the + * {@link CloudScan} annotation in one of the configuration file. Specifically, it adds a + * bean for each service bound to the application and one for + * {@link ApplicationInstanceInfo} + * + * @author Ramnivas Laddad + * @since 1.2.0 + */ +@Configuration +@Profile("cloud") +@Order(CloudAutoConfiguration.ORDER) +@ConditionalOnClass(Cloud.class) +@ConditionalOnMissingBean(Cloud.class) +@ConditionalOnProperty(prefix = "spring.cloud", name = "enabled", havingValue = "true", matchIfMissing = true) +@Import(CloudScanConfiguration.class) +public class CloudAutoConfiguration { + + // Cloud configuration needs to happen early (before data, mongo etc.) + public static final int ORDER = Ordered.HIGHEST_PRECEDENCE + 20; + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 8516090b8f0..7f3580dad70 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -9,6 +9,7 @@ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ +org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/TestAutoConfigurationSorter.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/TestAutoConfigurationSorter.java new file mode 100644 index 00000000000..5fa37f7b389 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/TestAutoConfigurationSorter.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2014 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.boot.autoconfigure; + +import org.springframework.core.io.ResourceLoader; + +/** + * Public version of {@link AutoConfigurationSorter} for use in tests. + * + * @author Phillip Webb + */ +public class TestAutoConfigurationSorter extends AutoConfigurationSorter { + + public TestAutoConfigurationSorter(ResourceLoader resourceLoader) { + super(resourceLoader); + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfigurationTests.java new file mode 100644 index 00000000000..5366fd1c47f --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cloud/CloudAutoConfigurationTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2014 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.boot.autoconfigure.cloud; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.TestAutoConfigurationSorter; +import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; +import org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.ResourceLoader; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link CloudAutoConfiguration}. + * + * @author Phillip Webb + */ +public class CloudAutoConfigurationTests { + + @Test + public void testOrder() throws Exception { + ResourceLoader loader = new DefaultResourceLoader(); + TestAutoConfigurationSorter sorter = new TestAutoConfigurationSorter(loader); + Collection classNames = new ArrayList(); + classNames.add(MongoAutoConfiguration.class.getName()); + classNames.add(DataSourceAutoConfiguration.class.getName()); + classNames.add(MongoRepositoriesAutoConfiguration.class.getName()); + classNames.add(JpaRepositoriesAutoConfiguration.class.getName()); + classNames.add(CloudAutoConfiguration.class.getName()); + List ordered = sorter.getInPriorityOrder(classNames); + assertThat(ordered.get(0), equalTo(CloudAutoConfiguration.class.getName())); + } + +} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index ff378c91c40..d902f7b0c08 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -100,6 +100,7 @@ 0.7-groovy-2.0 4.1.0.BUILD-SNAPSHOT 1.3.5.RELEASE + 1.1.0.BUILD-SNAPSHOT 3.0.1.RELEASE Dijkstra-SR3 0.16.0.RELEASE @@ -1018,6 +1019,11 @@ spring-batch-test ${spring-batch.version} + + org.springframework.cloud + spring-cloud-spring-service-connector + ${spring-cloud.version} + org.springframework.data spring-data-releasetrain @@ -1306,4 +1312,4 @@ - \ No newline at end of file +