繁体   English   中英

春豆理解

[英]Spring beans understanding

spring bean文件中extends和parent属性的用途是什么。 它是否与扩展另一个类的类有关。 如果有人可以分享一些关于这一点的想法,那就太好了。 一些linke和例子也会有所帮助。

abstractparent机制用于保持XML配置干(不要重复自己)。

考虑你有两个具有3个相似属性和2个不同的bean。

您可以执行以下操作,而不是在两个bean中重复这3个类似的属性:

  • 创建一个abstract的bean并保存这3个常见属性。
  • 在2个bean上设置parent属性,指向抽象bean。

一个例子就在这里

<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