简体   繁体   English

春豆理解

[英]Spring beans understanding

What is the use of extends and parent attributes in spring bean file. spring bean文件中extends和parent属性的用途是什么。 Is it related to a class extending another class. 它是否与扩展另一个类的类有关。 If some could share some thoughts around this it would be great. 如果有人可以分享一些关于这一点的想法,那就太好了。 Some linke and examples would also be helpful. 一些linke和例子也会有所帮助。

The abstract and parent mechanism is used to keep your XML configuration DRY (Don't Repeat Yourself). abstractparent机制用于保持XML配置干(不要重复自己)。

Consider you have 2 beans with 3 similar properties and 2 distinct. 考虑你有两个具有3个相似属性和2个不同的bean。

Instead of repeating those 3 similar properties in both beans, you can do this: 您可以执行以下操作,而不是在两个bean中重复这3个类似的属性:

  • make a bean that is abstract and holds those 3 common properties. 创建一个abstract的bean并保存这3个常见属性。
  • set the parent attribute on your 2 beans, to point to the abstract bean. 在2个bean上设置parent属性,指向抽象bean。

An example would be here . 一个例子就在这里

<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="BaseCustomerMalaysia" class="com.mkyong.common.Customer" abstract="true">
        <property name="country" value="Malaysia" />
    </bean>

    <bean id="CustomerBean" parent="BaseCustomerMalaysia">
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>

</beans>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM