DI依赖注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中所有属性由容器来注入
DI注入的几种方式:
- set方式注入(重点)
-
其他拓展方式注入
可以利用p命名空间和c命名空间进行注入

set方式注入

Address.java:
package com.kakafa.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.kakafa.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() +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
bean.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="com.kakafa.pojo.Address">
<property name="address" value="地球村2662号"/>
</bean>
<bean id="student" class="com.kakafa.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="许魏洲"/>
<!--第二种,bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books" >
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演绎</value>
<value>西游记</value>
</array>
</property>
<!--List注入-->
<property name="hobbies" >
<list>
<value>看书</value>
<value>打乒乓球</value>
<value>游泳</value>
<value>听歌</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="学号" value="202201064455"/>
<entry key="学院" value="音乐学院"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>英雄联盟</value>
<value>使命召唤</value>
</set>
</property>
<!--设置为null-->
<property name="wife">
<null/>
</property>
<!--如果要设置为空值,如下:
<property name="wife" value=""/>
-->
<!--Properties-->
<property name="info">
<props>
<prop key="年级">大一</prop>
<prop key="班级">一班</prop>
</props>
</property>
</bean>
</beans>
test:
import com.kakafa.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student student = (Student)context.getBean("student");
System.out.println(student.toString());
/*
* Student{
* name='许魏洲',
* address=Address{address='地球村2662号'},
* books=[红楼梦, 水浒传, 三国演绎, 西游记],
* hobbies=[看书, 打乒乓球, 游泳, 听歌],
* card={学号=202201064455, 学院=音乐学院},
* games=[王者荣耀, 英雄联盟, 使命召唤],
* wife='null',
* info={班级=一班, 年级=大一}}
* */
}
}

浙公网安备 33010602011771号