Announcement Announcement Module
Collapse
No announcement yet.
util:list name space and @autowired strange results IS IT A BUG ???? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Thanks for reply.

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

      Comment


      • #4
        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;

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment

            Working...
            X