//Address类
package com.itheima;
//引用类
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
//Student类
package com.itheima;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] book;
private List<String> hobby;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String[] getBook() {
return book;
}
public void setBook(String[] book) {
this.book = book;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", book=" + Arrays.toString(book) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
//注解配置
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="address" class="com.itheima.Address">
<property name="address" value="攸县"></property>
</bean>
<bean id="student" class="com.itheima.Student">
<!--普通注入 ,value-->
<property name="name" value="阳鹏"></property>
<!--bean注入,ref-->
<property name="address" ref="address"></property>
<!--数组注入-->
<property name="book">
<array>
<value>西游记</value>
<value>红楼梦</value>
</array>
</property>
<!--list-->
<property name="hobby">
<list>
<value>听歌</value>
<value>打游戏</value>
</list>
</property>
<!--map-->
<property name="card">
<map>
<entry key="身份证" value="123456"/>
<entry key="银行卡" value="233"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>lol</value>
<value>王者荣耀</value>
</set>
<!--null注入-->
</property>
<property name="wife" value=""/>
<!--properties-->
<property name="info">
<props>
<prop key="学号">2025253427</prop>
</props>
</property>
</bean>
</beans>
//测试类Mytest
import com.itheima.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");
Student student =(Student) context.getBean("student");
System.out.println(student.toString());
}
}