当前位置: 首页 > 面试题库 >

在spring如何为带有父级的xml中定义的bean选择构造函数?

栾鸣
2023-03-14
问题内容

我正在使用Spring 3.1.0。我试图了解spring读取其xml文件的方式。我试图了解spring如何处理bean定义中的歧义条件。

例如

我有 Alarm.java

package com.anshbansal.alarm2;

public class Alarm {

    private String type;
    private int volume;
    private int hour;

    public Alarm() {
    }

    public Alarm(String type, int volume, int hour) {
        this.type = type;
        this.volume = volume;
        this.hour = hour;
    }

    public Alarm(String type, int volume) {
        this.type = type;
        this.volume = volume;
    }

    public Alarm(int volume, String type) {
        this.type = type;
        this.volume = volume;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setVolume(int volume) {
        this.volume = volume;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    @Override
    public String toString() {
        return "Alarm{" +
                "type='" + type + '\'' +
                ", volume=" + volume +
                ", hour=" + hour +
                '}';
    }
}

我的spring xml文件alarm2.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.xsd">

       <bean id="propertyRandom" abstract="true">
              <constructor-arg value="23" type="int"/>
       </bean>


       <bean id="alarm1" class="com.anshbansal.alarm2.Alarm" parent="propertyRandom">
              <constructor-arg value="10" type="int"/>
              <constructor-arg value="Ringing" />
       </bean>


</beans>

存在歧义,因为尚不清楚哪些int会大量出现,哪些会花费数小时。如果我打印得到以下内容

Alarm{type='Ringing', volume=23, hour=10}

那么spring如何读取xml文件来解析要调用的构造函数?首先是父母,然后是豆?它记录在某处吗?

我知道可以使用诸如指定indexname作为属性之类的方法,但我也应该了解如何处理这种模棱两可的情况。这就是为什么我要问这个。


问题答案:

从Spring 文档中,

构造函数参数解析匹配使用参数的类型进行。如果Bean定义的构造函数参数中不存在潜在的歧义,则在实例化Bean时,在Bean定义中定义构造函数参数的顺序就是将这些参数提供给适当的构造函数的顺序。

我找到以下答案,该答案解释了选择构造函数时的弹簧行为。

如果指定不带索引的构造函数-
arg,则将调用可以满足给定参数的最贪婪的构造函数(按类型匹配参数)。对于java.io.File,这是File(String
parent,String child)构造函数:您的String参数按类型匹配两者,因此算法使用该构造函数。

参考

从父级继承时,构造函数参数将合并(与合并属性集合相同)。在您的情况下,合并子bean构造函数参数后,将

<constructor-arg value="23" type="int"/>
<constructor-arg value="10" type="int"/>
<constructor-arg value="Ringing" />

对于模棱两可的方案,请使用索引或构造函数参数名称。



 类似资料:
  • 我读了很多关于如何在spring boot项目中使用spring-boot-starter-parent的帖子。基本上,我读过一些帖子(Spring文档也讨论了这一点),这些帖子描述了两种实现这一点的方法 直接使用spring-boot-starter-parent作为项目父级。它给我们带来了依赖管理和插件管理的好处。 另一种方法是在项目pom中导入spring-boot-starter父级(如果

  • 问题内容: 有没有一种方法可以用XML编写Spring bean,以便在构造函数具有varargs参数类型时使用构造函数注入?IE,有没有一种方法可以指定数组,也可以指定列表? 例如: 问题答案: 因为是您可以使用的数组:

  • 问题内容: 我想在 Spring Java配置中 创建一个Spring bean,并在运行时传递一些构造函数参数。我创建了以下Java配置,其中有一个bean fixedLengthReport ,它在构造函数中需要一些参数。 但是我收到错误消息, 表明 未找到bean 导致 sourceSystem 无法连接。如何使用运行时构造函数参数创建bean? 我正在使用Spring 4.2 问题答案:

  • 第一次必须工作,但第二次出现错误: 没有类型为“java.lang.String”的合格bean可用:应至少有1个bean符合autowire候选。依赖项批注:{} 为什么? application.properties 我还发现,如果用@Component替换@Configuration或添加(代理BeanMethods = false),问题就会消失。 源代码

  • 问题内容: 在春季,bean的类可能没有公共构造函数,而只有私有构造函数吗?创建bean时会调用此私有构造函数吗?谢谢。 问题答案: 是的,Spring可以调用私有构造函数。如果找到具有正确参数的构造函数,无论可见性如何,它将使用反射将其构造函数设置为可访问的。

  • 我想在SpringJava配置中创建一个Springbean,并在运行时传递一些构造函数参数。我创建了以下Java配置,其中有一个beanfixedLengthReport,它需要构造函数中的一些参数。 但是我得到一个错误,sourceSystem无法连接,因为没有找到bean。如何用运行时构造函数参数创建bean? 我正在使用Spring 4.2