Spring中的set注入
beans.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.Oracle.pojo.Address">
<property name="address" value="sajcfacs"></property>
</bean>
<bean id="student" class="com.Oracle.pojo.Student">
<!-- 第一种,普通值注入:value-->
<property name="name" value="刘振"/>
<!-- Bean注入,ref-->
<property name="address" ref="address"></property>
<!-- 数组注入,ref-->
<property name="book">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
<value>西游记</value>
</array>
</property>
<!-- List注入,ref-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>写代码</value>
<value>看电影</value>
<value>睡觉</value>
</list>
</property>
<!-- map注入,ref-->
<property name="card">
<map>
<entry key="身份证号" value="120112199911142114"></entry>
<entry key="银行卡" value="154541348651511561"></entry>
</map>
</property>
<!-- Set注入,ref-->
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
<value>CS</value>
</set>
</property>
<!-- NULL注入,ref-->
<property name="wife">
<null></null>
</property>
<!-- Properties注入-->
<property name="info">
<props>
<prop key="driver">20181013</prop>
<prop key="url">男</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
Student.java
package com.Oracle.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] book;
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[] getBook() {
return book;
}
public void setBook(String[] book) {
this.book = book;
}
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() +
", book=" + Arrays.toString(book) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
Address.java
package com.Oracle.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 + '\'' +
'}';
}
}
测试类 MyTest.java
import com.Oracle.pojo.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("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
System.out.println(student.getAddress());
System.out.println(student.getBook());
System.out.println(student.getCard());
System.out.println(student.getGames());
System.out.println(student.getHobbies());
System.out.println(student.getInfo());
System.out.println(student.getWife());
System.out.println(student);
}
}