bean标签的父属性是否等同于Java中的继承?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19223888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:12:52  来源:igfitidea点击:

Does the parent attribute of bean tag is equivalent to inheritance in Java?

javaspring

提问by Bhargav Kumar R

I started studying Spring documentation. I came across parentattribute explanation, does using parentattribute between two beans is equivalent to inheritance relationship between these classes?

我开始学习 Spring 文档。碰到parent属性解释,parent两个bean之间使用属性是否就相当于这些类之间的继承关系?

If so, how to perform method overriding? Also I saw in some context, use of both parentattribute in XML config file and extendskeyword in bean class. Is it required to specify both springs in order to implement inheritance?

如果是这样,如何执行方法覆盖?我还在某些上下文中看到,parent在 XML 配置文件中使用了属性,extends在 bean 类中使用了关键字。是否需要指定两个弹簧才能实现继承?

采纳答案by Santosh

  • In spring, the parentin bean configuration signifies configuration inheritanceand not related to Java inheritance.
  • The configuration inheritancesaves a lot of code as you do away with repeated XML code.
  • 在 spring 中,parentin bean 配置表示configuration inheritance与 Java 继承无关。
  • 由于configuration inheritance消除了重复的 XML 代码,因此可以节省大量代码。

For example, you have following bean with attributes

例如,您有以下带有属性的 bean

Class MyBean {
    attrib1
    attrib2
    attrib3
    attrib4
} 

Say one instance of bean say bean1just needs attrib1and attrib2whereas another say bean2instance needs all four the attributes.

说一个 bean 实例说bean1只需要attrib1attrib2而另一个说bean2实例需要所有四个属性。

Lets configure these two beans

让我们配置这两个 bean

<bean id="bean1" class="MyBean">
    <property name="attrib1" value="val1" />
    <property name="attrib2" value="val2" />
</bean>

<bean id="bean2" parent="bean1">
    <property name="attrib3" value="val3" />
    <property name="attrib4" value="val4" />
</bean>

Note that bean2just needed to configure attrib3and attrib4. The other two attributes are inheritedfrom bean1

请注意,bean2只需要配置attrib3attrib4. 另外两个属性继承bean1

To answer your question:

回答你的问题:

Does it is required to specify both springs in order to implement inheritance?

是否需要指定两个弹簧才能实现继承?

No. As mentioned earlier this is not the same as java inheritance.

不。如前所述,这与 java 继承不同。

回答by M. Deinum

No... They aren't related to each other.

不……他们彼此没有关系。

A parent bean definition can be considered a form of template for other beans (which don't have to have any relationship). Imagine you have 3 beans which all share/need a username/password/url configuration you can then create a parent bean which defines all these properties. This reduces your configuration effort (you don't have to duplicate the properties in all the bean definitions).

可以将父 bean 定义视为其他 bean(不必具有任何关系)的模板形式。假设您有 3 个 bean,它们都共享/需要用户名/密码/url 配置,然后您可以创建一个定义所有这些属性的父 bean。这减少了您的配置工作(您不必在所有 bean 定义中复制属性)。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-child-bean-definitionsgives a nice overview.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-child-bean-definitions给出了一个很好的概述。