mapstruct 启动报错,bean注入失败,转换实现方法不能生成

第一个报错
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. [ERROR][org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:40] - *************************** APPLICATION FAILED TO START *************************** Description: A component required a bean of type 'com.xxx.xxx.convert.SubNoticeConvertor' that could not be found. Action: Consider defining a bean of type 'com.xxx.xxx.convert.SubNoticeConvertor' in your configuration.


那么我就把@Mapper(componentModel = "spring") 替换为@Mapper后利用Mappers.getMapper的方式还是不行。
2、这下换了另外的报错,Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.xxx.xxx.convert.SubNoticeConvertor at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61) ~[mapstruct-1.5.3.Final.jar:?] at com.
 

后边一顿操作引入maven
 

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.3.Final</version>
</annotationProcessorPath>
<!-- Lombok 注解处理器 -->
<!-- <annotationProcessorPath>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <version>1.18.24</version> &lt;!&ndash; 使用稳定版 &ndash;&gt;-->
<!-- </annotationProcessorPath>-->
</annotationProcessorPaths>
</configuration>
</plugin>
oh no!还是不可以!
好吧。。。查看idea的设置,发现也是正常的

一次次maven clean,一次次重启服务,转换类的实现方法一直没有编译成功!陷入自闭。。。


最后说一下奇葩解决方法:
idea清理缓存
File-Invalidate Caches 
看到的都勾选并清理重启idea



重启后,maven clean一下后,启动项目正常能转换。Spring注入的方式和INSTANCE方式都可以正常转换,并且去除了之前添加的plugin后也可以正常转换对象

### 解决 MapStruct 注入 Bean 失败方法 当遇到 MapStruct 注入 Bean 失败的情况时,通常是因为配置文件或依赖项设置不当所致。为了确保能够成功注入 Mapper 类型的 Bean,在项目中应当遵循特定的最佳实践。 #### 正确引入依赖 首先确认 `pom.xml` 或者构建工具中的依赖已正确添加: ```xml <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${mapstruct.version}</version> </dependency> <!-- 如果使用的是 Java 8 及以上版本 --> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> <scope>provided</scope> </dependency> ``` #### 使用 Spring 组件扫描功能自动注册 Mappers 为了让 Spring 能够识别并管理由 MapStruct 创建的接口实现类,可以采用如下方法之一: ##### 方法一:通过注解声明为组件 在定义 Mapper 接口时加上 `@Component` 注解使其成为 Spring 容器的一部分[^3]。 ```java import org.springframework.stereotype.Component; import org.mapstruct.Mapper; @Component @Mapper(componentModel = "spring") // 显式指定 spring 模型 public interface UserMapper { UserDTO userToUserDto(User user); } ``` ##### 方法二:全局启用 ComponentScan 另一种做法是在应用程序的主要启动类或其他合适位置开启对包路径下的组件扫描,从而让所有的带有 `@Mapper` 的接口都能被检测到并实例化为 Beans。 ```java @SpringBootApplication @ComponentScan(basePackages = {"com.example.mapper"}) // 替换成实际存放 mappers 的包名 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 验证配置有效性 完成上述更改之后,重启应用服务器,并检查日志输出是否有任何关于无法创建所需 Bean 的警告信息;如果没有,则说明问题已经得到解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值