要求被注入的属性,必须有set方法,set方法的方法名由set+属性首字母大写,如果属性是boolean类型,没有set方法,是is
项目目录
创建pojo类
Address.java
1 public class Address { 2 private String address; 3 4 public String getAddress() { 5 return address; 6 } 7 8 public void setAddress(String address) { 9 this.address = address; 10 } 11 }
Student.java
1 public class Student { 2 private String name; 3 private Address address; 4 private String[] books; 5 private List<String> hobbys; 6 private Map<String,String> card; 7 private Set<String> games; 8 private String wife; 9 private Properties info; 10 11 public String getName() { 12 return name; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 public Address getAddress() { 20 return address; 21 } 22 23 public void setAddress(Address address) { 24 this.address = address; 25 } 26 27 public String[] getBooks() { 28 return books; 29 } 30 31 public void setBooks(String[] books) { 32 this.books = books; 33 } 34 35 public List<String> getHobbys() { 36 return hobbys; 37 } 38 39 public void setHobbys(List<String> hobbys) { 40 this.hobbys = hobbys; 41 } 42 43 public Map<String, String> getCard() { 44 return card; 45 } 46 47 public void setCard(Map<String, String> card) { 48 this.card = card; 49 } 50 51 public Set<String> getGames() { 52 return games; 53 } 54 55 public void setGames(Set<String> games) { 56 this.games = games; 57 } 58 59 public String getWife() { 60 return wife; 61 } 62 63 public void setWife(String wife) { 64 this.wife = wife; 65 } 66 67 public Properties getInfo() { 68 return info; 69 } 70 71 public void setInfo(Properties info) { 72 this.info = info; 73 } 74 75 public void show(){ 76 System.out.println("name="+name+",address="+address.getAddress()+",book="); 77 for (String book:books){ 78 System.out.println(book); 79 } 80 System.out.println("爱好="+hobbys); 81 System.out.println("card="+card); 82 System.out.println("games="+games); 83 System.out.println("wife="+wife); 84 System.out.println("info="+info); 85 } 86 }
添加配置文件bean.xml,配置头
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 4 5 6 </beans>
1 <bean id="student" class="com.dz.pojo.Student"> 2 <property name="name" value="dz"/> 3 </bean>
测试
1 public class applicationtest { 2 @Test 3 public void test01(){ 4 ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml"); 5 Student student = (Student) classPathXmlApplicationContext.getBean("student"); 6 System.out.println(student.getName()); 7 } 8 }
Bean注入对象,这里的值是一个引用,ref
1 <!-- 这里的值是一个引用,ref--> 2 <property name="address" ref="addr"/> 3 <bean id="addr" class="com.dz.pojo.Address"> 4 <property name="address" value="北京"></property> 5 </bean>
1 <!-- 数组注入--> 2 <property name="books"> 3 <array> 4 <value>java1</value> 5 <value>java2</value> 6 <value>java3</value> 7 </array> 8 </property>
1 <!-- List注入--> 2 <property name="hobbys"> 3 <list> 4 <value>听歌</value> 5 <value>看电影</value> 6 <value>爬山</value> 7 </list> 8 </property>
1 <!-- Map注入--> 2 <property name="card"> 3 <map> 4 <entry key="dz1" value="11111111111"></entry> 5 <entry key="dz2" value="22222222222"></entry> 6 </map> 7 </property>
1 <!-- set注入--> 2 <property name="games"> 3 <set> 4 <value>LOL</value> 5 <value>DOTA</value> 6 </set> 7 </property>
1 <property name="wife"> 2 <null></null> 3 </property>
1 <!-- Properties注入--> 2 <property name="info"> 3 <props> 4 <prop key="学号">1212121</prop> 5 <prop key="性别">男</prop> 6 <prop key="姓名">dz</prop> 7 </props> 8 </property>
结果:
原文:https://www.cnblogs.com/lxzlovewyq/p/13698309.html