'MapStruct Injection of autowired dependency failed
I am try to implement mapstruct with lombok. Below is the pom.xml.
Pom.xml
<parent>
<groupId>com.dbcc.ecomm</groupId>
<artifactId>ecomm</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<org.mapstruct.version>1.3.0.Final </org.mapstruct.version>
<org.projectlombok.version>1.12.6</org.projectlombok.version>
</properties>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<pluginManagement>
<!-- Package up tests so we can re-use utilities and support classes -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
Mapper.java
@Mapper(componentModel = "spring")
public interface Mapper {
@Mappings({ @Mapping(source = "sourceName", target = "source"),
@Mapping(source = "subscriber", target = "subscriberId"),
@Mapping(source = "fulfilledDate", target = "fulfilledDate", dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") })
PhxVboProofOfDelivery mapPhxVboProofOfDeliveryDtoToPhxVboProofOfDelivery(
PhxVboProofOfDeliveryVO phxVboProofOfDeliveryVO);
default PhxVboProofOfDelivery addParameterManually(PhxVboProofOfDelivery vboProofOfDelivery,
@Context PhxVboSyncRecord phxVboSyncRecord) {
vboProofOfDelivery.setCreatedBy(DbccConstants.PHX_VBO_POD_SUBSCRIBER);
vboProofOfDelivery.setLastModifiedBy(DbccConstants.PHX_VBO_POD_SUBSCRIBER);
vboProofOfDelivery.setCreatedDate(CommonUtil.getTodaysDateandTime());
vboProofOfDelivery.setLastModifiedDate(CommonUtil.getTodaysDateandTime());
vboProofOfDelivery.setPhxVboSyncRecord(phxVboSyncRecord);
return vboProofOfDelivery;
}
Once I try to build the application from eclipse STS it working fine. The Impl file is getting generated in the target folder.When I try to deploy the war file into tomcat and launch the application, I am getting the below error.
Solution 1:[1]
I don't know why the bean injection error occurs since you did not post all the application files. But as a solution you could reference the mapper instance directly instead of using dependency injection. Add the following at the top of your mapper interface:
VboProofOfDeliveryMapper INSTANCE = Mappers.getMapper(VboProofOfDeliveryMapper.class);
And the reference the instance directly:
VboProofOfDeliveryMapper.INSTANCE.mapPhxVboProofOfDeliveryDtoToPhxVboProofOfDelivery(phxVboProofOfDeliveryVO);
Solution 2:[2]
The Problem is that target Folder is not on the build path nor its is defined as Source Folder. you have to define it as a Source Folder the easiest way is to do as under:
- Right click on the folder where resources are generated inside target folder.
- Open build path tab in the popup List.
- Select the use Folder as Source Folder option.
(Don't use target folder as it would create problems if any of the folders under target directory is already set as source folder).
Look at the Screen Shot as under for Better Clarification.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RaduP |
| Solution 2 |

