spring中的继承

与java中的继承不同。java中的继承是类与类之间的继承,子类继承父类的属性与行为;spring中的集成是对象与对象之间的继承,子对象继承父对象的属性值。

 

代码示例

<bean name="student1" class="com.exambner.ioc.Student">
        <property name="id" value="1"></property>
        <property name="name" value="小明"></property>
        <property name="age" value="18"></property>
</bean>

<bean name="student2" class="com.exambner.ioc.Student" parent="student1">
</bean>
public static void main(String[] args) {
        // 加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Student student = (Student) applicationContext.getBean("student1");
        System.out.println(student);
    }
Student{id=1, name='小明', age=18, addressList=null}

使用parent属性可以指定当前bean的父对象。这时当前bean就具有了父对象的所有属性值信息。也可以在子对象中对父对象信息进行覆盖赋值。

<bean name="student1" class="com.exambner.ioc.Student">
        <property name="id" value="1"></property>
        <property name="name" value="小明"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean name="student2" class="com.exambner.ioc.Student" parent="student1">
        <property name="name" value="小红"></property>
    </bean>
Student{id=1, name='小红', age=18, addressList=null}

 

不同类之间的继承,子类需要包含父对象所有的属性,但是可以有额外的属性。

父类:

package com.exambner.ioc;

import java.util.List;

public class Student {

    private long id;

    private String name;

    private int age;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

子类:

package com.exambner.ioc;

/**
 * @Author: wangwang
 * @Date: 2023-01-04-23:38
 * @Description:
 */
public class User {

    private long id;

    private String name;

    private int age;

    private String sex;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

配置文件:

<bean name="student1" class="com.exambner.ioc.Student">
        <property name="id" value="1"></property>
        <property name="name" value="小明"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean name="student2" class="com.exambner.ioc.Student" parent="student1">
        <property name="name" value="小红"></property>
    </bean>

    <bean id="user" class="com.exambner.ioc.User" parent="student2">
        <property name="sex" value="男"></property>
    </bean>

输出user:

public static void main(String[] args) {
        // 加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);
    }
User{id=1, name='小红', age=18, sex='男'}

Spring的继承关注点在于具体的对象,即不同类的两个对象也可以完成继承,前提是子对象需要包含父对象的所有属性,同时可以在此基础上添加其他属性。

posted @ 2023-01-04 22:43  Amireux-126  阅读(220)  评论(0)    收藏  举报