spring-cloud-aws: Application cannot register SSM client

Type: Bug

Component: Parameter Store

Describe the bug Currently we try to migrate our services to Spring Boot 2.4.4, Spring Cloud 2020.0.2 and Spring Cloud AWS 2.3. As far as I understand the bootstrap context has been deprecated by Spring Cloud Commons and the new way to include configuration via AWS Parameter Store would be

spring:
  config:
    import: "aws-parameterstore:"

which we have in application-aws.yml.

Now after deployment we currently end up with

java.lang.IllegalStateException: Could not register object [com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient@785a4557] under bean name 'configDataAWSSimpleSystemsManagement': there is already object [com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient@785a4557] bound
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:124)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerSingleton(DefaultListableBeanFactory.java:1138)
    at io.awspring.cloud.autoconfigure.paramstore.AwsParamStoreConfigDataLocationResolver.lambda$registerAndPromoteBean$1(AwsParamStoreConfigDataLocationResolver.java:106)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.DefaultBootstrapContext.close(DefaultBootstrapContext.java:133)
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:392)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:325)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:144)
    at org.springframework.cloud.stream.binder.DefaultBinderFactory.getBinderInstance(DefaultBinderFactory.java:338)
    at org.springframework.cloud.stream.binder.DefaultBinderFactory.doGetBinder(DefaultBinderFactory.java:221)
    at org.springframework.cloud.stream.binder.DefaultBinderFactory.getBinder(DefaultBinderFactory.java:149)
    at org.springframework.cloud.stream.function.FunctionConfiguration$FunctionToDestinationBinder.bindFunctionToDestinations(FunctionConfiguration.java:443)
    at org.springframework.cloud.stream.function.FunctionConfiguration$FunctionToDestinationBinder.afterPropertiesSet(FunctionConfiguration.java:401)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:63)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302)
    at my.ApplicationKt.main(Application.kt:48)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

Any suggestions what’s going on here?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 18 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@eddumelendez @MatejNedic Seems that Spring Cloud Stream creates this issue: Have a look at https://github.com/kschlesselmann/aws-ssm-issue-sample – as soon as you add a simple function + stream dependency the application startup fails.

Hey @davidmelia,

I was having some issues and was not able to work on a project. Will take deeper look today since I finally have some time free. From quick looks I had before some refactoring will be needed. Hopefully we will be able to have PR soon.

@MatejNedic any more movement on this? I imagine this blocks many projects moving to spring cloud aws 2.3.x

Hello @MatejNedic / @eddumelendez, We have the same issue, I was trying to understand it and found that at line 106 in https://github.com/awspring/spring-cloud-aws/blob/2.3.x/spring-cloud-starter-aws-parameter-store-config/src/main/java/io/awspring/cloud/autoconfigure/paramstore/AwsParamStoreConfigDataLocationResolver.java, there is a registerSingleton

event.getApplicationContext().getBeanFactory().registerSingleton(“configData” + type.getSimpleName(), instance);

This fails when being called the second time, in this case, the spring cloud stream starts as a child context and thus ends loading the environment again leading to attempting registration of the singleton bean twice and exception.

Is there any specific reason for the bean to be a singleton, can it be like other beans i.e. using registerIfAbsent.

If I am not wrong when the bootstrap is used this is exactly what happens(I mean skipping bean def) by using @ConditionalOnMissingBean.

Does this help and could we change it from registerSingleton to registerIfAbsent?

Thanks.

Thanks @kschlesselmann on sample! Will take a look why error is happening in following days.