之前使用无参构造方法或有参构造方法的方式 就是构造器注入,具体可以参照之前的博客
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 是 is .
<bean id="student" class="com.lding.pojo.Student">
<property name="name" value="冷丁">property>
bean>
<bean id="addr" class="com.lding.pojo.Address">
<property name="address" value="北京">property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="address" ref="addr">property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="books">
<array>
<value>操作系统value>
<value>计算机网路value>
<value>深入理解计算机系统value>
array>
property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="hobbies">
<list>
<value>编程value>
<value>看电影value>
<value>旅行value>
list>
property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="card">
<map>
<entry key="身份证" value="2102732312312312">entry>
<entry key="学生卡" value="179321803">entry>
map>
property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="games">
<set>
<value>LOLvalue>
<value>CFvalue>
<value>DNFvalue>
set>
property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="wife">
<null>null>
property>
bean>
<bean id="student" class="com.lding.pojo.Student">
<property name="info">
<props>
<prop key="学号">21321312prop>
<prop key="姓名">冷丁prop>
<prop key="性别">男prop>
props>
property>
bean>
接下来完成一个Demo 实现set注入
测试类 Address.java
package com.lding.pojo;
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.java
package com.lding.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
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 String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +"\n"+
", books=" + Arrays.toString(books)+"\n"+
", hobbies=" + hobbies +"\n"+
", card=" + card+"\n"+
", games=" + games+"\n"+
", wife='" + wife + '\'' +
", info=" + info +"\n"+
'}';
}
}
test.java
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
运行结果