本文讲解了Set方式常见的注入方式:
1. 普通值注入:value
2.bean注入:ref
3.数组注入
4.List注入
5.Map注入
6.Set注入
7.null值注入
8.Properties注入:key=value
什么是依赖注入?
依赖注入
·依赖:bean对象的创建依赖于容器!
·注入:bean对象中的所有属性,由容器来注入
学生类 ↓
package cn.di.spring;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobby;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
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> 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 Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
地址类↓
package cn.di.spring;
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 + '\'' +
'}';
}
}
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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="cn.di.spring.Address">
<property name="address" value="中国"/>
</bean>
<bean id="student" class="cn.di.spring.Student">
<!--第1种:普通值注入:value-->
<property name="name" value="张三"/>
<!--第2种:bean注入:ref-->
<property name="address" ref="address"/>
<!--第3种:数组注入:-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<!--第4种:list注入:-->
<property name="hobby">
<list>
<value>打球</value>
<value>唱歌</value>
<value>代码</value>
</list>
</property>
<!--第5种:map注入:-->
<property name="card">
<map>
<entry key="身份证" value="000545"/>
<entry key="银行卡" value="111454"/>
</map>
</property>
<!--第6种:Set注入:-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--第7种:null值注入:-->
<property name="wife">
<null/>
</property>
<!--第8种:Properties注入:key=value -->
<property name="info">
<props>
<prop key="学号">193102008</prop>
<prop key="性别">男</prop>
<prop key="姓名">张三</prop>
</props>
</property>
</bean>
</beans>
测试类 ↓
import cn.di.spring.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 s = (Student) context.getBean("student");
System.out.println(s.toString());
}
}