diff --git a/functions/common/aws-s3-common/src/main/java/org/springframework/cloud/fn/common/aws/s3/AutoConfigurationExclusionEnvironmentPostProcessor.java b/functions/common/aws-s3-common/src/main/java/org/springframework/cloud/fn/common/aws/s3/AutoConfigurationExclusionEnvironmentPostProcessor.java new file mode 100644 index 00000000..dd88c880 --- /dev/null +++ b/functions/common/aws-s3-common/src/main/java/org/springframework/cloud/fn/common/aws/s3/AutoConfigurationExclusionEnvironmentPostProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020-2020 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 + * + * https://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.fn.common.aws.s3; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.cloud.aws.autoconfigure.context.ContextResourceLoaderAutoConfiguration; +import org.springframework.core.annotation.Order; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.env.StandardEnvironment; + +/** + * @author Timo Salm + */ +@Order +public class AutoConfigurationExclusionEnvironmentPostProcessor implements EnvironmentPostProcessor { + + static final String SPRING_AUTOCONFIGURE_EXCLUDE_PROPERTY = "spring.autoconfigure.exclude"; + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + final PropertySource propertySource = environment.getPropertySources() + .get(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); + if (propertySource instanceof MapPropertySource) { + ((MapPropertySource) propertySource).getSource().put(SPRING_AUTOCONFIGURE_EXCLUDE_PROPERTY, + ContextResourceLoaderAutoConfiguration.class.getCanonicalName()); + } + } +} diff --git a/functions/common/aws-s3-common/src/main/resources/META-INF/spring.factories b/functions/common/aws-s3-common/src/main/resources/META-INF/spring.factories index b6e0ea51..c3d0c35d 100644 --- a/functions/common/aws-s3-common/src/main/resources/META-INF/spring.factories +++ b/functions/common/aws-s3-common/src/main/resources/META-INF/spring.factories @@ -1,3 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.fn.common.aws.s3.CompatibleStorageAmazonS3Configuration,\ org.springframework.cloud.fn.common.aws.s3.AmazonS3Configuration +org.springframework.boot.env.EnvironmentPostProcessor=\ + org.springframework.cloud.fn.common.aws.s3.AutoConfigurationExclusionEnvironmentPostProcessor diff --git a/functions/common/aws-s3-common/src/main/resources/application.properties b/functions/common/aws-s3-common/src/main/resources/application.properties deleted file mode 100644 index 64d4b5fe..00000000 --- a/functions/common/aws-s3-common/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -spring.autoconfigure.exclude=org.springframework.cloud.aws.autoconfigure.context.ContextResourceLoaderAutoConfiguration diff --git a/functions/common/aws-s3-common/src/test/java/org/springframework/cloud/fn/common/aws/s3/AutoConfigurationExclusionEnvironmentPostProcessorTests.java b/functions/common/aws-s3-common/src/test/java/org/springframework/cloud/fn/common/aws/s3/AutoConfigurationExclusionEnvironmentPostProcessorTests.java new file mode 100644 index 00000000..311117f2 --- /dev/null +++ b/functions/common/aws-s3-common/src/test/java/org/springframework/cloud/fn/common/aws/s3/AutoConfigurationExclusionEnvironmentPostProcessorTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020-2020 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 + * + * https://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.fn.common.aws.s3; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +import static org.springframework.cloud.fn.common.aws.s3.AutoConfigurationExclusionEnvironmentPostProcessor + .SPRING_AUTOCONFIGURE_EXCLUDE_PROPERTY; + +/** + * @author Timo Salm + */ +class AutoConfigurationExclusionEnvironmentPostProcessorTests { + + @Test + public void addsConfigurationPropertyToExcludeAmazonS3AutoConfiguration() { + final ConfigurableApplicationContext context = new SpringApplicationBuilder() + .sources(AutoConfigurationExclusionEnvironmentPostProcessor.class) + .web(WebApplicationType.NONE) + .build() + .run(); + + final ConfigurableEnvironment environment = context.getEnvironment(); + Assertions.assertEquals( + "org.springframework.cloud.aws.autoconfigure.context.ContextResourceLoaderAutoConfiguration", + environment.getProperty(SPRING_AUTOCONFIGURE_EXCLUDE_PROPERTY)); + } +}