Announcement Announcement Module
Collapse
No announcement yet.
AUTOWIRE_BY_NAME not wiring when bean type == setter type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AUTOWIRE_BY_NAME not wiring when bean type == setter type

    I'm using AbstractDependencyInjectionSpringContextTests, and it's not injecting by name when type is also equal. But it does work when I change setter type to Object. I've tried with 2.0.7 and 2.5.1. Any ideas?


    testFoo fails.
    testBar succeeds.

    Only difference being setFoo(String foo) vs setBar(Object bar)


    SpringInjectionTest:
    Code:
    public class SpringInjectionTest extends AbstractDependencyInjectionSpringContextTests {
    	
    	public SpringInjectionTest() {
    		super();
    		setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME);
    	}
    	
    	protected String[] getConfigLocations() {
    		return new String[] {"classpath:SpringInjectionTest-appContext.xml"};
    	}
    	
    	public void testFoo() {
    		assertEquals("foo", foo);
    	}
    	
    	public void testBar() {
    		assertEquals("bar", bar);
    	}
    	
    	private String foo;
    	public void setFoo(String foo) {
    		this.foo = foo;
    	}
    	
    	private String bar;
    	public void setBar(Object bar) {
    		this.bar = (String) bar;
    	}
    }
    SpringInjectionTest-appContext.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="foo" class="java.lang.String">
    		<constructor-arg value="foo"/>
    	</bean>
    	<bean id="bar" class="java.lang.String">
    		<constructor-arg value="bar"/>
    	</bean>
    </beans>

  • #2
    Only complex objects can be autowired, not primitives (why I don't know). Next to the basic primitives (int, long etc.) String and Class are also handled as primitives. So those are filtered out when autowiring.

    Comment

    Working...
    X