Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Issue with basic dependency injection.

  1. #1
    Join Date
    May 2011
    Posts
    7

    Default Issue with basic dependency injection.

    Hi Guys,

    I have a scenario where I need to inject values to the Arraylist in a class that does not have the setter I believe in this scenario I need to do a get on the list in the POC class and then do a add:

    public class POC {

    private ArrayList<String> beheaviour=new ArrayList<String>();

    public ArrayList<String> getBeheaviour() {
    return beheaviour;
    }


    }
    Here is the xml mapping code :

    <bean id="poc" class="outBoundocument.factory.POC">
    <property name="beheaviour">
    <list>
    <value>temp1</value>
    <value>temp2</value>
    <value>temp3</value>
    <value> temp4</value>
    <value>temp5</value>
    </list>
    </property>
    </bean>

    the following code returns :

    Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'beheaviour' of bean class [outBoundocument.factory.POC]: Bean property 'beheaviour' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?


    Moreover there is no way for me to modify the POC class so I will have to perform a property Injection some how. The Java translation of the above is as simple as

    POC poc =new POC ();
    poc.getBeheaviour.add("crap")
    Last edited by swaroopanl; May 23rd, 2011 at 02:07 PM. Reason: updated it to make more sense

  2. #2

    Default

    You defined bean meta data in the context.xml as create a bean POC and inject a property named (behavior) with a setter injection, Hence you need to define a setter method in your POC class , To resolve the issue just add a setter method in your POC class


    public void setBeheaviour(List<String> beheaviour)
    {
    this.beheaviour = beheaviour;
    }

    Code:
    <bean id="poc" class="outBoundocument.factory.POC">
    <property name="beheaviour">
    <list>
    <value>temp1</value>
    <value>temp2</value>
    <value>temp3</value>
    <value> temp4</value>
    <value>temp5</value>
    </list>
    </property> 
    </bean>
    -- Prasanna Talakani
    Blog

  3. #3
    Join Date
    May 2011
    Posts
    7

    Default

    Hi Patalakanti,

    As I have mentioned in the post there is no way for me to change the POC class as this it is a vendor provided class.Just for example you would run in to this scenario when the classes are generated trough JAXB too.Thanks for the post but the answer is not that simple as you suggested unfortunately.

  4. #4

    Post

    Sorry I missed the part about not being able to change POC class, If I understand you'r requirements, You are trying to extend POC class that is belongs to a third party

    You can do some thing like this (It will not work if the POC has defesive copying for behavior property)

    Code:
    public class POCExtension 
    {
    	
    	private POC test;
    	
    	public POCExtension(List<String> behaviorList, POC pocBean)
    	{
    		this.test = pocBean;
    		this.test.getBeheaviour().addAll(behaviorList);
    	}
    
    	public POC getTest() {
    		return test;
    	}
    }
    and configure your POC in context.xml file

    Code:
    <bean class="spring.jpa.service.POCExtension">
    <constructor-arg name="behaviorList">
    <list>
      <value>one</value>
    </list>
    </constructor-arg>
    <constructor-arg name="pocBean" ref="test"/>
    </bean>
    This will change the state of POC class (what I am suggesting is essetially a hack)
    -- Prasanna Talakani
    Blog

  5. #5
    Join Date
    May 2011
    Location
    delhi
    Posts
    10

    Default

    Hi
    Add a new class which extends your POC class and add a setter in the extended class. In your bean definition use this extended class. Hope this may solve your problem

    public class POC
    {
    private List<String> lists=new ArrayList<String>();

    public List<String> getLists() {
    return lists;
    }
    }


    public class POCExtended extends POC{

    public void setLists(List<String> lists) {
    getLists().addAll(lists);


    }
    }



    <bean id="POCBean" class="POCExtended">
    <property name="lists">
    <util:list list-class="java.util.ArrayList">
    <value>1</value>
    <value>2</value>
    <value>3</value>
    </util:list>
    </property>
    </bean>
    Last edited by vishnu.joshi; May 23rd, 2011 at 04:18 PM.

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

    Default

    Or write a FactoryBean which does the complex stuff.

    Code:
    public class POCFactoryBean implements FactoryBean<POC> {
    
      private List<String> beheaviour=new ArrayList<String>();
    
      public void setBeheaviour(List<String> beheaviour) {
        this.beheaviour=beheaviour;
      }
    
      public POC getObject() {
        POC poc = new POC();
        poc.getBeheaviour().addAll(this.beheaviour);
        return poc;
      }
    
    }
    Hopefully the class isn't very well written and the getBeheaviour returns the internal representation of the list and not a copy or unmodifiable list (which is what I would do ). If that is the case you would need to use reflection to set the field value instead of using the getter and add.
    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

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

    Default

    Or write a FactoryBean which does the complex stuff.

    Code:
    public class POCFactoryBean implements FactoryBean<POC> {
    
      private List<String> beheaviour=new ArrayList<String>();
    
      public void setBeheaviour(List<String> beheaviour) {
        this.beheaviour=beheaviour;
      }
    
      public POC getObject() {
        POC poc = new POC();
        poc.getBeheaviour().addAll(this.beheaviour);
        return poc;
      }
    
    }
    Hopefully the class isn't very well written and the getBeheaviour returns the internal representation of the list and not a copy or unmodifiable list (which is what I would do ). If that is the case you would need to use reflection to set the field value instead of using the getter and add.
    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

  8. #8
    Join Date
    May 2011
    Posts
    7

    Default

    Thanks Guys for the response but unfortunately if I perform all these changes I don't have the behavior bean persisting the changes as this behavior bean finally needs to be marshalled as xml. I now understand that there is no easy way to do this unless I use Aspect4j.Thanks for pouring in your responses.

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

    Default

    If the class/bean you are mentioning isn't spring managed (but instantiated by new or reflection) the changes as proposed are useless but that wasn't part of your initial question.
    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

  10. #10
    Join Date
    May 2011
    Posts
    7

    Default

    Hi Marten,

    The solutions that were proposed by you, Patalakanti and Vishnu are perfectly valid but I have a scenario where I need to to create a factory implementation 230 times for various objects I get from the complex object factory.Hence I choose not to go in that direction as this would be a maintainence nightmare. But I respect your thoughts and solution that you guys proposed.Thanks for your time.

Posting Permissions

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