liguofeng29’s blog

個人勉強用ブログだっす。

Spring4 - 抽象Beanと子Bean

下記定義は、人間は共通で目2個、口1個であることを定義してある。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
    <!-- abstract="true" 抽象Beanを定義する -->
    <bean id="personTemplate" abstract="true">
        <property name="eyes" value="2"/>
        <property name="mouse" ref="1"/>
    </bean>
    <!-- parent属性で子Beanを定義する -->
    <bean id="chinese" class="xxx.xxx.Chinese"
        parent="personTemplate"/>
    <bean id="american" class="xxx.xxx.Chinese"
        parent="personTemplate"/>
</beans>

抽象Beanを使わないとchinese,americanそれぞれに対してeyes,mouseを定義する必要があり、煩雑になってしまう。
保守する際に楽になるだろう。