4、DI依赖注入
4.1、通过set方式注入
- 依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
- 复杂类型
package com.oy.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 真实测试对象 1.2拓展方式注入
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
-
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="student" class="com.oy.pojo.Student"> <!--第一种,普通注入:value--> <property name="name" value="欧阳" /> </bean> </beans>
-
需要加入的依赖pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.7.RELEASE</version> </dependency> </dependencies>
-
Test
import com.oy.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()); } }
-
完善注入信息
<?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.oy.pojo.Address"> <property name="address" value="长沙"></property> </bean> <bean id="student" class="com.oy.pojo.Student"> <!--第一种,普通注入:value--> <property name="name" value="欧阳" /> <!--第二种bean注入 ref/--> <property name="address" ref="address"/> <!-- array --> <property name="books"> <array> <value>红楼梦</value> <value>水浒传</value> <value>三国演义</value> <value>西游记</value> </array> </property> <!-- list --> <property name="hobbys"> <list> <value>听歌</value> <value>敲代码</value> <value>打游戏</value> </list> </property> <!-- map key-value --> <property name="card"> <map> <entry key="身份证" value="4310200105078771"></entry> <entry key="银行卡" value="2100232434343424"></entry> </map> </property> <!-- set --> <property name="games"> <set> <value>lol</value> <value>coc</value> <value>cf</value> </set> </property> <!-- 设置为空--> <property name="wife"> <null/> </property> <!-- properties自定义类--> <property name="info"> <props> <prop key="学号">201914356422</prop> <prop key="性别">男</prop> <prop key="年龄">25</prop> </props> </property> </bean> </beans>
1.2拓展方式注入
我们可以使用p命名空间和c命名空间进行注入
注意点
p命名空间和命名空间不能直接导入,需导入xml约束
xml头文件
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.oy.pojo.User" p:name="欧阳" p:age="28" />
<!-- c命名空间注入,可以通过构造器注入:construct-args-->
<!-- 使用前提需要有参和无参的构造方法-->
<bean id="user1" class="com.oy.pojo.User" c:name="唐人" c:age="39"/>
</beans>
实体类
package com.oy.pojo;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试类
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanUser.xml");
User user = context.getBean("user1", User.class);
System.out.println(user.toString());
}
需要加入的依赖 pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
4.2、bean的作用域
-
单例模式(Spring默认机制)
<bean id="user1" class="com.oy.pojo.User" c:name="唐人" c:age="39" scope="singleton"/>
-
原型模式:每次从容器中获取的时候,都会产生一个新对象
<bean id="user1" class="com.oy.pojo.User" c:name="唐人" c:age="39" scope="prototype"/>
-
其余的 request、session、application、这些只能在web开发中使用到
5、Bean的自动装配
- 自动装配是Spring满足bean依赖一种方式
- Spring会在上下文中自动寻找,并且自动给bean装配属性
在Spring中有三种装配的方式
- 在xml中显示的配置
- 在Java中显示配置
- 隐式的自动装配bean【重点】