简体   繁体   中英

No qualifying bean with MapStruct

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>
<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM