Valyce 2021-12-02 14:53 采纳率: 0%
浏览 160
已结题

vscode,java使用mapstruct不生成Impl类

问题遇到的现象和发生背景

vscode编辑器下,java语言、技术:mapstruct;
1、通过gradle的tasks,可以正常编辑程序,且可以生成Impl类;
2、但直接F5调试,vscode编辑器下,不能生成Impl类,导致出错;在Idea编辑器下可以直接运行(不通过gradle);
interface CarMapper接口,不能自动生成OrderMapperImpl实现类。

我网上浏览了很多网页,没找到解决办法,相同问题如:
https://stackoverflow.com/questions/60809894/using-java-mapstruct-with-vscode
https://github.com/redhat-developer/vscode-java/issues/1660
https://github.com/yousetme/mapstruct-gradle-demo

运行结果及报错内容
[  restartedMain] com.example.demo.DemoApplication         : Starting DemoApplication using Java 11.0.2 on DESKTOP-GGGVRDA with PID 23220 (C:\Users\kxyang\Desktop\demo\demo\bin\main started by kxyang in C:\Users\kxyang\Desktop\demo\demo)
[  restartedMain] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
[  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
[  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
[  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
[  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
[  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2305 ms
[  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.Service.CarMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup="", name="", description="", authenticationType=CONTAINER, type=java.lang.Object.class, mappedName="")}
[  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
[  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:
A component required a bean of type 'com.example.demo.Service.CarMapper' that could not be found.

Action:
Consider defining a bean of type 'com.example.demo.Service.CarMapper' in your configuration.

我的代码片段
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CarMapper {
    CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); 
 
    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto carToCarDto(Car car); 
}

###build.gradle文件


```java
plugins {
    id 'org.springframework.boot' version '2.5.7'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
    implementation 'org.mapstruct:mapstruct:1.3.1.Final'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
    annotationProcessor 'com.google.auto.value:auto-value:1.5'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}





###### launch.json文件

```java
    "version": "0.2.0",
    "configurations": [       
        {
            "type": "java",
            "name": "Launch DemoApplication",
            "request": "launch",
            "mainClass": "com.example.demo.DemoApplication",
            "projectName": "demo"           
        }
    ]

####编译后结果
1、bin文件夹下vscode直接运行程序创建的目录;
2、build文件夹是gradle的tasks运行后产生的目录,可以看出来已经生成了OrderMapperImpl类;

img

  • 写回答

1条回答 默认 最新

  • 有问必答小助手 2021-12-03 21:25
    关注

    你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


    本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


    因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

    评论

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 修改了问题 12月3日
  • 修改了问题 12月2日
  • 修改了问题 12月2日
  • 展开全部

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。