title: Spring @Value property for custom class
body: <p>Is it possible to use Spring's @Value annotation to read and write property values of a custom class type?</p>
  
  <p>For example:</p>
  
  <pre><code>@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;
  
      ...
  }
  </code></pre>
  
  <p><strong>EDITED SOLUTION</strong></p>
  
  <p>Here is how I did it using Spring 4.x APIs...</p>
  
  <p>Created new PropertyEditorSupport class for Customer class:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Then in application's ApplicationConfig class:</p>
  
  <pre><code>@Bean
  public CustomEditorConfigurer customEditorConfigurer() {
  
      Map&lt;Class&lt;?&gt;, Class&lt;? extends PropertyEditor&gt;&gt; customEditors = 
              new HashMap&lt;Class&lt;?&gt;, Class&lt;? extends PropertyEditor&gt;&gt;(1);
      customEditors.put(Customer.class, CustomerPropertiesEditor.class);
  
      CustomEditorConfigurer configurer = new CustomEditorConfigurer();
      configurer.setCustomEditors(customEditors);
  
      return configurer;
  }
  </code></pre>
  
  <p>Cheers,
  PM</p>