bean的parent与abstract属性的使用

1.

  (1)在spring的配置文件中的bean能不能像Java中那样使用继承,就可以对一些父类的字段不用初始化,并且可以重写父类的字段了??

  (2)在如下配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <bean class="liusheng.entity.Teacher" name="teacher1">
            <property name="name" value="李四"></property>
            <property name="age" value="19"></property>
            <property name="sal" value="9000"></property>
    </bean>
    <bean class="liusheng.entity.Teacher" name="teacher2">
            <property name="name" value="李四"></property>
            <property name="age" value="19"></property>
            <property name="sal" value="8000"></property>
    </bean>
</beans>

  是不是发现两个老师除了sal以外的所有字段的值相同,这就形成的数据冗长,于是我们可以使用Spring的特性姐解决上述的问题

2.在Spring中提供了bean 的属性parent与abstract属性类实现这写功能

3.实体类(定义了一个Person类,定义了一个Teacher类,Teacher继承了Person)

Person类

package liusheng.entity;

public class Person {
        private String name;
        private Integer age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
        public Person() {
        }
        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
}

Teacher类:

package liusheng.entity;

public class Teacher extends Person {
        private Integer sal;
        public String toString() {
            return "Teacher [sal=" + sal + "]"+super.toString();
        }
        public Integer getSal() {
            return sal;
        }
        public void setSal(Integer sal) {
            this.sal = sal;
        }
        public Teacher() {
        }
        public Teacher(String name, Integer age,Integer sal) {
            super(name, age);
            this.sal=sal;
        }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <bean class="liusheng.entity.Person" name="parson"> 
        <property name="name" value="李四"></property>
        <property name="age" value="19"></property>
    </bean>
    <bean  parent="parson" class="liusheng.entity.Teacher" name="teacher1">
        <property name="sal" value="10000"></property>
    </bean>
    <bean  parent="parson" class="liusheng.entity.Teacher" name="teacher2">
        <property name="sal" value="10000"></property>
    </bean>
</beans>

console输出:

若不想父类被容器初始化,那么可以把Person的abstract属性设为true

2,若两个类之间不存在继承关系,可以把他们相同的属性抽取出来 ,抽取出来的由于是抽取出来的,所有没有class的属性

如下面的实体类:

package liusheng.entity;

public class User {
        private String name;
        private Integer age;
        private Integer id;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public User() {
        }
        public User(String name, Integer age, Integer id) {
            this.name = name;
            this.age = age;
            this.id = id;
        }
        
}

这个实体类的name和age 属性与Teacher 的name和age 属性 的名字和类型相同

故可以抽取出来当作一个抽象类

xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <bean abstract="true" name="parson"> 
        <property name="name" value="李四"></property>
        <property name="age" value="19"></property>
    </bean>
    <bean  parent="parson" class="liusheng.entity.Teacher" name="teacher1">
        <property name="sal" value="10000"></property>
    </bean>
    <bean  parent="parson" class="liusheng.entity.Teacher" name="teacher2">
        <property name="sal" value="100001"></property>
    </bean>
</beans>

结果如下:

总结:丛书上看到了这里我觉得spring强大的无可比拟,这激发我学习的Spring的无限兴趣,我爱Spring

 

转载于:https://www.cnblogs.com/SpringStudy/p/8577623.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值