Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field autowiring in TestContext framework ignores explicit @Qualifier in favor of qualification by field name [SPR-6128] #10796

Closed
spring-projects-issues opened this issue Sep 18, 2009 · 3 comments
Assignees
Labels

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Sep 18, 2009

Chris Beams opened SPR-6128 and commented

repro:

AutowiredQualifierTests-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="foo" class="java.lang.String">
		<constructor-arg value="normal" />
	</bean>

	<bean id="customFoo" class="java.lang.String">
		<constructor-arg value="custom" />
	</bean>

</beans>

AutowiredQualifierTests.java

package org.springframework.test.context.junit4.spr6128;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

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;

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class AutowiredQualifierTests {

	@Autowired
	private String foo;

	@Autowired
	@Qualifier("customFoo")
	private String customFoo;


	@Test
	public void test() {
		assertThat(foo, equalTo("normal"));
		assertThat(customFoo, equalTo("custom"));
	}
}

Expected: explicitly using @Qualifier should override any qualification by field name (just as, for example, @RequestParam("pName") does with relationship to the method parameter name.

Actual: if there is a bean that matches the name of the field, autowire-by-field-name takes precedence over any explicit @Qualifier.

This bug only manifests in a very particular set of conditions, but when it does it is deeply unintuitive.


Affects: 3.0 M4

Issue Links:

  • #10733 Autowiring doesn't fail in case of ambiguity with certain combinations of Java config and component scanning

Referenced from: commits 02dfe94

1 votes, 2 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2009

Sam Brannen commented

I verified that claims raised in this issue no longer apply.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2009

Sam Brannen commented

See Juergen's comments in #10733, as they may be related to the undesired effect you were seeing. In any case, the bug you encountered is no longer reproducible.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2009

Sam Brannen commented

Replaced FooTests with AutowiredQualifierTests which are checked into subversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants