1. DI依赖注入
依赖注入(Dependency Injection,简称DI),是IOC控制反转的一种方式
还有一种方式叫“依赖查找”(Dependency Lookup)。
通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。
2. 注入方式
-
构造器注入
-
set方式注入(重点)
依赖:bean对象的创建依赖于容器;
注入:bean对象中的所有属性,由容器来注入;
3. 环境搭建
复杂类型
真实测试对象
pojo类:(包含引用类型和基本类型的复杂类型)
package com.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> game;
private Properties infor;
private String wife;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", game=" + game +
", infor=" + infor +
", wife='" + wife + '\'' +
'}';
}
}
引用的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 + '\'' +
'}';
}
}
spring的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.pojo.Address">
<property name="address" value="xian"></property>
</bean>
<bean id="student" class="com.pojo.Student">
<property name="name" value="hou"/>
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>三国</value>
<value>西游</value>
<value>水浒</value>
</array>
</property>
<!--list-->
<property name="hobbies">
<list>
<value>eat</value>
<value>drink</value>
<value>play</value>
</list>
</property>
<property name="card">
<map>
<entry key="1" value="12"/>
<entry key="2" value="23"/>
</map>
</property>
<property name="game">
<set>
<value>wangzhe</value>
<value>daota</value>
<value>lol</value>
</set>
</property>
<property name="wife">
<null></null>
</property>
<!--properties-->
<property name="infor">
<props>
<prop key="id">20200405</prop>
<prop key="name">hdk</prop>
</props>
</property>
</bean>
</beans>
第三方标签
p标签和c标签
package com.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入/set注入-->
<bean id="user" class="com.pojo.User" p:name="li" p:age="10">
</bean>
<!--c命名空间/构造器-->
<bean id="user" class="com.pojo.User" c:name="li" c:age="19"></bean>
</beans>
bean的作用域
bean拥有的作用域有:
singleton、prototype、request、session、application
- 单例模式(默认)singleton
<bean id="user" class="com.pojo.User" c:name="li" c:age="22" scope="singleton"></bean>
- 原型模式:prototype 每次从容器中get的时候,都产生一个新对象;
<bean id="user" class="com.pojo.User" c:name="li" c:age="22" scope="prototype"></bean>
其余的request、session、application这些只能在web开放中使用。