Spring Boot 多模块项目中出现“已定义具有该名称的 bean”错误

Har*_*ora 5 java maven spring-boot

我正在使用一个 Spring Boot 多模块 Maven 项目,在这里我遇到了异常,每次我运行应用程序时,它都会因不同的文件而失败。

 The bean 'evServiceCpeRepository' could not be registered. A bean with that name has already been defined
Run Code Online (Sandbox Code Playgroud)

该项目有三个模块 sbill 、 amr 和executable ,

我在 sbill 模块中拥有的所有类... amr 模块是空的,它只有一个控制器,在 amr 模块 pom 文件中,我添加了 sbill 模块的依赖项,因为 amr 模块可以访问 sbill 的类。

可执行模块仅包含 spring boot 主类,除此之外没有任何类该模块是可执行模块。

 @SpringBootApplication
@ComponentScan(basePackages = { "com"})
@EntityScan(basePackages = { "com"})
 @EnableJpaRepositories(basePackages = { "com"})
 public class MainClass {public static void main(String[] args) {
 SpringApplication.run(MainClass.class, args);
 }}
Run Code Online (Sandbox Code Playgroud)

可执行模块的pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
    <groupId>com.sbill</groupId>
      <artifactId>sbill-wrapper</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      </parent>
       <packaging>war</packaging>
      <artifactId>executable</artifactId>

<dependencies>
  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
      <dependency>
       <groupId>com.sbill</groupId>
       <artifactId>sbill</artifactId>
         <version>0.0.1-SNAPSHOT</version>
       </dependency> 

     <dependency>
    <groupId>com.sbill</groupId>
    <artifactId>amr</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>

    </dependencies> 


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!-- Adding  plugin of mavan resource to copy dist folder for  war -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>


           <outputDirectory>${project.build.directory}/classes/static/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>../web/dist</directory>
                            </resource>         
                        </resources>            
                    </configuration>            
                 </execution>           
                </executions>
            </plugin>
          </plugins>       
       </build>  
      </project>       
Run Code Online (Sandbox Code Playgroud)

Har*_*ora 3

我能够自己解决这个问题...问题与@EnableJpaRepositories(basePackages = { "com"})注释有关。我已将此注释放入 Mainclass.java 文件中,并且此注释已存在于模块 sbill 的一个配置文件中。

所以 spring 两次尝试创建 beans 这就是它失败的原因。我从另一个配置文件中删除了该注释,它工作得很好。