答案 0 :(得分:3)
答案 1 :(得分:2)
要添加到上述jst的答案中,Spring Boot批注 @ConfigurationPropertiesBinding 可用于Spring Boot来识别用于属性绑定的转换器,如24.7.4 Properties Conversion中所述:
@Component
@ConfigurationPropertiesBinding
public class StringToPathConverter implements Converter<String, Path> {
@Override
public Path convert(@NonNull String pathAsString) {
return Paths.get(pathAsString);
}
}
答案 2 :(得分:0)
我采纳了james的想法,并在spring boot配置中定义了转换器:
@SpringBootConfiguration
public class Configuration {
public class PathConverter implements Converter<String, Path> {
@Override
public Path convert(String path) {
return Paths.get(path);
}
}
@Bean
@ConfigurationPropertiesBinding
public PathConverter getStringToPathConverter() {
return new PathConverter();
}
}