Search code examples
javaspring-bootwildflymapstruct

@Mapper UnsatisfiedDependencyException while deploying


I'm getting UnsatisfiedDependecyException at @Autowired private ApplicantDetailMapper mapper; in service class. In service class repositories are autowried too and they are working, so assume component scan is working.

I'm clueless about this issue.

Mapper interface using spring

@Mapper(componentModel = "spring")
public interface ApplicantDetailMapper {

 ApplicantEntity dtoToEntity(ApplicantDto source);

}

Service in which it is autowired

@Service
@Transactional
public class ApplicantDetailServiceImpl implements ApplicantDetailService {

@Autowired
private ExampleRepository repo;

@Autowired
private ApplicantDetailMapper mapper;

}

set up in pom.xml

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
        
    <properties>
      <org.mapstruct.version>1.4.0.Final</org.mapstruct.version>
    </properties>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>

Exception

Unsatisfied dependency expressed through field 'applicantDetailService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicantDetailServiceImpl': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dtos.mappers.ApplicantDetailMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

EDIT

Eclipse IDE For Enterprise Java Developers 2020-06


Solution

  • According to the official documentation

    If you are working with the Eclipse IDE, make sure to have a current version of the M2E plug-in. When importing a Maven project configured as shown above, it will set up the MapStruct annotation processor so it runs right in the IDE, whenever you save a mapper type. Neat, isn’t it? To double check that everything is working as expected, go to your project’s properties and select "Java Compiler" → "Annotation Processing" → "Factory Path". The MapStruct processor JAR should be listed and enabled there. Any processor options configured via the compiler plug-in (see below) should be listed under "Java Compiler" → "Annotation Processing". If the processor is not kicking in, check that the configuration of annotation processors through M2E is enabled. To do so, go to "Preferences" → "Maven" → "Annotation Processing" and select "Automatically configure JDT APT". Alternatively, specify the following in the properties section of your POM file: <m2e.apt.activation>jdt_apt</m2e.apt.activation>. Also make sure that your project is using Java 1.8 or later (project properties → "Java Compiler" → "Compile Compliance Level"). It will not work with older versions.

    So as it says Factory Path should show MapStruct Jar listed and enabled there. In my case it wasn't there at all.

    Setting "Automatically configure JDT APT" caused eclipse to show Maven Update Error. with details as

    update maven encountered problem Utility Module and Dynamic Web Module 3.1 cannot both be selected.

    So For me adding <m2e.apt.activation>jdt_apt</m2e.apt.activation> in pom.xml properties solved the problem.

    Source: MapStruct Documentation