无法在启动Spring应用程序期间注入MapStruct接口。

huangapple go评论84阅读模式
英文:

Cannot inject mapstruct interface during startup spring application

问题

我有以下的映射器(mapstruct版本1.3.1.Final)。

@Mapper(componentModel = "spring", uses = {}, unmappedTargetPolicy = ReportingPolicy.WARN)
public interface AccountMapper {
    @Mapping(source = "registrationDto.email", target = "email")
    @Mapping(source = "passwordDto.hashPassword", target = "password")
    Account from(RegistrationDto registrationDto, PasswordDto passwordDto);
}

当我尝试运行Spring应用程序时,出现了与Mapper关联的Bean找不到的问题。

com.xx.xx.Controller的构造函数中的参数1需要一个类型为'com.xxx.AccountMapper'的Bean,但找不到。
请考虑在您的配置中定义一个类型为'com.xxx.AccountMapper'的Bean。

我尝试了使用装饰器的解决方案。通过为接口添加@DecoratedWith(AccountMapperDecorator.class)注解,并创建以下类。

@Component
public abstract class AccountMapperDecorator implements AccountMapper {

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

    @Override
    public Account from(RegistrationDto registrationDto, PasswordDto passwordDto) {
        return delegate.from(registrationDto, passwordDto);
    }
}

然后我收到了以下错误信息。

没有找到类型为'com.xxx.AccountMapper'的合格Bean:期望至少有1个合格的自动装配候选Bean。
依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="delegate")}

在这种情况下可能出现的问题是什么?

英文:

I have the following mapper (mapstruct version 1.3.1.Final).

@Mapper(componentModel = "spring", uses = {}, unmappedTargetPolicy = ReportingPolicy.WARN)
public interface AccountMapper {
    @Mapping(source = "registrationDto.email", target = "email")
    @Mapping(source = "passwordDto.hashPassword", target = "password")
    Account from(RegistrationDto registrationDto, PasswordDto passwordDto);
}

When I attempt to run spring application I got the problem that bean associated with Mapper is not found.

Parameter 1 of constructor in com.xx.xx.Controller required a bean of type 'com.xxx.AccountMapper' that could not be found.

Consider defining a bean of type 'com.xxx.AccountMapper' in your configuration.

I tried solution with decorator. By adding annotation @DecoratedWith(AccountMapperDecorator.class) for interface and creating the following class.

@Component
public abstract class AccountMapperDecorator implements AccountMapper {

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

    @Override
    public Account from(RegistrationDto registrationDto, PasswordDto passwordDto) {
        return delegate.from(registrationDto, passwordDto);
    }
}

And then I receive.

No qualifying bean of type 'com.xxx.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="delegate")}

What could be the problem in that case ?

答案1

得分: 2

你是否运行过 mvn package
当你在你的集成开发环境中搜索类 AccountMapperImpl 时,能找到它吗?如果找不到,那是个问题。如果你找不到它,Spring 也会找不到。

也许你忘记在你的 pom.xml 中配置(或配置错误)mapstruct-processor?你的 pom.xml 中是否有类似这样的内容:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>...</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

在这里吗?

英文:

Did you run mvn package?
When you search for class AccountMapperImpl in your IDE, can you find it? If not that is a problem. If you can't find it, Spring won't too.

Maybe you forgot to configure (or misconfigured) mapstruct-processor in your pom.xml? Do you have something like:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.5.1&lt;/version&gt;
    &lt;configuration&gt;
        &lt;source&gt;1.8&lt;/source&gt;
        &lt;target&gt;1.8&lt;/target&gt;
        &lt;annotationProcessorPaths&gt;
            &lt;path&gt;
                &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
                &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
                &lt;version&gt;...&lt;/version&gt;
            &lt;/path&gt;
        &lt;/annotationProcessorPaths&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

there?

答案2

得分: 0

这是一个时常发生的情况,
如果你注意到了,那么可能 AccountMapperImpl 没有生成到 target/build 文件夹中
有几种方法可以解决这个问题:

  • 尝试清理 out/build 目录

  • 运行 mvn clean compile 或者 gradle clean compileJava

  • 设置你的 IDE 使用 maven/gradle 来运行你的代码
    无法在启动Spring应用程序期间注入MapStruct接口。

  • 确保启用了 注解处理(Annotation Processing)

英文:

This is a situation that happens from time to time,<br>
If you will noticed then probebly the AccountMapperImpl isn't generated to the target/build folders
There a few ways to fix that:

  • Try clean the out/build directories
  • Run mvn clean compile or gradle clean compileJava
  • Set your IDE to run your code with maven/gradle
    无法在启动Spring应用程序期间注入MapStruct接口。
  • Be sure that Annotation Processing is enabled

huangapple
  • 本文由 发表于 2020年9月8日 14:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63788313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定