问题遇到的现象和发生背景
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类;