是否可以从 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);
// ...
}
另见:
- Mapstruct - How can I inject a spring dependency in the Generated Mapper class
- Mapstruct Expressions
关于java - 来自 application.properties 的 Mapstruct 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69942709/