此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

使用限定符微调基于注释的自动布线

@Primary@Fallback是按类型使用自动布线的有效方法,具有多个可以确定一个主要(或非回备)候选者的情况。spring-doc.cadn.net.cn

当您需要对选择过程进行更多控制时,您可以使用 Spring 的@Qualifier注解。 您可以将限定符值与特定参数相关联,从而缩小类型匹配的集合,以便为每个参数选择特定的 bean。在最简单的情况下,这可以是一个简单的描述性值,如以下示例所示:spring-doc.cadn.net.cn

public class MovieRecommender {

	@Autowired
	@Qualifier("main")
	private MovieCatalog movieCatalog;

	// ...
}
class MovieRecommender {

	@Autowired
	@Qualifier("main")
	private lateinit var movieCatalog: MovieCatalog

	// ...
}

您还可以指定@Qualifier注释单个构造函数参数或method 参数,如以下示例所示:spring-doc.cadn.net.cn

public class MovieRecommender {

	private final MovieCatalog movieCatalog;

	private final CustomerPreferenceDao customerPreferenceDao;

	@Autowired
	public void prepare(@Qualifier("main") MovieCatalog movieCatalog,
			CustomerPreferenceDao customerPreferenceDao) {
		this.movieCatalog = movieCatalog;
		this.customerPreferenceDao = customerPreferenceDao;
	}

	// ...
}
class MovieRecommender {

	private lateinit var movieCatalog: MovieCatalog

	private lateinit var customerPreferenceDao: CustomerPreferenceDao

	@Autowired
	fun prepare(@Qualifier("main") movieCatalog: MovieCatalog,
				customerPreferenceDao: CustomerPreferenceDao) {
		this.movieCatalog = movieCatalog
		this.customerPreferenceDao = customerPreferenceDao
	}

	// ...
}

以下示例显示了相应的 bean 定义。spring-doc.cadn.net.cn

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config/>

	<bean class="example.SimpleMovieCatalog">
		<qualifier value="main"/> (1)

		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean class="example.SimpleMovieCatalog">
		<qualifier value="action"/> (2)

		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>
1 带有main限定符值与构造函数参数连接,该参数使用相同的值进行限定。
2 带有action限定符值与构造函数参数连接,该参数使用相同的值进行限定。

对于回退匹配,bean 名称被视为默认限定符值。因此,您可以使用idmain而不是嵌套限定符元素,导致获得相同的匹配结果。但是,尽管您可以使用此约定来引用特定 bean 的名称,@Autowired从根本上讲,是关于类型驱动的注入,具有可选的语义限定符。这意味着限定符值,即使具有 bean 名称fallback,在类型匹配集中始终具有缩小的语义。他们没有在语义上表达对唯一 bean 的引用id. 好的限定符值是mainEMEApersistent,表示特定组件的特征,这些特征是独立于豆子id,如果是匿名 bean定义,例如前面示例中的定义。spring-doc.cadn.net.cn

限定符也适用于类型化集合,如前所述,例如Set<MovieCatalog>. 在这种情况下,所有匹配的 bean,根据声明的限定符,都作为集合注入。这意味着限定符不必是 独特。 相反,它们构成筛选条件。例如,您可以定义 倍数MovieCatalog具有相同限定符值“action”的 bean,所有这些都注入到Set<MovieCatalog>注释为@Qualifier("action").spring-doc.cadn.net.cn

让限定符值根据目标 Bean 名称进行选择,在类型匹配candidates 中,不需要@Qualifier注解。如果没有其他解析指示器(例如限定符或主标记),对于非唯一依赖关系情况,Spring 将注入点名称(即字段名称或参数名称)与目标 Bean 名称进行匹配,并选择同名候选者(如果有)(通过 Bean 名称或关联的别名)。spring-doc.cadn.net.cn

