在Spring中,依赖注入有三种方式:构造器注入、Set注入、拓展注入,其中Set注入是重点,接下来将详细介绍不同类型变量使用Set注入如何实现。
1、配置环境:
- 以下软件环境为:IntelliJ IDEA
- 建立一个"spring-03-di"模块,在该模块下建一个"con.kuang.pojo"包
- 在包下建两个实体类:Address与Student
其中,Address类代码如下:
package com.kuang.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类代码如下:
package com.kuang.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
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> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
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() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
2、建立一个beans.xml文件,各种类型变量注入方式:基本类型、引用类型、数组、List、Map、Set、null、Properties
<?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.kuang.pojo.Address">
<property name="address" value="南京"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!--基本类型-->
<property name="name" value="陈大发" />
<!--引用类型-->
<property name="address" ref="address" />
<!--数组-->
<property name="books">
<array>
<value>语文</value>
<value>数学</value>
<value>英语</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>跑步</value>
<value>爬山</value>
</list>
</property>
<!--Map-->
<property name="cards">
<map>
<entry key="身份证" value="23262119880324774"></entry>
<entry key="银行卡" value="622223262119880324"></entry>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>斗地主</value>
<value>掼蛋</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">1190088</prop>
<prop key="性别">女</prop>
<prop key="民族">汉族</prop>
</props>
</property>
</bean>
</beans>
3、建立一个测试类MyTest,代码如下:
import com.kuang.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.toString());
}
}
输出为:
* Student{
* name='陈大发',
* address=Address{address='南京'},
* books=[语文, 数学, 英语],
* hobbys=[听歌, 跑步, 爬山],
* cards={身份证=23262119880324774, 银行卡=622223262119880324},
* games=[LOL, 斗地主, 掼蛋],
* wife='null',
* info={学号=1190088, 性别=女, 民族=汉族}}