Introduce defaultCandidate flag (for plain type vs. qualified match)

Closes gh-26528
This commit is contained in:
Juergen Hoeller
2024-02-20 12:00:47 +01:00
parent bc2257aaff
commit a8fb16b47c
5 changed files with 72 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -239,13 +239,27 @@ public @interface Bean {
String[] name() default {};
/**
* Is this bean a candidate for getting autowired into some other bean?
* Is this bean a candidate for getting autowired into some other bean at all?
* <p>Default is {@code true}; set this to {@code false} for internal delegates
* that are not meant to get in the way of beans of the same type in other places.
* @since 5.1
* @see #defaultCandidate()
*/
boolean autowireCandidate() default true;
/**
* Is this bean a candidate for getting autowired into some other bean based on
* the plain type, without any further indications such as a qualifier match?
* <p>Default is {@code true}; set this to {@code false} for restricted delegates
* that are supposed to be injectable in certain areas but are not meant to get
* in the way of beans of the same type in other places.
* <p>This is a variation of {@link #autowireCandidate()} which does not disable
* injection in general, just enforces an additional indication such as a qualifier.
* @since 6.2
* @see #autowireCandidate()
*/
boolean defaultCandidate() default true;
/**
* The optional name of a method to call on the bean instance during initialization.
* Not commonly used, given that the method may be called programmatically directly

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -241,6 +241,11 @@ class ConfigurationClassBeanDefinitionReader {
beanDef.setAutowireCandidate(false);
}
boolean defaultCandidate = bean.getBoolean("defaultCandidate");
if (!defaultCandidate) {
beanDef.setDefaultCandidate(false);
}
String initMethodName = bean.getString("initMethod");
if (StringUtils.hasText(initMethodName)) {
beanDef.setInitMethodName(initMethodName);