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

Check if you have missed adding following annotations (whichever applicable)

  1. @EnableJpaRepositories({"com.base.package"})
  2. @EnableMongoRepositories({"com.base.package"})

This worked for me


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.


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.

Tags:

Spring Boot