Results 1 to 6 of 6

Thread: util:list name space and @autowired strange results IS IT A BUG ????

  1. #1
    Join Date
    Dec 2009
    Location
    Pune,India
    Posts
    60

    Default util:list name space and @autowired strange results IS IT A BUG ????

    I have rather different experience.

    here is an example
    I have person class in my configuration file I have say following bean definitions.

    Code:
     <bean id="person1" class="com.test.Person"/>
    
       <bean id="personA" class="za.co.discovery.util.Person"/>
       	   
       <bean id="personB" class="za.co.discovery.util.Person"/>
    	 
    
             <util:list id="persons">
    		<ref bean="personA" />
    		<ref bean="personB" />
    	</util:list>
    in my code if I put @Autowired

    Code:
    @Autowired
    	List<Person> persons;
    All I get in the list personA , personB and surprisingly person1 . with autowired list will give me all the beans person declared inside application context. My List size showing me 3 instead of 2

    if I declare one more list


    Code:
    <bean id="person1" class="com.test.Person"/>
    
       <bean id="personA" class="za.co.discovery.util.Person"/>
       	   
       <bean id="personB" class="za.co.discovery.util.Person"/>
    
    <bean id="personC" class="za.co.discovery.util.Person"/>
    
    
    <bean id="personD" class="za.co.discovery.util.Person"/>
    
    
    <util:list id="persons">
    		<ref bean="personA" />
    		<ref bean="personB" />
    	</util:list>
    
    <util:list id="persons2">
    		<ref bean="personC" />
    		<ref bean="personD" />
    	</util:list>
    
    @Autowired
    	List<Person> persons;
    
    @Autowired
    	List<Person> persons2;
    both list showing me size 5 that means all the person's beans inside xml i.e. 5 instead of 2.

    but if I do context.getBean("persons") give me size 2 and context.getBean("persons2")
    give me size 2 which is proper behavior

    is it bug ????

    I have tested both spring 2.5.6 and spring 3.1.0.M1 same result.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    No it is not a bug, it is intended behavior (which is also explained in the reference guide). @Autowired on a collection will give you all instances of the declared type, if you want to have a bean/list from the context use @Qualifier("<beanname>") in addition to @Autowired.
    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
    Dec 2009
    Location
    Pune,India
    Posts
    60

    Default

    Thanks for reply.

    autowire by name for util:list does work with @Resource as well.
    But it is available in java 6 only.

  4. #4
    Join Date
    Dec 2009
    Location
    Pune,India
    Posts
    60

    Post just for info

    with java 6

    @Resource
    private List<Person> persons;

    this will autowired by name . you do not reuired autowire annotation.


    for java 5 you can take help of spring framework to achieve similar but not the same.

    Code:
    <bean id="personA" class="za.co.discovery.util.Person"> <qualifier value="p1"></qualifier> <property name="name" value="RajeshA" /> </bean> <bean id="personB" class="za.co.discovery.util.Person"> <qualifier value="p1"></qualifier> <property name="name" value="RajeshB" /> </bean> <bean id="personC" class="za.co.discovery.util.Person"> <qualifier value="p2"></qualifier> <property name="name" value="" /> </bean> <bean id="personD" class="za.co.discovery.util.Person"> <qualifier value="p2"></qualifier> <property name="name" value="" /> </bean> <bean id="personE" class="za.co.discovery.util.Person"> <qualifier value="p2"></qualifier> <property name="name" value="" /> </bean> <util:list id="persons"> <ref bean="personA" /> <ref bean="personB" /> </util:list> <util:list id="persons1"> <ref bean="personC" /> <ref bean="personD" /> <ref bean="personE" /> </util:list>


    Code:
      @Autowired
    	@Qualifier("p2")
    	private List<Person> persons;
    persons list autoried with 3 objects person c ,d and e


    Code:
      @Autowired
    	@Qualifier("p1")
    	private List<Person> persons;

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Yes you can... @Qualifier fallsback to the beanname when no qualifier is given for beans... So yes you can achieve exactly the same with spring annotations. Next to that you can also use the @Resource annotation on java 5, you simply have to add a simple jar containing the annotations.
    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

  6. #6
    Join Date
    Dec 2009
    Location
    Pune,India
    Posts
    60

    Default

    Yes but most of the time we have restrictions while adding jar. Those who can not add new jars for some reason ( due to client restriction or not autorize to do so) then we have to use Spring annotations only.

Posting Permissions

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