У меня есть myapp.properties
файл с парами ключ-значение определяется как:Пользовательские преобразования тип собственности значения
prefix.int-field=123
prefix.string-field=asdf
prefix.custom-type-field=my.package.CustomType
Я пытаюсь внедрить эти свойства с помощью @Value
аннотацию в следующем классе:
@PropertySource(value = "classpath:myapp.properties")
@Component
class MySettings {
@Value("${prefix.int-field}")
private int intField;
@Value("${prefix.string-field}")
private String stringField;
@Value("${prefix.custom-type-field}") // <-- this is the problem
private CustomInterface customField;
}
class CustomType implements CustomInterface {...}
interface CustomInterface {...}
Теперь intField
и stringField
инициализируются с требуемыми значениями, как и ожидалось, но customField
бросает исключение:
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [my.package.CustomInterface]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:303) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:125) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:61) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
Как преобразовать значения свойств текста в мой пользовательский тип?
Я попытался проконсультироваться с documentation, но я не вижу правильного пути его выполнения. Я использую Spring Boot 1.3.6.
Вы пытались добавить свой собственный конвертер? Это должен быть класс, реализующий Converter, подробнее http://stackoverflow.com/questions/34239585/how-to-register-custom-converters-in-spring-boot –
freakman
Почему бы вам просто не ввести это как фасоль? –
@freakman Я еще не пробовал, я новичок в Spring и смущен. Я дам ему выстрел – radoh