MapStruct 装饰器对生成的代码没有影响

MapStruct decorator has no effect generated code

我写了需要的接口机智注释和抽象 class 作为装饰器。生成后(mvn clean package)装饰函数由“默认”过程更新,形成参数和结果类型。我不知道,这是什么问题。你能帮帮我吗?

环境: 地图结构版本 1.4.2.Final 龙目岛版本 1.18.22(Spring 启动 2.6.3) lombok-mapstruct-binding: 0.2.0

        <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-mapstruct-binding</artifactId>
        <version>${lombok-mapstruct-binding.version}</version>
    </dependency>

    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                    </exclude>
                    <exclude>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>${lombok-mapstruct-binding.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

Mapper接口声明:

@Mapper(componentModel = "spring", uses = {DateMapper.class})
@DecoratedWith(BeerMapperDecorator.class)
public interface BeerMapper
{

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

    BeerDto beerToBeerDto(Beer beer);

    Beer beerDtoToBeer(BeerDto beerDto);

    BeerDto beerToBeerDtoWithInventory(Beer beer);

}

装饰师:

public abstract class BeerMapperDecorator implements BeerMapper {

@Autowired
@Qualifier("delegate")
private BeerMapper delegate;

private BeerInventoryService beerInventoryService;

@Autowired
public void setBeerInventoryService(BeerInventoryService beerInventoryService) {
    this.beerInventoryService = beerInventoryService;
}

@Override
public BeerDto beerToBeerDtoWithInventory(Beer beer) {
    BeerDto dto = delegate.beerToBeerDto(beer);
    dto.setQuantityOnHand(beerInventoryService.getOnhandInventory(beer.getId()));

    System.out.println("   -----------  Decorated --------------");

    return dto;
}

}

以及生成的源:

    @Override
public BeerDto beerToBeerDtoWithInventory(Beer beer) {
    if ( beer == null ) {
        return null;
    }

    BeerDtoBuilder beerDto = BeerDto.builder();

    beerDto.id( beer.getId() );
    if ( beer.getVersion() != null ) {
        beerDto.version( beer.getVersion().intValue() );
    }
    beerDto.createdDate( dateMapper.asOffsetDateTime( beer.getCreatedDate() ) );
    beerDto.lastModifiedDate( dateMapper.asOffsetDateTime( beer.getLastModifiedDate() ) );
    beerDto.beerName( beer.getBeerName() );
    beerDto.upc( beer.getUpc() );
    beerDto.price( beer.getPrice() );

    return beerDto.build();
}

检查 BeerMapperDecorator 的实现,此 class 被标记为主要注入依赖项。

应该看起来像这样:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2022-03-01T20:17:02+0100",
    comments = "version: 1.5.0.Beta2, compiler: Eclipse JDT (IDE) 1.4.50.v20210914-1429, environment: Java 17.0.1 (Azul Systems, Inc.)"
)
@Component
@Primary
public class BeerMapperImpl extends BeerMapperDecorator {

    @Autowired
    @Qualifier("delegate")
    private BeerMapper delegate;

    @Override
    public BeerDto beerToBeerDto(Beer beer)  {
        return delegate.beerToBeerDto( beer );
    }

    @Override
    public Beer beerDtoToBeer(BeerDto beerDto)  {
        return delegate.beerDtoToBeer( beerDto );
    }
}

您正在查看的被标记为委托接口:

@Component
@Qualifier("delegate")
public class BeerMapperImpl_ implements BeerMapper {