在 Spring XML 中将一个 bean 声明为主要 bean

Declare a bean as primary in Spring XML

我正尝试在我的 XML 的 bean 标签中使用主 属性,如下所示:

<bean id = "gslFeatures" class="aero.sita.pts.bcs.common.model.features.GSLFeatures" primary = "true">
    </bean>

但是当我尝试 运行 我的应用程序时出现以下错误:

cvc-complex-type.3.2.2: 属性 'primary' 不允许出现在元素 'bean'.

现在这个错误很容易解释了,但是我该如何解决这个问题并确保这个 bean 在自动装配时被认为是主要的。

在您的 xml 文件中使用 2.0 以上的模式版本(xsd 版本)。您可以在以下 link https://www.springframework.org/schema/beans/ .

中找到架构版本

但是,最好不要指定版本。在这种情况下,将选择最新版本。

示例:

<?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.xsd">

    <bean id="electricMotor1" class="com.chiranth.ElectricMotor1" primary="true"/>
    <bean id="electricMotor2" class="com.chiranth.ElectricMotor2"/>

    <bean id="modelX" class="com.chiranth.TeslaModelX" autowire="constructor"/>
</beans>