简体   繁体   English

使用@SpringBootConfiguration的Spring Boot多模块应用程序

[英]Spring Boot Multi Module application with @SpringBootConfiguration

I have a maven multi module app with the structure : 我有一个结构的maven多模块应用程序:

-parent_project
-actual_project
-jpaBase

Parent project is a maven aggregation of the projects : actual_project and jpaBase (both spring boot applications) 父项目是项目的maven聚合:actual_project和jpaBase(两个spring引导应用程序)

jpaBase is a dependency of actual_project. jpaBase是actual_project的依赖项。

Now when i do a mvn package I am getting a unit test error : 现在当我做一个mvn包时,我得到一个单元测试错误:

Found multiple @SpringBootConfiguration annotated classes 找到多个@SpringBootConfiguration注释类

because both jpaBase and actual_project have classes annotated with @SpringBootConfiguration. 因为jpaBase和actual_project都有使用@SpringBootConfiguration注释的类。

How can I make sure that Spring considers the SpringBootConfiguration class of only actual_project and not of jpaBase. 我怎样才能确保Spring仅考虑spring_project的SpringBootConfiguration类而不考虑jpaBase。

Thanks! 谢谢!

From the Spring documentation : 从Spring文档:

Application should only ever include one @SpringBootConfiguration and most idiomatic Spring Boot applications will inherit it from @SpringBootApplication. 应用程序应该只包含一个@SpringBootConfiguration,大多数惯用的Spring Boot应用程序将从@SpringBootApplication继承它。

You said : 你说 :

because both jpaBase and actual_project have classes annotated with @SpringBootConfiguration. 因为jpaBase和actual_project都有使用@SpringBootConfiguration注释的类。

It should not. 它不应该。 This annotation has to be used a single time by application. 此注释必须由应用程序一次使用。 So, only actual_project and parent_project which are Spring Boot applications should declare this annotation. 因此,只有作为Spring Boot应用程序的actual_project和parent_project才应声明此批注。

@SpringBootConfiguration replaces the declaration of these annotations: @SpringBootConfiguration替换了这些注释的声明:
@Configuration , @EnableAutoConfiguration and @ComponentScan @Configuration @EnableAutoConfiguration@ComponentScan @EnableAutoConfiguration@ComponentScan

You have the information in the official documentation . 您拥有官方文档中的信息。

So, in your jpaBase project, you could replace @SpringBootConfiguration by the declaration of these three annotations : 因此,在您的jpaBase项目中,您可以通过声明这三个注释来替换@SpringBootConfiguration

@Configuration
@EnableAutoConfiguration
@ComponentScan({ "yourPackage" })
public class JpaConfig {
   ....
}

Actually, for anyone looking at this, SpringBootConfiguration only replaces @Configuration . 实际上,对于任何看这个的人来说, SpringBootConfiguration只替换@Configuration

@SpringBootApplication replaces @Configuration , @EnableAutoConfiguration and @ComponentScan . @SpringBootApplication取代@Configuration @EnableAutoConfiguration@ComponentScan @EnableAutoConfiguration@ComponentScan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM