Repository bean defined in @EnableJpaRepositories declared on Application, could not be registered. A bean with that name has already been defined

Paul Marcelin Bejan

I have a multi maven project

on the ACCOUNTS:

@SpringBootApplication
@ComponentScan({ "com.myproject*" })
@EntityScan("com.myproject*")
@EnableJpaRepositories("com.myproject*")
public class MyProjectAccountsApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyProjectAccountsApplication.class, args);
    }

}

I have a CustomerIndividual Entity

@Data
@Entity
@Table(name = "customer_individual")
public class CustomerIndividual {

    @Id
    @Column(name = "id_customer", nullable = false)
    private Long id;

    @Column(name = "fk_nationality", nullable = false)
    private Integer fkNationality;

    @Column(name = "fk_residence", nullable = false)
    private Long fkResidence;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "fk_sex_type", nullable = false)
    private SexType sexType;

    @Column(name = "birth_date", nullable = false)
    private LocalDate birthDate;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "surname", nullable = false)
    private String surname;

    @Column(name = "email", nullable = false)
    private String email;

    @Column(name = "phone_number", nullable = false)
    private String phoneNumber;
    
    @OneToOne(fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn(name = "id_customer")
    @MapsId
    private Customer customer;
    
} 

that has a ManyToOne relationship with an entity (SexType) into another module called TYPES.

So ACCOUNTS module has TYPES as a dependency.

@SpringBootApplication
@ComponentScan({ "com.myproject*" })
@EntityScan("com.myproject*")
@EnableJpaRepositories("com.myproject*")
public class MyProjectTypesApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyProjectTypesApplication.class, args);
    }

}

SexType:

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "sex_type")
@AttributeOverride(name = "id", column = @Column(name = "id_sex_type", nullable = false))
public class SexType extends TypeEntity {

}

ACCOUNTS application failed to starts due to given exception:

The bean 'accountRepository', defined in com.myproject.accounts.account.repository.AccountRepository defined in @EnableJpaRepositories declared on MyProjectAccountsApplication, could not be registered. A bean with that name has already been defined in com.myproject.accounts.account.repository.AccountRepository defined in @EnableJpaRepositories declared on MyProjectTypesApplication and overriding is disabled.

A solution for that is to create a separate Maven module for the application types with the controller and put the TYPES module as a dependency.

But I'm wondering if there is a better solution to avoid creating a new maven module.

Mar-Z

If you have multiple Spring Boot applications in one Maven project you need something to distinguish between them. One possible solution is to use different Spring profiles and activate only one per application.

Add @Profile annotation to the configuration class like this:

@Profile("types")
@SpringBootApplication
public class MyProjectTypesApplication {
// ...
}

@Profile("accounts")
@SpringBootApplication
public class MyProjectAccountsApplication {
// ...
}

You can then activate specific profile by setting JVM system parameter when running application:

-Dspring.profiles.active=types/accounts

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bean 'scopedTarget.oauth2ClientContext' could not be registered same bean name has already been defined in class path

spring Bean Overriding 2.1.x in nested class @Configuration @Bean creation fails with 'A bean with that name has already been defined'

Spring Boot application-The bean could not be registered

Spring Boot application-The bean could not be registered

No bean named 'transactionManager' is defined

NoSuchBeanDefinitionException for bean defined in JavaConfig

Error creating bean with name 'entityManagerFactory' defined

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

NoSuchBeanDefinitionException: No bean named 'name' is defined

No qualifying bean of type [] is defined

Android Attribute has already been defined

No bean named is defined

Error:Attribute "theme" has already been defined

Attribute "barLength" has already been defined

Android: Attribute 'rippleColor' has already been defined

Error creating bean with name 'repositorySearchController' defined in URL [jar:file:/Users/umair/.m2/repository

Error creating bean with name 'userController' defined in file

No Qualifying Bean When Bean is Defined

Error(1147): This module has already been defined

No bean named 'CustomAuthenticationProvider' is defined

NoSuchBeanDefinitionException, but bean is defined

Attribute textAllCaps has already been defined

Attribute "titleTextStyle" has already been defined?

Variable has already been defined in my method, or has it?

overloading an operator that has already been defined

Spring bean injection - inject properties after bean has been defined

Why do I have "i is not defined" instead of 'i' has already been declared

"A bean with that name has already been defined" error coming in spring boot multi module project

how do I assign a function to a declared function that has already been defined