Results 1 to 3 of 3

Thread: Question on use of custom property editors - problem found?

  1. #1
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default Question on use of custom property editors - problem found?

    I am fairly new to the Spring framework, developing a web application for internal use.

    I'd request help explaining to me whether I've missed something.

    I've registered several custom property editors using the CustomEditorConfigurer bean factory post-processor.

    It *appears* that there is no code that will suck those custom editors into the BeanWrapperImpl that is used by Spring's MVC/web piece ServletDataBinder and, ultimately, the validation piece's DataBinder.

    Am I missing something blatantly obvious? I can see that the constructor of BeanWrapperImpl will force-register certain well-defined PropertyEditors, but I can't find anywhere in the code (nor in tracing through with a debugger) where it would pick up the additional registered custom editors that I've defined in my application context.

    I added hacked my SimpleFormController's initBinder method to suck up the custom editors from the Context and BeanFactory, and once this occurs, the Binder does work as expected - that is, when binding request parameters, my custom editors are invoked as I'd expect.

    Code:
            ConfigurableApplicationContext cxt = (ConfigurableApplicationContext) getApplicationContext();
            AbstractBeanFactory abf = (AbstractBeanFactory) cxt.getBeanFactory();
            Map editors = abf.getCustomEditors();
            Iterator keySet = editors.keySet().iterator();
            while (keySet.hasNext()) {
                Class className = (Class) keySet.next();
                PropertyEditor propEdit = (PropertyEditor) editors.get(className);
                binder.registerCustomEditor(className, propEdit);
            }
    My question is - there has to be some way for the default Binder to pick up the custom property editors, as they're around in the BeanFactory for my application context. What's the bit that I'm missing here? Or is it a bug/feature request?

    Thanks in advance - Peter

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    It *appears* that there is no code that will suck those custom editors into the BeanWrapperImpl that is used by Spring's MVC/web piece ServletDataBinder and, ultimately, the validation piece's DataBinder.
    The CustomEditorConfigurers configure custom editors for the application context, not for the ServletRequestDataBinder, so you're right about this.

    The solution you're currently using should be okay, as long as the Map call to getCustomEditors() returns new editors every time; PropertyEditors are not thread-safe.

    I've noticed a couple of people lately asking for more standard a infrastructure to register custom editors in controllers. Maybe you could file a feature request in JIRA?

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Lightbulb PropertyEditors don't have to be Spring beans

    In simple use cases, the process is normally as follows:
    1. Write your custom PropertyEditor class
    2. Extend SimpleFormController
    3. In your controller, override initBinder, so that it creates (using Java's "new" operator) instances of your custom PropertyEditor and then registers them against the appropriate types/fields.
    If you wanted to define your PropertyEditors as Spring beans for some reason, I guess you could inject them into your controller (via its constructor or a setter, according to your religion) instead of using the "new" operator. You would then register them within the initBinder method as above.

    But you're right that there doesn't seem to be any way for controllers to automatically detect your custom property editors.
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  4. Replies: 2
    Last Post: May 13th, 2005, 05:42 AM
  5. Replies: 1
    Last Post: Apr 13th, 2005, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •