Announcement Announcement Module
Collapse
No announcement yet.
Service injection & PropertyEditor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Service injection & PropertyEditor

    Hi, I'm trying to use a PropertyEditor to format the output of an entity in my jsp, but a strange error appears...

    Suppose the entity Gender and a service with a method to retrieve genders from database by its identifier. So, the code would be... ("#" means "at" symbol - forum not allow me to post this character due to my post number)

    Code:
    public class GenderPropertyEditor extends PropertyEditorSupport
    {
    
    	#Autowired private ResearcherService researcherService;
    	
    	public GenderPropertyEditor(){}
    	
    
    	public void setAsText(String text)
    	{
    		
    		Gender gender;
    		try
    		{
    			gender = researcherService.getGender(new Integer(text));
    			setValue(gender);
    		} 
    		catch (NumberFormatException e)
    		{
    			setValue(null);
    		} 
    		catch (InstanceNotFoundException e)
    		{
    			setValue(null);
    		}
    
    	}
    		
    	
    	public String getAsText() 
    	{
    		Gender gender = (Gender)getValue();
    			
    		if (gender == null)
    			return "";
    			
    		return gender.getGenderID().toString();
    	}
    }
    and the binder:

    Code:
    public class LBDPortalBindingInitializer implements WebBindingInitializer 
    {
    	#InitBinder
    	public void initBinder(WebDataBinder binder, WebRequest request)
    	{
    		binder.registerCustomEditor(Gender.class, new GenderPropertyEditor() );
    	}
    
    }
    In debug mode, all data is filled correctly in property editor's methods, but the service is null. The service isn't being injected to this editor, but I can't found the reason.

    żAny missing annotation? żNeed more configuration?

    Thanks

  • #2
    Hi, I'm trying to use a PropertyEditor to format the output of an entity in my jsp, but a strange error appears...
    Well nothing strange is happening, what you see is what I would expect to happen and how it should happen. Spring will only inject into beans it knows about, your PropertyEditor is (correctly!) outside the ApplicationContext. So Spring wil not inject it.


    You have a couple of solutions:

    1. Inject your service into the LBDPortalBindingInitializer and create a constructor on the GenderPropertyEditor which takes the services.
    2. Register your GenderPropertyEditor in the ApplicationContext and mark it as a prototype, make your Initializer ApplicationContextAware and in the initBinder method retrieve a new instance from the ApplicationContext.
    3. Register your GenderPropertyEditor in the ApplicationContext and mark it as scope request and aop:scoped-proxy and inject it into your Initializer, then simply use it in your initBinder method. Spring will create a request scoped proxy for you.

    Comment


    • #3
      A lot of thanks, Marten.

      It works perfectly using the constructor approach

      Thinking about the problem, I was assuming that Spring will find this class scaning the packages (due to base scan package provided in the configuration) and directly inject the service -- because it was marked by Autowired --, but I was wrong.

      Thanks!

      Comment


      • #4
        If you have annotated your PropertyEditor with @Component it will. However that wouldn't be the instance you are using, because you are creating a new one yourself, outside the scope of Spring. So even if that did happen it still wouldn't have worked.

        Comment


        • #5
          Before getting your solution, I annotate entity with @Component, but also fails (NullPointerException for the service).

          As I said, constructor solution works perfect

          Comment


          • #6
            Before getting your solution, I annotate entity with @Component, but also fails (NullPointerException for the service).
            Ehrm that is what I tried to explain.

            Originally posted by mdeinum
            If you have annotated your PropertyEditor with @Component it will. However that wouldn't be the instance you are using, because you are creating a new one yourself, outside the scope of Spring. So even if that did happen it still wouldn't have worked.
            Although it might not be the best sentences I wrote .

            Comment

            Working...
            X