Skip to content

Add fallback option default false to @Qualifier [SPR-13623] #18201

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

Closed
spring-projects-issues opened this issue Oct 30, 2015 · 11 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

Yanming Zhou opened SPR-13623 and commented

It's identical to current implementation If fallback=false, so it's backward compatible.
fallback=true is useful for override base platform in particular app.

@Component
public class DefaultEchoService implements EchoService {
}
@Qualifier(value = "customEchoService", fallback = true)
@Autowired
private EchoService echoService;

Spring will inject DefaultEchoService as echoService, If we have another EchoService named "customEchoService", spring should inject it instead.

@Component("customEchoService")
public class CustomEchoService implements EchoService {
}

No further details from SPR-13623

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

Just to make sure I understand this properly. You'd like that when fallback is true and there is no a candidate bean named customEchoService it should inject what exactly? Should it be named default + beanType.getSimpleName()?.

That looks a bit weird to me and it changes the semantic of Qualifier quite significantly.

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

If fallback is true and there is no candidate named "customEchoService", It should act as no @Qualifier, It will inject "defaultEchoService" with this case but regardless of the name.

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

It's still not clear, sorry. It will inject "defaultEchoService" because that's the only bean of type EchoseService in the context?

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

@Qualifier(value = "customEchoService", fallback = true)
@Autowired
private EchoService echoService;

is equals to

@Autowired
private EchoService echoService;

if there is no "customEchoService"

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

your "equals to" makes no sense without more context. What if I have two EchoService beans in the context?

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

"equals to" means exactly identical. just ignore @Qualifier and keep the spring rule.
It will inject primary candidate or the only candidate.
It will inject nothing if there is no candidate and required=false.
It will throws exception if there is no candidate or more than one candidate and no a primary.

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

why don't you start with that in your original description rather than having a defaultEchoService that insinuates that default is some magic prefix? Anyway, your request is clear now.

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

Should it be resolved in 4.3 ?

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

We just discussed this issue and we're not keen on implementing this. This significantly complexify the semantic of @Qualifier and your "fallback" may mean something different to someone else. If you need to have such fine tune autowiring arrangement, we advise you to inject the list of candidates and figure out which one you have to use.

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

Inject the list or map will contaminate clean code. How about add a dedicated annotation? Such like @FallbackQualifier or @AlternativeQualifier. It's useful for certain scene, If spring add such feature, Developers will realize its value.

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

Workaround :

	@Autowired(required = false)
	@Qualifier("cacheStringRedisTemplate")
	private RedisTemplate<String, String> cacheStringRedisTemplate;

	@Autowired
	@Qualifier("stringRedisTemplate")
	private RedisTemplate<String, String> stringRedisTemplate;


	@PostConstruct
	public void afterPropertiesSet() {
		if (cacheStringRedisTemplate != null)
			stringRedisTemplate = cacheStringRedisTemplate;
	}

Wish:

@Autowired
@Qualifier(value = "cacheStringRedisTemplate", fallback = "stringRedisTemplate")
private RedisTemplate<String, String> stringRedisTemplate;
@Autowired
@Qualifier("cacheStringRedisTemplate")
@FallbackQualifier("stringRedisTemplate")
private RedisTemplate<String, String> stringRedisTemplate;
@Autowired
@CandidateQualifier("cacheStringRedisTemplate")
@Qualifier("stringRedisTemplate")
private RedisTemplate<String, String> stringRedisTemplate;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants