实验4:正确的为各种属性赋值。
各种bean类
Person类
public class Person {
//基本类型直接使用
//<property name="lastName" value="小花"></property>自动的进行类型转换;
private String lastName = "小明";
private Integer age;
private String gender;
private String email;
private Car car;
private List<Book> books;
private Map<String, Object> maps;
private Properties properties;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Person(String lastName, Integer age, String gender, String email) {
super();
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.email = email;
System.out.println("有参构造器...");
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Person [lastName=" + lastName + ", age=" + age + ", gender="
+ gender + ", email=" + email + ", car=" + car + ", books="
+ books + ", maps=" + maps + ", properties=" + properties + "]";
}
Book类
public class Book {
private String bookName;
private String author;
public Book() {
super();
// TODO Auto-generated constructor stub
System.out.println("book被创建");
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [bookName=" + bookName + ", author=" + author + "]";
}
Car类
public class Car {
private String carName;
private Integer price;
private String color;
public Car() {
super();
// TODO Auto-generated constructor stub
System.out.println("car被创建...");
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [carName=" + carName + ", price=" + price + ", color="
+ color + "]";
}
实验4.1、测试使用null值
xml配置信息
<-- 添加person对象的bean为lastName属性赋空值 -->
<bean id="person01" class="com.zxy.bean.Person">
<!-- lastName="null" -->
<property name="lastName">
<null />
</property>
</bean>
实验4.2、引用类型赋值(引用其他bean、引用内部bean)
xml配置信息
<bean id="car01" class="com.zxy.bean.Car">
<property name="carName" value="宝马"></property>
<property name="color" value="绿色"></property>
<property name="price" value="30000"></property>
</bean>
<-- 引用其他外部的car为本bean的car属性赋值 -->
<bean id="person02" class="com.atguigu.bean.Person">
<!--
ref:代表引用外面的一个值 ,引用其他bean;
ref="car01" ==>car = ioc.getBean("car01")
-->
<property name="car" ref="car01"></property>
</bean>
<bean id="person03" class="com.zxy.bean.Person">
<property name="car">
<!--
对象我们可以使用bean标签创建 car = new Car();
注意:设置的内部bean不能被获取到,只能内部使用
-->
<bean class="com.zxy.bean.Car">
<property name="carName" value="大奔"></property>
<property name="color" value="pink"></property>
<property name="price" value="9999"></property>
</bean>
</property>
</bean>
实验4.3、集合类型赋值(List、Map、Properties)
xml配置信息
<bean id="person04" class="com.zxy.bean.Person">
<!-- 如何为list类型赋值 -->
<property name="books">
<!-- books = new ArrayList<Book>(); -->
<list>
<!-- list标签体中添加每一个元素 -->
<bean class="com.zxy.bean.Book" p:bookName="西游记"></bean>
<!-- 引用外部一个元素 -->
<ref bean="book01" />
</list>
</property>
<!-- Map<String, Object> maps; -->
<property name="maps">
<!-- maps = new LinkedHashMap<>(); -->
<map>
<!-- 一个entry代表一个键值对 -->
<entry key="key01" value="张三"></entry>
<entry key="key02" value="18"></entry>
<entry key="key03" value-ref="book01"></entry>
<entry key="key04">
<bean class="com.zxy.bean.Car">
<property name="carName" value="宝马"></property>
</bean>
</entry>
<entry key="key05">
<value>李四</value>
</entry>
<!-- <entry key="key05"> <map></map> </entry> -->
</map>
</property>
<!-- private Properties properties; -->
<property name="properties">
<!-- properties = new Properties();所有的k=v都是string -->
<props>
<!-- k=v都是string;值直接写在标签体中 -->
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
实验4.4、util名称空间创建集合类型的bean
xml配置信息
<!-- util名称空间创建集合类型的bean;方便别人引用 -->
<bean id="person03" class="com.atguigu.bean.Person">
<!-- <property name="maps" ></property> -->
<property name="maps" ref="myMap"></property>
</bean>
<!-- 相当于new LinkedHashMap<>() -->
<util:map id="myMap">
<!-- 添加元素 -->
<entry key="key01" value="张三"></entry>
<entry key="key02" value="18"></entry>
<entry key="key03" value-ref="book01"></entry>
<entry key="key04">
<bean class="com.atguigu.bean.Car">
<property name="carName" value="宝马"></property>
</bean>
</entry>
<entry key="key05">
<value>李四</value>
</entry>
</util:map>
实验4.5、级联属性赋值
xml配置信息
<!-- 级联属性赋值: 级联属性:属性的属性-->
<bean id="person04" class="com.atguigu.bean.Person">
<!--为car赋值的时候。改变car的价格 -->
<property name="car" ref="car01"></property>
<!-- 为car属性的级联属性price赋值 -->
<property name="car.price" value="900000"></property>
</bean>
实验5比较重要之后重点说明
实验6:通过abstract属性创建一个模板bean
xml配置信息
<!-- abstract="true":设置这个bean的配置是一个抽象的,不能获取他的实例,只能被别人用来继承 -->
<bean id="person05" class="com.zxy.bean.Person" abstract="true">
<property name="lastName" value="张三"></property>
<property name="age" value="18"></property>
<property name="gender" value="男"></property>
<property name="email" value="zhangsan@168.com"></property>
</bean>
实验7:通过继承实现bean配置信息的重用
xml配置信息
<!--parent:指定当前bean的配置信息继承于哪个 -->
<bean id="person06" class="com.zxy.bean.Person" parent="person05">
<property name="lastName" value="李四"></property>
</bean>
今天到此结束!!!
下次见~~~