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

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

Cannot inject mapstruct interface during startup spring application

问题

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

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

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

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

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

  1. @Component
  2. public abstract class AccountMapperDecorator implements AccountMapper {
  3. @Autowired
  4. @Qualifier("delegate")
  5. private AccountMapper delegate;
  6. @Override
  7. public Account from(RegistrationDto registrationDto, PasswordDto passwordDto) {
  8. return delegate.from(registrationDto, passwordDto);
  9. }
  10. }

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

  1. 没有找到类型为'com.xxx.AccountMapper'的合格Bean:期望至少有1个合格的自动装配候选Bean
  2. 依赖注解:{@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).

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

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

  1. Parameter 1 of constructor in com.xx.xx.Controller required a bean of type 'com.xxx.AccountMapper' that could not be found.
  2. 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.

  1. @Component
  2. public abstract class AccountMapperDecorator implements AccountMapper {
  3. @Autowired
  4. @Qualifier("delegate")
  5. private AccountMapper delegate;
  6. @Override
  7. public Account from(RegistrationDto registrationDto, PasswordDto passwordDto) {
  8. return delegate.from(registrationDto, passwordDto);
  9. }
  10. }

And then I receive.

  1. 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 中是否有类似这样的内容:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>3.5.1</version>
  5. <configuration>
  6. <source>1.8</source>
  7. <target>1.8</target>
  8. <annotationProcessorPaths>
  9. <path>
  10. <groupId>org.mapstruct</groupId>
  11. <artifactId>mapstruct-processor</artifactId>
  12. <version>...</version>
  13. </path>
  14. </annotationProcessorPaths>
  15. </configuration>
  16. </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:

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.5.1&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;source&gt;1.8&lt;/source&gt;
  7. &lt;target&gt;1.8&lt;/target&gt;
  8. &lt;annotationProcessorPaths&gt;
  9. &lt;path&gt;
  10. &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
  11. &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
  12. &lt;version&gt;...&lt;/version&gt;
  13. &lt;/path&gt;
  14. &lt;/annotationProcessorPaths&gt;
  15. &lt;/configuration&gt;
  16. &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:

确定