No qualifying bean with MapStruct

huangapple go评论79阅读模式
英文:

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>

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
			&lt;version&gt;3.8.1&lt;/version&gt;
			&lt;configuration&gt;
				&lt;source&gt;11&lt;/source&gt;
				&lt;target&gt;11&lt;/target&gt;
				&lt;annotationProcessorPaths&gt;
					&lt;path&gt;
						&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
						&lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
						&lt;version&gt;1.3.1.Final&lt;/version&gt;
					&lt;/path&gt;
					&lt;path&gt;
						&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
						&lt;artifactId&gt;lombok&lt;/artifactId&gt;
						&lt;version&gt;1.18.12&lt;/version&gt;
					&lt;/path&gt;
				&lt;/annotationProcessorPaths&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;

这是映射器:

@Mapper(componentModel = &quot;spring&quot;)
public interface ControllerBusinessMapper {
    //一些方法
}

在注入它的类中:

@Autowired
private ControllerBusinessMapper businessMapper;

我的 Spring 配置类将包扫描设置在包层次结构的根目录下。而且映射器的实现生成在 target/generated-sources 下:

@Generated(
    value = &quot;org.mapstruct.ap.MappingProcessor&quot;,
    date = &quot;2020-08-23T03:56:23+0200&quot;,
    comments = &quot;version: 1.3.1.Final, compiler: javac, environment: Java 11.0.7 (Oracle Corporation)&quot;
)
@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:

&lt;dependency&gt;
	&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
	&lt;artifactId&gt;lombok&lt;/artifactId&gt;
	&lt;version&gt;[1.18.12,)&lt;/version&gt;
	&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
	&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
	&lt;artifactId&gt;mapstruct-jdk8&lt;/artifactId&gt;
	&lt;version&gt;1.3.1.Final&lt;/version&gt;
&lt;/dependency&gt;

</dependencies>

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
			&lt;version&gt;3.8.1&lt;/version&gt;
			&lt;configuration&gt;
				&lt;source&gt;11&lt;/source&gt;
				&lt;target&gt;11&lt;/target&gt;
				&lt;annotationProcessorPaths&gt;
					&lt;path&gt;
						&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
						&lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
						&lt;version&gt;1.3.1.Final&lt;/version&gt;
					&lt;/path&gt;
					&lt;path&gt;
						&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
						&lt;artifactId&gt;lombok&lt;/artifactId&gt;
						&lt;version&gt;1.18.12&lt;/version&gt;
					&lt;/path&gt;
				&lt;/annotationProcessorPaths&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;

This is the mapper:

@Mapper(componentModel = &quot;spring&quot;)
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 = &quot;org.mapstruct.ap.MappingProcessor&quot;,
    date = &quot;2020-08-23T03:56:23+0200&quot;,
    comments = &quot;version: 1.3.1.Final, compiler: javac, environment: Java 11.0.7 (Oracle Corporation)&quot;
)
@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

huangapple
  • 本文由 发表于 2020年8月23日 10:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63542765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定