属性 个值的自定义类型转换
Custom type conversion of property values
我有一个 myapp.properties
文件,其键值对定义为:
prefix.int-field=123
prefix.string-field=asdf
prefix.custom-type-field=my.package.CustomType
我正在尝试通过在以下 class:
中使用 @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.
要解决您的眼前问题,您需要查看 bean 上的 @PostConstruct
选项。这将允许您在 bean 对上下文可用之前采取行动。
@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}")
private String customFieldType;
private CustomInterface customField;
@PostConstruct
public void init() {
customField = (CustomInterface) Class.forName(customFieldType).newInstance(); // short form... will need checks that it finds the class and can create a new instance
}
}
class CustomType implements CustomInterface {...}
interface CustomInterface {...}
我很好奇您是否想在 class 上使用 @Configuration
注释并创建 CustomInterface
的实例,该实例在 Spring 应用上下文。要做到这一点,你应该做这样的事情:
@Component
@ConfigurationProperties(prefix = "prefix")
class MySettings {
private int intField;
private String stringField;
private String customTypeField;
// getters and setters
}
然后将用于 @Configuration
class:
@Configuration
class MyConfiguration {
@Bean
public CustomInterface customInterface(MySettings mySettings) {
return (CustomInterface) Class.forName(mySettings.getCustomTypeField()).newInstance();
}
}
此时您将拥有 CustomInterface
的实例化 bean,您可以 Spring 将其自动装配到其他对象中。
我有一个 myapp.properties
文件,其键值对定义为:
prefix.int-field=123
prefix.string-field=asdf
prefix.custom-type-field=my.package.CustomType
我正在尝试通过在以下 class:
中使用@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.
要解决您的眼前问题,您需要查看 bean 上的 @PostConstruct
选项。这将允许您在 bean 对上下文可用之前采取行动。
@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}")
private String customFieldType;
private CustomInterface customField;
@PostConstruct
public void init() {
customField = (CustomInterface) Class.forName(customFieldType).newInstance(); // short form... will need checks that it finds the class and can create a new instance
}
}
class CustomType implements CustomInterface {...}
interface CustomInterface {...}
我很好奇您是否想在 class 上使用 @Configuration
注释并创建 CustomInterface
的实例,该实例在 Spring 应用上下文。要做到这一点,你应该做这样的事情:
@Component
@ConfigurationProperties(prefix = "prefix")
class MySettings {
private int intField;
private String stringField;
private String customTypeField;
// getters and setters
}
然后将用于 @Configuration
class:
@Configuration
class MyConfiguration {
@Bean
public CustomInterface customInterface(MySettings mySettings) {
return (CustomInterface) Class.forName(mySettings.getCustomTypeField()).newInstance();
}
}
此时您将拥有 CustomInterface
的实例化 bean,您可以 Spring 将其自动装配到其他对象中。