MapStruct没有合格的bean

问题描述

在使用MapStruct,Lombock和Spring时,在Tomcat 9中部署应用程序时出现以下错误

没有类型为“ somepackage.ControllerBusinessMapper”的合格Bean 可用:至少有1个符合自动装配条件的bean 候选人

这是我的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
}

在其注入的类中:

@Autowired
private ControllerBusinessMapper businessMapper;

我的spring配置类在软件包层次结构的根目录中设置软件包扫描。映射器的实现也在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无法找到实现类,我缺少了什么?我试图将generate-sources文件添加到构建路径中,并将其包括在软件包扫描中,但是没有用。

解决方法

要使用MapStruct,需要在Eclipse中进行其他配置:

  • 安装m2e-apt插件
  • 在每个Maven模块中,转到Maven>注释处理
  • 选中启用项目特定设置
  • 选择自动配置JDT APT模式