首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用spring MapStruct componentModel为修饰的componentModel映射程序创建bean

无法使用spring MapStruct componentModel为修饰的componentModel映射程序创建bean
EN

Stack Overflow用户
提问于 2021-05-26 14:24:54
回答 1查看 539关注 0票数 0

我目前正在用设置一个MapStruct映射器,到目前为止一切都很好,每个子映射程序都可以自动完成,并按预期进行注入。但是,在加载ApplicationContext时,使用修饰映射程序会导致以下失败:

创建名为“exampleMapperImpl”的bean时出错:通过构造函数参数0表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:,不具备“...ExampleMapper”类型的限定bean :期望至少有一个bean,它可以作为自动选择。依赖性注释:{@org.springframework.beans.factory.annotation.Qualifier(value="delegate")}

我正是按照MapStruct 文档来装饰我的映射器,这就是为什么设置不起作用的原因。

代码语言:javascript
复制
@Mapper(componentModel = "spring",
        uses = GenericMapper.class,
        injectionStrategy = InjectionStrategy.CONSTRUCTOR)
@DecoratedWith(ExampleMapperDecorator.class)
public interface ExampleMapper {

    ...

}


public abstract class ExampleMapperDecorator implements ExampleMapper {

    @Autowired
    @Qualifier("delegate")
    private ExampleMapper delegate;

    ...

}

这是我的测试设置,它适用于除修饰映射器之外的所有映射器:

代码语言:javascript
复制
@SpringBootTest(classes = {
        ExampleMapperImpl.class,
        GenericMapperImpl.class
})
class ExampleMapperTest {

    @Autowired
    private ExampleMapper underTest;

    ...

}

如果有人能为我指出为什么不能加载ApplicationContext以及我如何修复设置,我会非常感激的。生成的实现如下:

代码语言:javascript
复制
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-05-26T15:44:17+0200",
    comments = "version: 1.4.2.Final, compiler: javac, environment: Java 13.0.2 (Oracle Corporation)"
)
@Component
@Primary
public class ExampleMapperImpl extends ExampleMapperDecorator implements OrderingMapper {

    private final ExampleMapper delegate;

    @Autowired
    public ExampleMapperImpl(@Qualifier("delegate") ExampleMapper delegate) {

        this.delegate = delegate;
    }

    ...
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-28 07:52:07

我终于设法解决了这个问题。对于修饰映射器,除了ExampleMapperImpl之外,还会生成一个额外的类ExampleMapperImpl_,这个类也必须包含在SpringBootTest注释中。

代码语言:javascript
复制
@SpringBootTest(classes = {
        ExampleMapperImpl.class,
        ExampleMapperImpl_.class,
        GenericMapperImpl.class
})
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67706928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档