无法在启动 spring 应用程序期间注入 mapstruct 接口
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 的问题。
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.
我尝试了装饰器的解决方案。通过为接口添加注释 @DecoratedWith(AccountMapperDecorator.class)
并创建以下 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);
}
}
然后我收到。
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")}
在那种情况下可能是什么问题?
你 运行 mvn package
了吗?
当你在你的IDE中搜索classAccountMapperImpl
时,你能找到吗?如果不是那是一个问题。如果找不到,Spring 也找不到。
也许您忘记在 pom.xml 中配置(或配置错误)mapstruct-processor?你有这样的东西吗:
<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>
有吗?
这种情况时有发生,
如果您会注意到,AccountMapperImpl 可能不会生成到 target/build
文件夹
有几种方法可以解决这个问题:
- 尝试清理
out/build
目录
- 运行
mvn clean compile
或 gradle clean compileJava
- 使用
maven/gradle
将您的 IDE 设置为 运行 您的代码
- 确保
Annotation Processing
已启用
我有以下映射器(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 的问题。
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.
我尝试了装饰器的解决方案。通过为接口添加注释 @DecoratedWith(AccountMapperDecorator.class)
并创建以下 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);
}
}
然后我收到。
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")}
在那种情况下可能是什么问题?
你 运行 mvn package
了吗?
当你在你的IDE中搜索classAccountMapperImpl
时,你能找到吗?如果不是那是一个问题。如果找不到,Spring 也找不到。
也许您忘记在 pom.xml 中配置(或配置错误)mapstruct-processor?你有这样的东西吗:
<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>
有吗?
这种情况时有发生,
如果您会注意到,AccountMapperImpl 可能不会生成到 target/build
文件夹
有几种方法可以解决这个问题:
- 尝试清理
out/build
目录 - 运行
mvn clean compile
或gradle clean compileJava
- 使用
maven/gradle
将您的 IDE 设置为 运行 您的代码 - 确保
Annotation Processing
已启用