英文:
No qualifying bean with MapStruct
问题
我在使用MapStruct、Lombok和Spring时,在Tomcat 9上部署应用程序时遇到以下错误:
> 找不到类型为 'somepackage.ControllerBusinessMapper' 的合格bean
> 可用:预期至少有1个合格的bean可用于自动装配
> 候选项
这是我的 pom.xml 文件:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>[1.18.12,)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
这是映射器:
@Mapper(componentModel = "spring")
public interface ControllerBusinessMapper {
//一些方法
}
在注入它的类中:
@Autowired
private ControllerBusinessMapper businessMapper;
我的 Spring 配置类将包扫描设置在包层次结构的根目录下。而且映射器的实现生成在 target/generated-sources 下:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-08-23T03:56:23+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.7 (Oracle Corporation)"
)
@Component
public class ControllerBusinessMapperImpl implements ControllerBusinessMapper {
//一些方法
}
我遇到的错误表明 Spring 无法找到实现类,我漏掉了什么?我尝试将 generated-sources 文件夹添加到构建路径并在包扫描中包含它,但没有起作用。
英文:
I get the following error on application deployment in Tomcat 9 when using MapStruct, Lombock and Spring:
> No qualifying bean of type 'somepackage.ControllerBusinessMapper'
> available: expected at least 1 bean which qualifies as autowire
> candidate
This is my pom.xml:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>[1.18.12,)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
This is the mapper:
@Mapper(componentModel = "spring")
public interface ControllerBusinessMapper {
//Some methods
}
And in the class where its injected:
@Autowired
private ControllerBusinessMapper businessMapper;
My spring configuration class sets the package scan in the root of the package hierarchy. And also the implementation of the mapper is generated under target/generated-sources:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-08-23T03:56:23+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.7 (Oracle Corporation)"
)
@Component
public class ControllerBusinessMapperImpl implements ControllerBusinessMapper {
//Some methods
}
The error I have suggests Spring is not able to find the implementation class, what am I missing? I tried to add the generated-sources folder to the build path and include it in the package scan but it didnt work.
答案1
得分: 1
在Eclipse中需要进行附加配置才能使用MapStruct:
- 安装m2e-apt插件
- 在每个Maven模块中转到Maven > 注解处理
- 勾选启用项目特定设置
- 选择自动配置JDT APT模式
英文:
Additional configuration in Eclipse is needed in order to use MapStruct:
- Install m2e-apt plugin
- In each maven module go to Maven > Annotation Processing
- Check Enable Project Specific Settings
- Select Automatically configure JDT APT mode
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论