MapStruct 没有合格的 bean

No qualifying bean with MapStruct

我在 Tomcat 9 中使用 MapStruct、Lombock 和 Spring 时在应用程序部署中遇到以下错误:

No qualifying bean of type 'somepackage.ControllerBusinessMapper' available: expected at least 1 bean which qualifies as autowire candidate

这是我的 pom.xml:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>[1.18.12,)</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>1.3.1.Final</version>
</dependency>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.3.1.Final</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.12</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

这是映射器:

@Mapper(componentModel = "spring")
public interface ControllerBusinessMapper {
    //Some methods
}

并且在 class 注入的地方:

@Autowired
private ControllerBusinessMapper businessMapper;

我的 spring 配置 class 将包扫描设置在包层次结构的根目录中。映射器的实现也在 target/generated-sources:

下生成
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2020-08-23T03:56:23+0200",
    comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.7 (Oracle Corporation)"
)
@Component
public class ControllerBusinessMapperImpl implements ControllerBusinessMapper {
    //Some methods
}

我的错误提示 Spring 无法找到实现 class,我错过了什么?我尝试将 generated-sources 文件夹添加到构建路径并将其包含在包扫描中,但没有成功。

需要在 Eclipse 中进行额外配置才能使用 MapStruct:

  • 安装 m2e-apt 插件
  • 在每个 Maven 模块中转到 Maven > Annotation Processing
  • 选中启用项目特定设置
  • Select自动配置JDT APT模式