0

I have one abstract configuration:

@Configuration
@EnableJpaRepositories(basePackages = {
    "x","y" }, entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager", repositoryBaseClass = BaseRepositoryImpl.class)
@EnableTransactionManagement
@EnableConfigurationProperties({ DataSourceProperties.class })
public abstract class AbstractBaseConfiguration {}

Then its implemented by the concrete Configuration class

@ComponentScan(basePackages = { "x", "z" })
public class BaseConfiguration extends AbstractBaseConfiguration {}

Combined in

@Configuration
@Import({ BaseConfiguration.class, RestConfiguration.class })
@ComponentScan(basePackages = { "x", "z" })
public class RestBaseConfiguration {}

So nested configurations, in the application at the end RestBaseConfiguration is used.

This was working before when using SpringBoot 2.0.X.

Now I upgraded SpringBoot to latest version (2.6.1). Suddenly I get exception:

org.springframework.beans.factory.support.BeanDefinitionOverrideException: 
Invalid bean definition with name 'com.example.XRepository' defined in com.example.XRepository defined in @EnableJpaRepositories declared on AbstractBaseConfiguration
Cannot register bean definition JpaRepositoryFactoryBean defined in XRepository defined in @EnableJpaRepositories declared on AbstractBaseConfiguration
There is already JpaRepositoryFactoryBean defined in XRepository defined in @EnableJpaRepositories declared on BaseConfiguration

I guess underlying issue is because beans are being overwritten https://stackoverflow.com/a/53723731/978302.

The question is why? Based on the error message, is EnableJpaRepositories being also proxied for the abstract class and for this reason repositories will be instantiated twice? Or somehow basePackages parameter of the annotation is interfering with the scans and then scan are executed twice..

RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36
  • Can you provide more information? I think if you have multiple classes that extend your AbstractBaseConfiguration in your spring context each of them would duplicate your configuration. A better approach would be to not have an abstract class AbstractBaseConfiguration. Instead you should use @Import for configs you depend on or rely on classpath scanning. – benebo22 Jan 27 '22 at 21:23
  • 1
    Just copied the setup into brand new application and can't reproduce the problem. I can extend Configuration annotated classes without an issue. I can also use @EnableJpaRepositories annotation. There is something else interfering with my configuration. Will try to find out and extend the question. Thanks for the Import annotation hint. I see I used mixed approach once importing config other time extending class. – RenatoIvancic Jan 28 '22 at 03:26

0 Answers0