The IoC Container 1.7

作者: 小鲍比大爷 | 来源:发表于2019-02-17 17:34 被阅读0次

    1.7. Bean Definition Inheritance

    xml中可以通过配置bean定义来配置对象的属性、方法等一系列配置。Spring提供了一个功能,在多个bean拥有相同配置的情况下,可以将公共部分抽象成父bean定义,其他的bean继承父bean的定义,子类的bean根据自身的情况覆盖父bean定义的配置或者添加自己独有的配置。
    配置示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <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="inheritedTestBean" abstract="true"
              class="org.springframework.beans.TestBean">
            <property name="name" value="parent"/>
            <property name="age" value="1"/>
        </bean>
    
        <bean id="inheritsWithDifferentClass"
              class="examples.DerivedTestBean"
              parent="inheritedTestBean" init-method="initialize">
            <property name="name" value="override"/>
            <!-- the age property value of 1 will be inherited from parent -->
        </bean>
    
        <bean id="inheritedTestBeanWithoutClass" abstract="true">
            <property name="name" value="parent"/>
            <property name="age" value="1"/>
        </bean>
    
        <bean id="inheritsWithClass" class="examples.DerivedTestBean"
              parent="inheritedTestBeanWithoutClass" init-method="initialize">
            <property name="name" value="override"/>
            <!-- age will inherit the value of 1 from the parent bean definition-->
        </bean>
    
    </beans>
    

    上述配置中,假设bean定义指定了class,比如inheritedTestBean,那么abstract可以设置成true,也可以设置成false,设置成true,Spring容器会在启动时初始化该bean,否则不初始化;如果不指定class,比如inheritedTestBeanWithoutClass,那么abstract必须设置成true,因为不指定类型是没有办法初始化的。
    代码示例:

    package examples;
    
    import lombok.Data;
    
    @Data
    public class DerivedTestBean {
    
        private String name;
        private int age;
    
        public void initialize() {
    
        }
    
        @Override
        public String toString() {
            return String.format("name=%s, age=%d", name, age);
        }
    }
    
    package examples;
    
    import lombok.Data;
    
    @Data
    public class TestBean {
    
        private String name;
        private int age;
    }
    
    import examples.DerivedTestBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Application {
    
        public static void main(String[] args) {
            final ApplicationContext context =
                    new ClassPathXmlApplicationContext( "inherit.xml");
    
            DerivedTestBean derivedTestBean1 = context.getBean("inheritsWithDifferentClass", DerivedTestBean.class);
            System.out.println(derivedTestBean1);
    
            DerivedTestBean derivedTestBean2 = context.getBean("inheritsWithClass", DerivedTestBean.class);
            System.out.println(derivedTestBean2);
        }
    }
    

    执行结果:

    name=override, age=1
    name=override, age=1
    

    该节所提供的功能相当于支持bean定义提供模板定义(父bean定义),通过指定parent属性的值,可以继承模板定义的配置。

    相关文章

      网友评论

        本文标题:The IoC Container 1.7

        本文链接:https://www.haomeiwen.com/subject/ogageqtx.html