【问题标题】:No qualifying bean with MapStructMapStruct 没有符合条件的 bean
【发布时间】:2020-08-23 22:44:36
【问题描述】:

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

没有“somepackage.ControllerBusinessMapper”类型的限定 bean 可用:预计至少有 1 个符合 autowire 条件的 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 配置类将包扫描设置在包层次结构的根目录中。而且mapper的实现也是在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 无法找到实现类,我错过了什么?我尝试将生成的源文件夹添加到构建路径并将其包含在包扫描中,但它不起作用。

【问题讨论】:

  • 您很可能没有对创建 Impl 类的包进行组件扫描。

标签: java spring mapstruct


【解决方案1】:

为了使用 MapStruct,需要在 Eclipse 中进行额外的配置:

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

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2021-09-13
    相关资源
    最近更新 更多