简体   繁体   English

Micronaut 中 MapStruct 生成类的 NoSuchBeanException

[英]NoSuchBeanException for MapStruct generated classes in Micronaut

When generating mapper-implementations with MapStruct, using "jsr330" componentModel, micronaut will throw a NoSuchBeanException during runtime when trying to inject those.当使用 MapStruct 生成映射器实现时,使用“jsr330”组件模型,当尝试注入它们时,micronaut 将在运行时抛出 NoSuchBeanException。

A workaround would be to use a provider that will supply the mapper-objects, but the generated code should work.一种解决方法是使用提供映射器对象的提供程序,但生成的代码应该可以工作。

Mapper definition:映射器定义:

@Mapper(componentModel = "jsr330")
public interface FooBarMapper {
    Foo toFoo(Bar bar);
}

Controller:控制器:

@Controller
public class SomeController {
    @Inject
    public SomeController(FooBarMapper mapper) {
    }

    @Get
    public String foo() {
        return "foo";
    }
}

pom.xml excerpt: pom.xml 摘录:

<annotationProcessorPaths>
    <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </path>
    <path>
        <groupId>io.micronaut</groupId>
        <artifactId>micronaut-inject-java</artifactId>
        <version>${micronaut.version}</version>
    </path>
    <path>
        <groupId>io.micronaut.configuration</groupId>
        <artifactId>micronaut-openapi</artifactId>
        <version>${micronaut.version}</version>
    </path>
</annotationProcessorPaths>

When calling the method on the controller, I would expect Micronaut to find the Mapstruct generated class (it is annotated with @Singleton), but instead, the result is在控制器上调用该方法时,我希望 Micronaut 找到 Mapstruct 生成的类(它用 @Singleton 注释),但结果是

Message: No bean of type [com.example.FooBarMapper] exists.消息:不存在 [com.example.FooBarMapper] 类型的 bean。 Ensure the class is declared a bean and if you are using Java or Kotlin make sure you have enabled annotation processing.确保将类声明为 bean,如果您使用的是 Java 或 Kotlin,请确保已启用注释处理。 Path Taken: new SomeController([FooBarMapper mapper]) io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [mapper] of class: com.example.SomeController采用的路径:new SomeController([FooBarMapper mapper]) io.micronaut.context.exceptions.DependencyInjectionException:无法为类的参数 [mapper] 注入值:com.example.SomeController

I found Micronaut PR which should resolve the problem with mapstruct using jsr330 component model.我发现Micronaut PR应该使用jsr330组件模型解决 mapstruct 的问题。 They are planning to include the fix in 1.1.0 .他们计划在1.1.0包含此修复程序。

UPDATE: The issue if fixed in 1.1.0.RC1更新:如果在1.1.0.RC1修复该问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM