Announcement Announcement Module
Collapse
No announcement yet.
using @Required and @Autowired and @Qualifier Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment

    Working...
    X