Spring @Value property for custom class

后端 未结 3 1617
故里飘歌
故里飘歌 2021-01-04 10:04

Is it possible to use Spring\'s @Value annotation to read and write property values of a custom class type?

For example:

@Component
@PropertySource(\         


        
3条回答
  •  猫巷女王i
    2021-01-04 11:11

    You have to create a class extending PropertyEditorSupport.

    public class CustomerEditor extends PropertyEditorSupport {
      @Override
      public void setAsText(String text) {
        Customer c = new Customer();
        // Parse text and set customer fields...
        setValue(c);
      }
    }
    

提交回复
热议问题