创建实体类

package cn.javaex.bean;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Person {
private String username;
private int age;
private String sex;
private String email;

private Car car;
private List<Book> bookList;
private Map<String, Object> map;
private Properties properties;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}

@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", sex=" + sex + ", email=" + email + ", car=" + car
+ ", bookList=" + bookList + ", map=" + map + ", properties=" + properties + "]";
}

}



配置文件

<?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.xsd">

<bean id="car01" class="cn.javaex.bean.Car">
<property name="carName" value="宝马"></property>
<property name="price" value="400000"></property>
<property name="color" value="黑色"></property>
</bean>

<bean id="person01" class="cn.javaex.bean.Person">
<!-- username==null -->
<property name="username">
<!-- 进行复杂的赋值 -->
<null />
</property>

<!-- ref:代表引用外面的一个值 -->
<!-- <property name="car" ref="car01"></property> -->
<!-- 引用内部bean -->
<property name="car">
<bean class="cn.javaex.bean.Car">
<property name="carName" value="奔驰"></property>
<property name="price" value="400000"></property>
<property name="color" value="黑色"></property>
</bean>
</property>
</bean>

<bean id="book01" class="cn.javaex.bean.Book">
<property name="bookName" value="三国志"></property>
</bean>

<bean id="person02" class="cn.javaex.bean.Person">
<!-- 为List类型赋值 -->
<property name="bookList">
<!-- bookList = new ArratList<Book>() -->
<list>
<!-- list标签体中添加每一个元素 -->
<bean class="cn.javaex.bean.Book">
<property name="bookName" value="西游记"></property>
</bean>
<!-- 引用外部一个元素 -->
<ref bean="book01"/>
</list>
</property>

<!-- Map<String, Object> map -->
<property name="map">
<!-- maps = new LinkedHashMap<>(); -->
<map>
<!-- 一个entry就代表一个键值对 -->
<entry key="key01" value="王五"></entry>
<entry key="key02" value="21"></entry>
<entry key="key03" value-ref="book01"></entry>
<entry key="key04">
<bean class="cn.javaex.bean.Car">
<property name="carName" value="奥迪"></property>
<property name="price" value="400000"></property>
<property name="color" value="黑色"></property>
</bean>
</entry>
</map>
</property>

<!-- private Properties properties; -->
<property name="properties">
<!-- properties = new Properties(); -->
<props>
<!-- 所有的key,value都是String -->
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>

</bean>
</beans>



测试

/**
* 为各种属性赋值
*/
@Test
public void test02() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("application2.xml");
Person bean1 = (Person) ioc.getBean("person01");
System.out.println(bean1.getCar());

Person bean2 = (Person) ioc.getBean("person02");
System.out.println(bean2.getBookList());

System.out.println(bean2.getMap());

System.out.println(bean2.getProperties());
}


输出结果

Car [carName=奔驰, price=400000, color=黑色]
[Book [bookName=西游记, author=null], Book [bookName=三国志, author=null]]
{key01=王五, key02=21, key03=Book [bookName=三国志, author=null], key04=Car [carName=奥迪, price=400000, color=黑色]}
{password=123456, username=root}



回到顶部