【问题标题】:MapStruct Injection of autowired dependency failed自动装配依赖的 MapStruct 注入失败
【发布时间】:2021-07-17 12:22:56
【问题描述】:

我正在尝试使用 lombok 实现 mapstruct。下面是 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;
}

一旦我尝试从 eclipse STS 构建应用程序,它就可以正常工作。 Impl 文件正在目标文件夹中生成。当我尝试将 war 文件部署到 tomcat 并启动应用程序时,我收到以下错误。

【问题讨论】:

    标签: java spring-mvc mapstruct


    【解决方案1】:

    我不知道为什么会出现 bean 注入错误,因为您没有发布所有应用程序文件。但作为一种解决方案,您可以直接引用映射器实例,而不是使用依赖注入。 在映射器界面顶部添加以下内容:

    VboProofOfDeliveryMapper INSTANCE = Mappers.getMapper(VboProofOfDeliveryMapper.class);
    

    并且直接引用实例:

    VboProofOfDeliveryMapper.INSTANCE.mapPhxVboProofOfDeliveryDtoToPhxVboProofOfDelivery(phxVboProofOfDeliveryVO);
    

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 2013-05-01
      • 2012-10-27
      • 2018-02-21
      • 1970-01-01
      相关资源
      最近更新 更多