Spring配置的parent属性和java属性是相同的吗?

来源:互联网 发布:单片机电路图 编辑:程序博客网 时间:2024/06/16 03:11

Spring配置的parent属性和java属性是相同的吗?

本质上来说不同,Spring的parent的属性只是一个标签,用来创建实例的模板.简单来说,就省了注入属性的代码,可以把parent指向的bean的注入属性一起加进来。和方法无关。

SO的回答:

In spring, the parent in bean configuration signifies configuration inheritance and not related to Java inheritance.
The configuration inheritance saves a lot of code as you do away with repeated XML code.
For example, you have following bean with attributes

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.

Lets configure these two beans

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





Note that bean2 just needed to configure attrib3 and attrib4. The other >two attributes are inherited from 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.

0 0