Announcement Announcement Module
Collapse
No announcement yet.
Bug or new feature on Configuration, with @Bean and @Qualifier Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bug or new feature on Configuration, with @Bean and @Qualifier

    Hi everybody, i have writed this simple test case:

    Code:
    package test;
    
    import javax.annotation.Resource;
    
    import junit.framework.Assert;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "file:src/test/resources/test.xml")
    public class BeanInWhichIInject {
    
    	@Resource(name = "bean1")
    	TestBeanI bean1;
    
    	@Autowired
    	@Qualifier(value = "bean2")
    	TestBeanI bean2;
    
    	@Test
    	public void testValue1() {
    		Assert.assertEquals(1, bean1.getValue().intValue());
    	}
    
    	@Test
    	public void testValue2() {
    		Assert.assertEquals(2, bean2.getValue().intValue());
    	}
    
    }
    Code:
    package test;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    @Configuration
    public class Config {
    
    	@Bean
    	@Primary
    	@Qualifier(value = "bean2")
    	public TestBeanI createBean2() {
    		System.out.print("test bean2");
    		return new TestBean1(2);
    	}
    
    }
    Code:
    package test;
    
    public interface TestBeanI{
    
    	
    	Integer getValue();
    }
    Code:
    package test;
    
    import org.springframework.stereotype.Service;
    
    @Service(value = "bean1")
    public class TestBean1 implements TestBeanI {
    
    	public TestBean1() {
    		i = new Integer(1);
    	}
    
    	public TestBean1(final Integer i) {
    		this.i = i;
    	}
    
    	Integer i;
    
    	@Override
    	public Integer getValue() {
    		return i;
    	}
    
    }
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <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"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
    
    	
    	<context:component-scan base-package="test" />
    
    </beans>
    The @Qualifier annotation, in red, is ignored and the bean has the name of the method.

    In the method in red is renamed in "bean2" all work.

    Why is ignored? Is a bug or a "missed feature"?

    I use spring 3.1.

    Thanks

  • #2
    @Qualifier is not the way to name a bean. Instead you should use the name attribute on @Bean annotation. See the reference documentation [1].

    So for you it should be.
    Code:
    package test;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    @Configuration
    public class Config {
    
    	@Bean(name = "bean2")
    	@Primary
    	public TestBeanI createBean2() {
    		System.out.print("test bean2");
    		return new TestBean1(2);
    	}
    
    }
    @Qualifier is used to specify which bean should be injected when using the @Autowired annotation.
    Just as you do in your implementation of the class BeanInWhichIInject.
    Code:
    	@Autowired
    	@Qualifier(value = "bean2")
    	TestBeanI bean2;
    Without @Qualifier Spring would not know if it should inject bean1 or bean2 here.

    [1] http://static.springsource.org/sprin...ng-bean-naming

    Best regards
    Pontus Ullgren
    Last edited by ullgren; Aug 31st, 2012, 10:11 AM.

    Comment


    • #3
      Thanks, I have made two "errors" in same time: wrong use of qualifier and wrong use of Bean annotation.

      To solve I have used attribute name on Bean annotation with @Resource annotation on property to inject on.

      Comment

      Working...
      X