英文:
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:
<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>
there?
答案2
得分: 0
这是一个时常发生的情况,
如果你注意到了,那么可能 AccountMapperImpl 没有生成到 target/build
文件夹中
有几种方法可以解决这个问题:
-
尝试清理
out/build
目录 -
运行
mvn clean compile
或者gradle clean compileJava
-
确保启用了
注解处理(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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论