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