从 6.1 版本开始,这需要-parametersJava 编译器标志。从 6.2 开始,容器对 bean 名称匹配应用快速快捷方式解析,当参数名称与bean name 匹配时绕过完整类型匹配算法,并且没有类型、限定符或主要条件覆盖匹配。 是的 因此,建议您的参数名称与目标 Bean 名称匹配。spring-doc.cadn.net.cn

作为按名称注入的替代方案,请考虑 JSR-250@Resource注解 在语义上定义为通过其唯一名称标识特定目标组件, 声明的类型与匹配过程无关。@Autowired宁愿 不同的语义:按类型选择候选 Bean 后,指定的String限定符值仅在这些类型选择的候选项中考虑(例如, 匹配account限定符与标有相同限定符标签的 bean)。spring-doc.cadn.net.cn

对于本身被定义为集合的 bean,Map,或数组类型,@Resource是一个很好的解决方案,通过唯一名称引用特定的集合或数组 bean。 也就是说,您可以匹配集合,Map,以及数组类型通过 Spring 的@Autowired类型匹配算法,只要元素类型信息 保存在@Bean返回类型签名或集合继承层次结构。 在这种情况下,您可以使用限定符值在相同类型的集合中进行选择, 如上一段所述。spring-doc.cadn.net.cn

@Autowired还考虑注入的自引用(即,引用回 当前注入的 bean)。有关详细信息,请参阅自注入spring-doc.cadn.net.cn

@Autowired适用于字段、构造函数和多参数方法,允许 在参数级别通过限定符注释缩小范围。相比之下,@Resource仅支持具有单个参数的字段和 bean 属性 setter 方法。因此,如果您的注入目标是构造函数或多参数方法,则应坚持使用限定符。spring-doc.cadn.net.cn

您可以创建自己的自定义限定符注释。为此,请定义注释并提供@Qualifier注释,如以下示例所示:spring-doc.cadn.net.cn

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {

	String value();
}
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class Genre(val value: String)

然后,您可以在自动连接的字段和参数上提供自定义限定符,如以下示例所示:spring-doc.cadn.net.cn

public class MovieRecommender {

	@Autowired
	@Genre("Action")
	private MovieCatalog actionCatalog;

	private MovieCatalog comedyCatalog;

	@Autowired
	public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
		this.comedyCatalog = comedyCatalog;
	}

	// ...
}
class MovieRecommender {

	@Autowired
	@Genre("Action")
	private lateinit var actionCatalog: MovieCatalog

	private lateinit var comedyCatalog: MovieCatalog

	@Autowired
	fun setComedyCatalog(@Genre("Comedy") comedyCatalog: MovieCatalog) {
		this.comedyCatalog = comedyCatalog
	}

	// ...
}

接下来,您可以提供候选 Bean 定义的信息。您可以添加<qualifier/>标签作为<bean/>标记,然后指定typevalue以匹配您的自定义限定符注释。该类型与注释的完全限定的类名匹配。或者,为了方便起见,如果不存在冲突名称的风险,您可以使用短类名。以下示例演示了这两种方法:spring-doc.cadn.net.cn

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config/>

	<bean class="example.SimpleMovieCatalog">
		<qualifier type="Genre" value="Action"/>
		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean class="example.SimpleMovieCatalog">
		<qualifier type="example.Genre" value="Comedy"/>
		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>

类路径扫描和托管组件中,您可以看到基于注释的替代方法在 XML 中提供限定符元数据。具体而言,请参阅提供带有注释的限定符元数据spring-doc.cadn.net.cn

在某些情况下,使用不带值的注释可能就足够了。这可以是 当注释具有更通用的用途并且可以应用于 几种不同类型的依赖项。例如,您可以提供离线 目录,当没有可用的 Internet 连接时可以搜索。首先,定义 简单注解,如以下示例所示:spring-doc.cadn.net.cn

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Offline {
}
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class Offline

然后将注释添加到要自动连接的字段或属性中,如 以下示例:spring-doc.cadn.net.cn

