Results 1 to 6 of 6

Thread: Service injection & PropertyEditor

  1. #1
    Join Date
    Aug 2008
    Posts
    27

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2008
    Posts
    27

    Default

    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!

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2008
    Posts
    27

    Default

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

    As I said, constructor solution works perfect

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

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

    Quote 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 .
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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