Student(学生类)
1 package com.kuang.pojo; 2 3 import java.util.*; 4 5 public class Student { 6 private String name; 7 private Address address; 8 private String[] books; 9 private List<String> hobbys; 10 private Map<String,String> card; 11 private Set<String> games; 12 private String wife; 13 private Properties info; 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public Address getAddress() { 24 return address; 25 } 26 27 public void setAddress(Address address) { 28 this.address = address; 29 } 30 31 public String[] getBooks() { 32 return books; 33 } 34 35 public void setBooks(String[] books) { 36 this.books = books; 37 } 38 39 public List<String> getHobbys() { 40 return hobbys; 41 } 42 43 public void setHobbys(List<String> hobbys) { 44 this.hobbys = hobbys; 45 } 46 47 public Map<String, String> getCard() { 48 return card; 49 } 50 51 public void setCard(Map<String, String> card) { 52 this.card = card; 53 } 54 55 public Set<String> getGames() { 56 return games; 57 } 58 59 public void setGames(Set<String> games) { 60 this.games = games; 61 } 62 63 public String getWife() { 64 return wife; 65 } 66 67 public void setWife(String wife) { 68 this.wife = wife; 69 } 70 71 public Properties getInfo() { 72 return info; 73 } 74 75 public void setInfo(Properties info) { 76 this.info = info; 77 } 78 79 @Override 80 public String toString() { 81 return "Student{" + 82 "name='" + name + '\'' + 83 ", address=" + address + 84 ", books=" + Arrays.toString(books) + 85 ", hobbys=" + hobbys + 86 ", card=" + card + 87 ", games=" + games + 88 ", wife='" + wife + '\'' + 89 ", info=" + info + 90 '}'; 91 } 92 }
Address(地址类)
1 package com.kuang.pojo; 2 3 public class Address { 4 private String address; 5 6 public String getAddress() { 7 return address; 8 } 9 10 public void setAddress(String address) { 11 this.address = address; 12 } 13 14 @Override 15 public String toString() { 16 return "Address{" + 17 "address='" + address + '\'' + 18 '}'; 19 } 20 }
beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 https://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <!-- set注入 --> 7 <bean id="address" class="com.kuang.pojo.Address"> 8 <property name="address" value="火之国:木叶村"/> 9 </bean> 10 11 <bean id="student" class="com.kuang.pojo.Student"> 12 <!--第一种:普通值注入,value--> 13 <property name="name" value="漩涡鸣人"/> 14 <!--第二种:Bean注入,ref--> 15 <property name="address" ref="address"/> 16 <!--数组注入--> 17 <property name="books"> 18 <array> 19 <value>海贼王</value> 20 <value>小叮当</value> 21 <value>君的名字</value> 22 <value>火影忍者</value> 23 </array> 24 </property> 25 <!--list注入--> 26 <property name="hobbys"> 27 <list> 28 <value>写bug</value> 29 <value>找bug</value> 30 <value>改bug</value> 31 </list> 32 </property> 33 <!--Map--> 34 <property name="card"> 35 <map> 36 <entry key="身份证" value="木叶村:七代目"/> 37 <entry key="银行卡" value="木叶村:自己脑补"/> 38 </map> 39 </property> 40 <!--Set--> 41 <property name="games"> 42 <set> 43 <value>wzry</value> 44 <value>cjzc</value> 45 <value>mrzh</value> 46 </set> 47 </property> 48 <!-- null --> 49 <property name="wife"> 50 <null/> 51 </property> 52 <!-- Properties --> 53 <property name="info"> 54 <props> 55 <prop key="username">那累都</prop> 56 <prop key="password">1234</prop> 57 <prop key="sex">man</prop> 58 <prop key="driver">77</prop> 59 </props> 60 </property> 61 62 63 </bean> 64 65 </beans>
MyTest(测试类)
1 import com.kuang.pojo.Student; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class MyTest { 6 public static void main(String[] args) { 7 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 8 Student student = (Student)context.getBean("student"); 9 System.out.println(student.toString()); 10 11 /* 12 Student{name='漩涡鸣人', 13 address=Address{address='火之国:木叶村'}, 14 books=[海贼王, 小叮当, 君的名字, 火影忍者], 15 hobbys=[写bug, 找bug, 改bug], 16 card={身份证=木叶村:七代目, 银行卡=木叶村:自己脑补}, 17 games=[wzry, cjzc, mrzh], 18 wife='null', 19 info={password=1234, sex=man, driver=77, username=那累都}} 20 */ 21 } 22 }