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
Does the parent attribute of bean tag is equivalent to inheritance in Java?
提问by Bhargav Kumar R
I started studying Spring documentation. I came across parent
attribute explanation, does using parent
attribute 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 parent
attribute in XML config file and extends
keyword in bean class. Is it required to specify both springs in order to implement inheritance?
如果是这样,如何执行方法覆盖?我还在某些上下文中看到,parent
在 XML 配置文件中使用了属性,extends
在 bean 类中使用了关键字。是否需要指定两个弹簧才能实现继承?
采纳答案by Santosh
- In spring, the
parent
in bean configuration signifiesconfiguration inheritance
and not related to Java inheritance. - The
configuration inheritance
saves a lot of code as you do away with repeated XML code.
- 在 spring 中,
parent
in 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 bean1
just needs attrib1
and attrib2
whereas another say bean2
instance needs all four the attributes.
说一个 bean 实例说bean1
只需要attrib1
,attrib2
而另一个说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 bean2
just needed to configure attrib3
and attrib4
. The other two attributes are inheritedfrom bean1
请注意,bean2
只需要配置attrib3
和attrib4
. 另外两个属性继承自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.