Results 1 to 2 of 2

Thread: using @Required and @Autowired and @Qualifier

  1. #1
    Join Date
    Aug 2010
    Posts
    11

    Default using @Required and @Autowired and @Qualifier

    Can we use @Required and @Autowired and @Qualifier on the same setter method.

    When I try to use in the below mentioned way I have getting compile time error saying "The annotation @Qualifier is disallowed for this location"

    Code:
    public class Customer
    {
        private Person person;
        private int type;
        private String action;
    
        @Qualifier("PersonBean1")
        @Autowired()
        @Required()
        public void setPerson(Person person)
        {
    	this.person = person;
        }
    
    //setter and getters for type,action
    }
    My configuration file is
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context 
    		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<context:annotation-config/>
    
    	<bean id="CustomerBean" class="Customer">
    		<property name="action" value="buy" />
    		<property name="type" value="1" />
    	</bean>
    
    	<bean id="PersonBean" class="Person">
    		<property name="name" value="mkyong" />
    		<property name="address" value="address 123" />
    		<property name="age" value="28" />
    	</bean>
    
    	<bean id="PersonBean1" class="Person">
    		<property name="name" value="mkyong" />
    		<property name="address" value="address 123" />
    		<property name="age" value="28" />
    	</bean>
    </beans>
    Could anyone explain why this cannot be done ?

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

    Default

    Because @Qualifier is only allowed on fields and attributes not methods (you need to put it on the method attribute). This is also explained in the reference guide (which I suggest as a must read).
    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
  •