Spring Boot多模块注入错误

时间:2018-07-28 08:05:25

标签: java spring maven spring-boot module

我在这个问题上发现了很多问题,但是没有一个解决方案对我有用。当它是一个单模块应用程序时,该项目可以正常工作。当我将文件拆分为不同的模块时,我将无法再运行它。

模块结构:

root
|
|_api
|   \_com.example.utils.mappers.MyObjectMapper.java
|
|
|_persistence
|
|_rest
|   \_com.example.Application.java
|
\_service
    \_com.example.service.MyObjectServiceImpl.java

错误:

Field myObjectMapper in com.example.service.MyObjectServiceImpl required a bean 
of type 'com.example.utils.mappers.MyObjectMapper' that could not be found.

有问题的两个文件MyObjectMapper:

package com.example.utils.mappers;

@Mapper(componentModel = "spring")
public interface myObjectMapper {
  // some mapping functionality
}

和Service类:

package com.example.service;

@Service
public class MyObjectServiceImpl implements MyObjectService {

   @Inject
   private MyObjectMapper myObjectMapper;

   /* some DAO calls */
}

全部运行的类:

package com.example;

@SpringBootApplication(scanBasePackages = "com.example"}
public class Application extends SpringBootServletInitializer {
  /* call to SpringApplication run method */
}

root / pom.xml的示例

<groupId>com.example</groupId>
<artifactId>root</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>
    <module>persistence</module>
    <module>api</module>
    <module>service</module>
    <module>rest</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
</parent>

service / pom.xml的示例:

<groupId>com.example</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
    <groupId>com.example</groupId>
    <artifactId>root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../</relativePath>
</parent>

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

1 个答案:

答案 0 :(得分:0)

因此,错误毕竟与项目的多模块性质无关。当我将依赖项从原始pom.xml分区到模块poms中时,我仅转移了mapstruct工件,而无意间遗漏了第二个mapstruct-processor工件。