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.
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.
Comments