Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataSource bean overriding in spring boot 2.1

I have upgraded to spring boot 2.1 release and I have got strange exception when starting up the application.

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.

The full error message is:

[o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: 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.

Beans must not be overridden according to our policy and it's disabled with:

spring.main.allow-bean-definition-overriding=false 

I don't have any data source configuration in my application code. The only option that triggers this error is @EnableAutoConfiguration and in my application properties I have set the data source type to:

spring.datasource.type=com.zaxxer.hikari.HikariDataSource 

The boot application is initialized with

@SpringBootApplication @EnableAutoConfiguration public class MyApplication extends SpringBootServletInitializer {      public static void main(String[] args) {         new MyApplication()             .configure(new SpringApplicationBuilder(MyApplication.class))             .run(args);     } } 

There is also configuration class that imports various other configurations:

@Configuration @ImportResource(locations = {     "classpath*:conf/spring/*.xml",     "classpath*:conf/spring/core/*.xml",     "classpath*:conf/spring/plugin/**/*.xml" }) @EnableAsync @EnableRetry @EnableCaching @EnableBatchProcessing @EnableCircuitBreaker public class AppConfig {     ... } 

Does anyone knows what could cause that issue and where to search?

It didn't happened prior to Spring Boot 2.1 (i.e. 2.0.5).

like image 488
Mariusz Miesiak Avatar asked Nov 01 '18 15:11

Mariusz Miesiak


People also ask

How do you override beans in spring boot?

Bean Overriding Spring beans are identified by their names within an ApplicationContext. Thus, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext which has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.

Can we have 2 beans with same ID in spring?

Here, @Bean instantiates two beans with ids the same as the method names and registers them within the BeanFactory (Spring container) interface. Next, we can initialize the Spring container and request any of the beans from the Spring container. This strategy also makes it simple to achieve dependency injection.

How do @configuration annotated classes support Singleton beans?

If you use @Configuration, all methods marked as @Bean will be wrapped into a CGLIB wrapper which works as if it's the first call of this method, then the original method's body will be executed and the resulting object will be registered in the spring context.

What is the default bean ID if you only use bean How can you override this?

When using @Bean without specifying name or alias, the default bean ID will be created based on the name of the method which was annotated with @Bean annotation. You can override this behaviour by specifying name or aliases for the bean. Alias is always the second name of the bean.


1 Answers

I ran into a similar problem with this today and the following spring cloud config issue helped me: Issue 1142.

We were using Spring Cloud Config which is not compatible with Spring Boot 2.1.0 as of yet. The Greenwich release train of Spring Cloud will be compatible with Spring Boot 2.1.0.

Your @EnableCircuitBreaker annotation leads me to believe you might also be using a version of Spring Cloud that is not compatible with the 2.1.0 release of Spring Boot.

like image 99
Brad Fontaine Avatar answered Sep 21 '22 08:09

Brad Fontaine