Datasource Issue Exposed By Axon Configuration

I’m trying to test my axon application in Spring Boot v2.1.0.M3 but it fails to start because of a bean definition for a bean named datasource. This is not a bean I’ve configured and I don’t know exactly where it’s coming from but I have been able to isolate it to my axon configuration when the version of Spring Boot changes from v2.0.4.RELEASE to v2.1.0.M3. I have a very simple project available on github to demonstrate.

I started this project as a simple JPA project on Spring Boot v2.1.0.M3 (without any Axon configuration). This simple project behaved exactly as expected–I could hit the REST endpoint and get the data I initialized in a Data Loader (a spring boot command line runner).

Next, I added the Axon dependencies and provided some simple configuration (including axondb configuration; axondb is expected to be running on http://localhost:8123). I did not define any actual axon components (aggregates or command/event handlers) I merely added the configuration.

When I try to deploy the application in this state it fails with the following message:


APPLICATION FAILED TO START


Description:

The bean ‘dataSource’, defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

If I take the prescribed action and allow bean definition overriding then the application will deploy, but I’m not very comfortable enabling this feature.

NOTE: I also get the following stack trace, which doesn’t really help:

2018-09-14 14:50:15.553e[0;39m e[32mDEBUGe[0;39m e[35m13606e[0;39m e[2m—e[0;39m e[2m[ restartedMain]e[0;39m e[36mo.s.b.d.LoggingFailureAnalysisReporter e[0;39m e[2m:e[0;39m Application failed to start due to an exception

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name ‘dataSource’ defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Cannot register bean definition [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] for bean ‘dataSource’: There is already [Root bean: class [null]; scope=refresh; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] bound.

at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:877) ~[spring-beans-5.1.0.RC3.jar:5.1.0.RC3]

at org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$RefreshScopeBeanDefinitionEnhancer.postProcessBeanDefinitionRegistry(RefreshAutoConfiguration.java:140) ~[spring-cloud-context-2.0.1.RELEASE.jar:2.0.1.RELEASE]

at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.0.RC3.jar:5.1.0.RC3]

at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:125) ~[spring-context-5.1.0.RC3.jar:5.1.0.RC3]

at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693) ~[spring-context-5.1.0.RC3.jar:5.1.0.RC3]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530) ~[spring-context-5.1.0.RC3.jar:5.1.0.RC3]

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.M3.jar:2.1.0.M3]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:797) [spring-boot-2.1.0.M3.jar:2.1.0.M3]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:421) [spring-boot-2.1.0.M3.jar:2.1.0.M3]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:340) [spring-boot-2.1.0.M3.jar:2.1.0.M3]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) [spring-boot-2.1.0.M3.jar:2.1.0.M3]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1279) [spring-boot-2.1.0.M3.jar:2.1.0.M3]

at com.fedexx.DatasourceTestApplication.main(DatasourceTestApplication.java:10) [classes/:na]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]

at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.0.M3.jar:2.1.0.M3]

I image any axon application on any version of Spring Boot greater than 2.1 will have this same problem. Has anyone else seen this?

I failed to mention that if I take the referenced git hub project and change the spring-boot-starter-parent version to 2.0.4.RELEASE then the application deploys and behaves as expected.

So I’ve looked a little closer at this issue and have found that it has nothing to do with axon! Rather, there is an issue with spring-cloud-starter-config, when mixed with spring-boot-starter-data-jpa and Spring Boot v2.1.x. Sorry about the mistake!

Thanks for sharing!
I was getting to a similar conclusion based on the ‘cannot register X because I have an X configured’.