public class MovieRecommender {

	@Autowired
	@Offline (1)
	private MovieCatalog offlineCatalog;

	// ...
}
1 这一行将@Offline注解。
class MovieRecommender {

	@Autowired
	@Offline (1)
	private lateinit var offlineCatalog: MovieCatalog

	// ...
}
1 这一行将@Offline注解。

现在 bean 定义只需要一个限定符type,如以下示例所示:spring-doc.cadn.net.cn

<bean class="example.SimpleMovieCatalog">
	<qualifier type="Offline"/> (1)
	<!-- inject any dependencies required by this bean -->
</bean>
1 此元素指定限定符。

您还可以定义自定义限定符注释,这些注释接受 添加或代替简单的value属性。如果多个属性值 然后在要自动连接的字段或参数上指定,Bean 定义必须匹配 所有这些属性值都被视为自动配线候选值。例如, 请考虑以下注释定义:spring-doc.cadn.net.cn

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MovieQualifier {

	String genre();

	Format format();
}
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class MovieQualifier(val genre: String, val format: Format)

在这种情况下Format是一个枚举,定义如下:spring-doc.cadn.net.cn

public enum Format {
	VHS, DVD, BLURAY
}
enum class Format {
	VHS, DVD, BLURAY
}

要自动连接的字段使用自定义限定符进行注释,并包含值 对于这两个属性:genreformat,如以下示例所示:spring-doc.cadn.net.cn

public class MovieRecommender {

	@Autowired
	@MovieQualifier(format=Format.VHS, genre="Action")
	private MovieCatalog actionVhsCatalog;

	@Autowired
	@MovieQualifier(format=Format.VHS, genre="Comedy")
	private MovieCatalog comedyVhsCatalog;

	@Autowired
	@MovieQualifier(format=Format.DVD, genre="Action")
	private MovieCatalog actionDvdCatalog;

	@Autowired
	@MovieQualifier(format=Format.BLURAY, genre="Comedy")
	private MovieCatalog comedyBluRayCatalog;

	// ...
}
class MovieRecommender {

	@Autowired
	@MovieQualifier(format = Format.VHS, genre = "Action")
	private lateinit var actionVhsCatalog: MovieCatalog

	@Autowired
	@MovieQualifier(format = Format.VHS, genre = "Comedy")
	private lateinit var comedyVhsCatalog: MovieCatalog

	@Autowired
	@MovieQualifier(format = Format.DVD, genre = "Action")
	private lateinit var actionDvdCatalog: MovieCatalog

	@Autowired
	@MovieQualifier(format = Format.BLURAY, genre = "Comedy")
	private lateinit var comedyBluRayCatalog: MovieCatalog

	// ...
}

最后,bean 定义应包含匹配的限定符值。这个例子 还演示了您可以使用 bean 元属性而不是<qualifier/>元素。如果可用,则<qualifier/>元素及其属性 优先级,但自动布线机制会回退到<meta/>标记,如果不存在此类限定符,则在 以下示例:spring-doc.cadn.net.cn

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config/>

	<bean class="example.SimpleMovieCatalog">
		<qualifier type="MovieQualifier">
			<attribute key="format" value="VHS"/>
			<attribute key="genre" value="Action"/>
		</qualifier>
		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean class="example.SimpleMovieCatalog">
		<qualifier type="MovieQualifier">
			<attribute key="format" value="VHS"/>
			<attribute key="genre" value="Comedy"/>
		</qualifier>
		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean class="example.SimpleMovieCatalog">
		<meta key="format" value="DVD"/>
		<meta key="genre" value="Action"/>
		<!-- inject any dependencies required by this bean -->
	</bean>

	<bean class="example.SimpleMovieCatalog">
		<meta key="format" value="BLURAY"/>
		<meta key="genre" value="Comedy"/>
		<!-- inject any dependencies required by this bean -->
	</bean>

</beans>