gpt4 book ai didi

java - 来自 application.properties 的 Mapstruct 值

转载 作者:行者123 更新时间:2023-12-05 00:52:37 28 4
gpt4 key购买 nike

是否可以从 application.properties 文件的字段中设置值?

我正在寻找类似的东西

    @Mapping(target="version", expression="${application.version}")
StateDto stateToStateDto(State state);

其中 application.version=v1 来自 application.properties 文件。

最佳答案

考虑一个“实用服务”,例如:

@Service
public class PropertyService {

@org.springframework.beans.factory.annotation.Value("${application.version}")
private String appVersion;

// accessors, more properties/stuff..
}

然后你可以定义你的映射:

@Mapper(// ..., ...
componentModel = "spring")
public abstract class StateMapper {

@Autowired
protected PropertyService myService;

@org.mapstruct.Mapping(target="version", expression="java(myService.getAppVersion())")
public abstract StateDto stateToStateDto(State state);
// ...
}

另见:


My minimal solution @github

关于java - 来自 application.properties 的 Mapstruct 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69942709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com