是否可以使用Spring的@Value批注来读取和写入自定义类类型的属性值?
例如:
@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;
...
}
Run Code Online (Sandbox Code Playgroud)
编辑解决方案
以下是我使用Spring 4.x API做到的方法......
为Customer类创建了新的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 …Run Code Online (Sandbox Code Playgroud) spring ×1