Announcement Announcement Module
No announcement yet.
Dependency Not Being Injected into PropertyEditorRegistrar Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency Not Being Injected into PropertyEditorRegistrar

    In order to correctly bind a unique entity ID to an object in a controller I am using a custom property editor (that needs to do a database lookup) that registered using the following beans in the dispatcher servlet context:

       <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
          <property name="propertyEditorRegistrars">
                <ref bean="customPropertyEditorRegistrar" />
          class="CustomPropertyEditorRegistrar" />
    Unfortunately the personDao dependency in the registrar is never injected and remains null:

    public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar
    	private PersonDao personDao;
    	public void setPersonDao(PersonDao personDao)
    		this.personDao = personDao;
    	public void registerCustomEditors(PropertyEditorRegistry registry)
    		PersonPropertyEditor personPropertyEditor = new PersonPropertyEditor();
    		registry.registerCustomEditor(Person.class, personPropertyEditor);
    The custom property editor is being using in a controller:

    	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
    So I'm mixing XML and annotation configuration. I have nothing in my root context. Why is the dependency not getting injected?

  • #2
    For starters your configuration is flawed and next to that @Required is doing nothing for dependency injection (I suggest a read of the reference guide).

    The org.springframework.beans.factory.config.CustomEdi torConfigurer is only needed if you need PropertyEditors or PropertyEditorRegistrars available to convert values in your application contexts NOT for binding in web applications.

    Next @Required is doing nothing it only checks if a dependency has been set but it does so only when the RequiredAnnotationBeanPostProcessor is registered. But again it doesn't nothing more then check if a property is injected.

    And finally why are you doing it this way? WHy not simply put @Component on your CustomPropertyEditorRegistrar and @Autowired on the dao property...


    • #3
      If I remove the @Repository annotation on the person dao and move it into the servlet context xml then the dependency gets injected.

      I guess this means that the annotation is being ignored. I have <mvc:annotation-driven /> and <context:annotation-config/> in the servlet contex xml. I am wondering if I should move the <context:annotation-config/> into the root context because only mvc related stuff gets scanned?

      The weird thing is that the person dao gets injected into services injected into the controllers. Is this because the source of the dependencies starts with an MVC related component so gets scanned in this case but not for the registrar?


      • #4
        context:annotation-config registeres the post processor still however @Required does nothing for dependency injection. Also as I stated you don't need the org.springframework.beans.factory.config.CustomEdi torConfigurer because that is only needed if you want type conversion in your application context (it does nothing at all for controllers). This bean is also the source of your problems, because this bean gets instantiated and initialized BEFORE everything else, this is because the CustomEditorConfigurer is configured BEFORE any of the other beans. This is needed because it needs to modify/operate on the ApplicationContext.


        • #5
          Thanks for all the feedback.

          I also tried @Autowired and that didn't work either.

          I did wonder if the whole thing was a bit over the top since I don't need it to convert Person ids in the context xml into person objects (I think this is was you are getting at), but it is what's outlined in the documentation with an example of using it in a controller:

 Registering additional custom PropertyEditors

          I tried moving stuff into the root context, but that didn't work either.

          All this was before I read your (plural) replies so I will rethink my approach on the basis of this. That said I'm still concerned that the dependency is not getting set - from your feedback it appears that this is because the bean has to run before the rest of the context comes up (the context containing the DAO), but then why should it work when the bean is placed in the XML instead of annotated (as I initially tried)?


          • #6
            As suggested I removed the bean definitions from the XML and just used annotations. The dependency gets correctly injected. Thanks for the help.


            • #7
              Look at Controller code in This do the same that do you want to do with Person and PersonDao but with a Category and Category Service. Using Annotations or XML don't does the matter...