Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The bean 'metaDataSourceAdvisor', defined in null, could not be registered

Tags:

spring-boot

I've updated the Spring Boot version from 2.0.3 to 2.1.1 but I'm getting this:

*************************** APPLICATION FAILED TO START ***************************  Description:  The bean 'metaDataSourceAdvisor', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.  Action:  Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true 

I get the error - the problem is that defined in null is not really a good pointer where to look. I've seen this question but I'd actually prefer to keep disallowing overriding ambiguous beans.

Any ideas?


Log output

2019-01-06 07:33:39.282  INFO 29626 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2019-01-06 07:33:39.282  INFO 29626 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2019-01-06 07:33:39.912  INFO 29626 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode. 2019-01-06 07:33:39.997  INFO 29626 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 80ms. Found 23 repository interfaces. 2019-01-06 07:33:39.999  WARN 29626 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'metaDataSourceAdvisor' defined in null: Cannot register bean definition [Root bean: class [org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'metaDataSourceAdvisor': There is already [Root bean: class [org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound. 2019-01-06 07:33:40.008  INFO 29626 --- [  restartedMain] ConditionEvaluationReportLoggingListener :   Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-01-06 07:33:40.010 ERROR 29626 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :  
like image 425
Stefan Falk Avatar asked Jan 06 '19 06:01

Stefan Falk


People also ask

How do I override a Java Bean?

Bean Overriding Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext that 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 I override a spring bean?

The most common approach followed for overriding a spring bean is to define a new bean, with the same id as the original bean, in a separate XML file. During context initialization, Spring would register the last bean found for the id, and use it for all the injections.


2 Answers

Okay, I found the issue myself: I had @EnableGlobalMethodSecurity twice in my project:

@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) // <-- public class WebSecurityConfig extends WebSecurityConfigurerAdapter { } 

and

@SpringBootApplication @EnableJpaRepositories(basePackages = {"mz.server.spring.repository"}) @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) // <-- @EntityScan(basePackages = "mz.server.hibernate.model") @EnableTransactionManagement @EnableScheduling public class Application { } 

So that's a nice new Spring Boot feature I'd say.

Just watch out for unwanted duplicate annotations if you see this kind of error.

like image 177
Stefan Falk Avatar answered Sep 21 '22 22:09

Stefan Falk


Expanding on the accepted answer, as per the release notes for Spring Boot 2.1

Bean overriding has been disabled by default to prevent a bean being accidentally overridden. If you are relying on overriding, you will need to set spring.main.allow-bean-definition-overriding to true.

So if you have used @EnableGlobalMethodSecurity more than once in your codebase and these beans are part of the same component scan then this annotation will attempt to create the metaDataSourceAdvisor bean more than once. This will throw an exception during initialization.

This will also apply to other auto configuration annotations that create beans. Make sure to only use their enabling annotations once.

like image 29
Stensig Avatar answered Sep 20 '22 22:09

Stensig