6

I'm using spring-cloud-aws-autoconfigure:2.1.0.RELEASE to connect to AWS. However when the app is running in an enviromnent other than AWS, I don't want the auto configuration to take place.

I tried turning off the auto configuration as suggested here and here with java configuration class, and also with spring.autoconfigure.excludes property in my yml file like this:

spring:
  autoconfigure:
    exclude:
      - org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration

But none of those solutions seems to work. The autoconfiguration still takes place and consequently, the app fails to start.

Jardo
  • 1,939
  • 2
  • 25
  • 45

2 Answers2

7

Found a solution: I added this directly to my main application class:

import org.springframework.cloud.aws.autoconfigure.context.*;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {
        ContextCredentialsAutoConfiguration.class,
        ContextInstanceDataAutoConfiguration.class,
        ContextRegionProviderAutoConfiguration.class,
        ContextResourceLoaderAutoConfiguration.class,
        ContextStackAutoConfiguration.class,
        MailSenderAutoConfiguration.class,
})
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}
Matthias Bohlen
  • 598
  • 6
  • 12
4

Found solution: I excluded every class I found in the autoconfiguration jar:

spring:
  autoconfigure:
    exclude:
      - org.springframework.cloud.aws.autoconfigure.cache.ElastiCacheAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.mail.MailSenderAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.metrics.CloudWatchExportAutoConfiguration
Jardo
  • 1,939
  • 2
  • 25
  • 45
  • I added `org.springframework.cloud.aws.autoconfigure.metrics.CloudWatchExportAutoConfiguration` to this list – Alik Apr 10 '20 at 19:47