• SpringBean 定义继承


    Bean定义继承

    bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等。子bean的定义继承副定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。

    Spring Bean定义的继承与Java类的继承无关,但是继承的概念是一样的。你可以定义一个父bean的定义作为模板和其他子bean就可以从父bean中继承所需的配置。

    当你使用基于XML的配置元数据时,通过使用父属性,指定父bean作为该属性的值来表明自bean的定义。

    Bean.xml:在该配置文件中我们定义有两个属性message1,message2的“helloworld“bean,然后,使用parent属性把”helloIndia“bean定义为”helloworld“的孩子。这个字bean继承message2的属性,重写message1的属性,并且引入一个属性message3.

    <?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-3.0.xsd">
         <bean id="helloworld" class="com.tuorialsponit.HelloWorld">
             <property name="message1" value="Hello world"/>
             <property name="message2" value="Hello Second World"></property>
         </bean>
         
         <bean id="helloIndia" class="com.tuorialsponit.HelloIndia" parent="helloworld">
             <property name="message1" value="hello India"></property>
             <property name="message3" value="Namaste India"></property>
         </bean>
     </beans>

    HelloWorld.java的内容:

    package com.tuorialsponit;
    
    public class HelloWorld {
        private String message1;
        private String message2;
        public String getMessage1() {
            return message1;
        }
        public void setMessage1(String message1) {
            this.message1 = message1;
        }
        public String getMessage2() {
            return message2;
        }
        public void setMessage2(String message2) {
            this.message2 = message2;
        }
        
    }

    HelloIndia.java:

    package com.tuorialsponit;
    
    public class HelloIndia {
        private String message1;
        private String message2;
        private String message3;
        public String getMessage1() {
            return message1;
        }
        public void setMessage1(String message1) {
            this.message1 = message1;
        }
        public String getMessage2() {
            return message2;
        }
        public void setMessage2(String message2) {
            this.message2 = message2;
        }
        public String getMessage3() {
            return message3;
        }
        public void setMessage3(String message3) {
            this.message3 = message3;
        }
    }

    MainApp.java内容:

    package com.tuorialsponit;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
        public static void main(String[] args){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            HelloWorld obj1 = (HelloWorld) context.getBean("helloworld");
            System.out.println(obj1.getMessage1());
            System.out.println(obj1.getMessage2());
            
            System.out.println("-----------------------");
            HelloIndia obj2 = (HelloIndia) context.getBean("helloIndia");
            System.out.println(obj2.getMessage1());
            System.out.println(obj2.getMessage2());
            System.out.println(obj2.getMessage3());
    //        String message = obj.getMessage();
    //        System.out.println(message);
        }
    }

    执行结果:

    在这里你可以观察到,我们创建”helloIndia“bean的同时并没有传递message2,但是由于Bean定义的继承,所以它传递了message2.

    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-3.0.xsd">
        <bean id="beanTemplate" abstract="true">
            <property name="message1" value="Hello world"/>
             <property name="message2" value="Hello dddddddddSecond World"></property>
             <property name="message3" value="Hello world1"/>
        </bean>
         <bean id="helloworld" class="com.tuorialsponit.HelloWorld">
             <property name="message1" value="Hello world"/>
             <property name="message2" value="Hello Second World"></property>
         </bean>
         
         <bean id="helloIndia" class="com.tuorialsponit.HelloIndia" parent="beanTemplate">
             <property name="message1" value="hello India"></property>
             <property name="message3" value="Namaste India"></property>
         </bean>
     </beans>

    父bean(abstract)自身不能被实例化,因为它是不完整的,而且它也被明确地标记为抽象的。当一个定义是抽象的,它仅仅作为一个纯粹的模板bean定义来使用,充当子定义的父定义使用。

  • 相关阅读:
    soso指定关键字页面搜索内容爬取 Ryan
    警惕ASP网站Global.asa导致网站被挂马或转向
    exp1 逆向与Bof基础实验报告
    Pytorch中Unormalize变换的实现
    postgresql model Jsonb格式存储 gorm2.0
    jmeter Uncaught Exception java.lang.NoClassDefFoundError: Could not initialize class org.apache.jmeter.gui.util.FileDialoger in thread Thread[AWTEventQueue0,6,main]. See log file for details.
    介绍 PostgreSQL STRING_AGG() 和 ARRAY_AGG()函数
    问题golang uint类型如何转换成string
    combobox,edit可以设置边框颜色不?
    IOS中如何解析并显示Gif 一片
  • 原文地址:https://www.cnblogs.com/fangpengchengbupter/p/7811896.html
Copyright © 2020-2023  润新知