title: Customizing PropertyEditorSupport in Spring
body: <p>I'm working with Spring 3.2. In order to validate double values globally, I use <code>CustomNumberEditor</code>. The validation is indeed performed.</p>
  
  <p>But when I input a number like <code>1234aaa</code>, <code>123aa45</code> and so forth, I expect the <code>NumberFormatException</code> to be thrown but it doesn't. The docs says,</p>
  
  <blockquote>
    <p>ParseException is caused, if the beginning of the specified string cannot be
    parsed</p>
  </blockquote>
  
  <p>Therefore, such values as mentioned above are parsed up to they are represented as numbers and the rest of the string is then omitted.</p>
  
  <p>To avoid this, and to make it throw an exception, when such values are fed, I need to implement my own Property Editor by extending the <code>PropertyEditorSupport</code> class as mentioned in this <a href="https://stackoverflow.com/q/4726861/1391249">question</a>.</p>
  
  <pre><code>package numeric.format;
  
  import java.beans.PropertyEditorSupport;
  
  public final class StrictNumericFormat extends PropertyEditorSupport
  {
      @Override
      public String getAsText() 
      {
          System.out.println("value = "+this.getValue());
          return ((Number)this.getValue()).toString();
      }
  
      @Override
      public void setAsText(String text) throws IllegalArgumentException 
      {
          System.out.println("value = "+text);
          super.setValue(Double.parseDouble(text));
      }
  }
  </code></pre>
  
  <p>The editors I have specified inside a method annotated with the <code>@InitBinder</code> annotation are as follows.</p>
  
  <pre><code>package spring.databinder;
  
  import java.text.DateFormat;
  import java.text.DecimalFormat;
  import java.text.Format;
  import java.text.NumberFormat;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import org.springframework.beans.propertyeditors.CustomDateEditor;
  import org.springframework.beans.propertyeditors.CustomNumberEditor;
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.InitBinder;
  import org.springframework.web.context.request.WebRequest;
  
  @ControllerAdvice
  public final class GlobalDataBinder 
  {
      @InitBinder
      public void initBinder(WebDataBinder binder, WebRequest request)
      {
          DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
          dateFormat.setLenient(false);
          binder.setIgnoreInvalidFields(true);
          binder.setIgnoreUnknownFields(true);
          //binder.setAllowedFields("startDate");
          binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  
          //The following is the CustomNumberEditor
  
          NumberFormat numberFormat = NumberFormat.getInstance();
          numberFormat.setGroupingUsed(false);
          binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, numberFormat, false));
      } 
  }
  </code></pre>
  
  <p>Since I'm using Spring 3.2, I can take advantage of <a href="http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html" rel="nofollow noreferrer"><code>@ControllerAdvice</code></a></p>
  
  <hr>
  
  <p>Out of curiosity, the <strong>overridden</strong> methods from the <code>PropertyEditorSupport</code> class in the <code>StrictNumericFormat</code> class are never invoked and the statements that redirect the output to the console as specified inside of those methods (<code>getAsText()</code> and <code>setAsText()</code>)  don't print anything on the server console.</p>
  
  <p>I have tried all the approaches described in all the answers of that <a href="https://stackoverflow.com/q/4726861/1391249">question</a> but none worked for me. What am I missing here? Is this required to configure in some xml file(s)? </p>