自定义类的Spring@Value属性
是否可以使用Spring的@Value注释来读取和写入自定义类类型的属性值 例如:自定义类的Spring@Value属性,spring,Spring,是否可以使用Spring的@Value注释来读取和写入自定义类类型的属性值 例如: @Component @PropertySource("classpath:/data.properties") public class CustomerService { @Value("${data.isWaiting:#{false}}") private Boolean isWaiting; // is this possible for a custom class lik
@Component
@PropertySource("classpath:/data.properties")
public class CustomerService {
@Value("${data.isWaiting:#{false}}")
private Boolean isWaiting;
// is this possible for a custom class like Customer???
// Something behind the scenes that converts Custom object to/from property file's string value via an ObjectFactory or something like that?
@Value("${data.customer:#{null}}")
private Customer customer;
...
}
已编辑的解决方案
下面是我如何使用Spring4.xapi实现的
为客户类创建了新的PropertyEditorSupport类:
public class CustomerPropertiesEditor extends PropertyEditorSupport {
// simple mapping class to convert Customer to String and vice-versa.
private CustomerMap map;
@Override
public String getAsText()
{
Customer customer = (Customer) this.getValue();
return map.transform(customer);
}
@Override
public void setAsText(String text) throws IllegalArgumentException
{
Customer customer = map.transform(text);
super.setValue(customer);
}
}
然后在应用程序的ApplicationConfig类中:
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
Map<Class<?>, Class<? extends PropertyEditor>> customEditors =
new HashMap<Class<?>, Class<? extends PropertyEditor>>(1);
customEditors.put(Customer.class, CustomerPropertiesEditor.class);
CustomEditorConfigurer configurer = new CustomEditorConfigurer();
configurer.setCustomEditors(customEditors);
return configurer;
}
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
Map<Class<?>, Class<? extends PropertyEditor>> customEditors =
new HashMap<Class<?>, Class<? extends PropertyEditor>>(1);
customEditors.put(Customer.class, CustomerPropertiesEditor.class);
customEditors.put(Customer[].class, CustomerPropertiesEditor.class);
CustomEditorConfigurer configurer = new CustomEditorConfigurer();
configurer.setCustomEditors(customEditors);
return configurer;
}
@Bean
public CustomEditorConfigurer CustomEditorConfigurer(){
Map,Class这是可能的,但是阅读Spring文档。您可以看到以下示例:
示例用法
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
请参阅详细信息您必须创建一个扩展属性编辑器支持的类
public class CustomerEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) {
Customer c = new Customer();
// Parse text and set customer fields...
setValue(c);
}
}
Spring可以读取属性并将它们直接加载到类中。
此外,通过使代码更干净,您可以在类的顶部添加,而不是逐个连接每个嵌套属性
鉴于上述情况,下面是最后一个示例,并给出了解释:
// File: CustomerConfig.java
@Configuration
// Set property source file path (optional)
@PropertySource("classpath:/data.properties")
// Put prefix = "data" here so that Spring read properties under "data.*"
@ConfigurationProperties(prefix = "data")
public class CustomerConfig {
// Note: Property name here is the same as in the file (data.customer)
// Spring will automatically read and put "data.customer.*" properties into this object
private Customer customer;
// Other configs can be added here too... without wiring one-by-one
public setCustomer(Customer customer){
this.customer = customer;
}
public getCustomer(){
return this.customer;
}
}
就是这样,现在您有了“data.customer.*”属性,可以通过CustomerConfig.getCustomer()加载和访问
要将其集成到您的服务中(基于示例代码):
这样,将配置读入类就不需要“魔法破解”。
请查看文档/示例,并从中获取更多有用的信息
注意:我建议不要使用PropertyEditorSupport,因为
a) 它是为不同的目的而构建的,将来可能会因破坏代码而改变
b) 它需要手动“处理”内部代码=>可能的错误
取而代之的是,使用为这个目的而构建的东西(Spring已经拥有了它),以便使代码更容易理解,并获得可能在将来(或现在)进行的内部改进/优化
进一步改进:您的CustomerService似乎也充斥着配置(@PropertyService)。我建议您也通过另一个类读取这些属性(类似)然后在这里连接该类,而不是在CustomerService中执行所有操作。如果您想将其与列表一起使用,可以使用array替代
将您的属性定义为Customer[]而不是List,然后:
在ApplicationConfig类中:
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
Map<Class<?>, Class<? extends PropertyEditor>> customEditors =
new HashMap<Class<?>, Class<? extends PropertyEditor>>(1);
customEditors.put(Customer.class, CustomerPropertiesEditor.class);
CustomEditorConfigurer configurer = new CustomEditorConfigurer();
configurer.setCustomEditors(customEditors);
return configurer;
}
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
Map<Class<?>, Class<? extends PropertyEditor>> customEditors =
new HashMap<Class<?>, Class<? extends PropertyEditor>>(1);
customEditors.put(Customer.class, CustomerPropertiesEditor.class);
customEditors.put(Customer[].class, CustomerPropertiesEditor.class);
CustomEditorConfigurer configurer = new CustomEditorConfigurer();
configurer.setCustomEditors(customEditors);
return configurer;
}
谢谢Efe。这就是我一直在寻找的。我正在编辑我的第一篇文章以展示完整的解决方案。有没有办法为类型列表指定自定义编辑器?