我目前正在用设置一个MapStruct映射器,到目前为止一切都很好,每个子映射程序都可以自动完成,并按预期进行注入。但是,在加载ApplicationContext时,使用修饰映射程序会导致以下失败:
创建名为“exampleMapperImpl”的bean时出错:通过构造函数参数0表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:,不具备“...ExampleMapper”类型的限定bean :期望至少有一个bean,它可以作为自动选择。依赖性注释:{@org.springframework.beans.factory.annotation.Qualifier(value="delegate")}
我正是按照MapStruct 文档来装饰我的映射器,这就是为什么设置不起作用的原因。
@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;
...
}这是我的测试设置,它适用于除修饰映射器之外的所有映射器:
@SpringBootTest(classes = {
ExampleMapperImpl.class,
GenericMapperImpl.class
})
class ExampleMapperTest {
@Autowired
private ExampleMapper underTest;
...
}如果有人能为我指出为什么不能加载ApplicationContext以及我如何修复设置,我会非常感激的。生成的实现如下:
@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;
}
...
}发布于 2021-05-28 07:52:07
我终于设法解决了这个问题。对于修饰映射器,除了ExampleMapperImpl之外,还会生成一个额外的类ExampleMapperImpl_,这个类也必须包含在SpringBootTest注释中。
@SpringBootTest(classes = {
ExampleMapperImpl.class,
ExampleMapperImpl_.class,
GenericMapperImpl.class
})https://stackoverflow.com/questions/67706928
复制相似问题