文章目录
Spring DI 依赖注入
一、概述
- DI:Dependency Injection,依赖注入
- DI配合IOC一起使用
- DI就是为Spring所管理的属性进行赋值,如setter方法
- 是指对象间的依赖关系,绑定两个类之间的关系,可以由框架来完成
二、各种依赖注入环境准备
1、环境准备(2~7修改applicationContext.xml配置文件)
1.1、创建Maven项目
1.2、创建Maven的Module
1.3、导入jar包,配置pom.xml,并下载
<!--配置jar包-->
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
1.4、创建com.sgz.di.pojo包
1.5、创建Student类
package com.sgz.di.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* 作者:沈公子
* 日期:2022/8/4 - 19:39
* 需求:
*/
public class Student {
private Integer id;
private String name;
private Integer age;
private String gender;
private Teacher teacher; // 类类型
private String[] strings; // 数组类型,字面量类型
private Teacher[] teachers; // 数组类型,类类型
private List<String> lists; // List集合类型,字面量类型
private List<Teacher> teacherList; // List集合类型,类类型
private Map<String, Object> maps; // Map集合类型,字面量类型
private Map<String, Teacher> teacherMap; // Map集合类型,类类型
public Student() {
}
public Student(Integer id, String name, Integer age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public Student(Integer id, String name, Integer age, String gender, Teacher teacher, String[] strings, Teacher[] teachers, List<String> lists, List<Teacher> teacherList, Map<String, Object> maps, Map<String, Teacher> teacherMap) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.teacher = teacher;
this.strings = strings;
this.teachers = teachers;
this.lists = lists;
this.teacherList = teacherList;
this.maps = maps;
this.teacherMap = teacherMap;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String[] getStrings() {
return strings;
}
public void setStrings(String[] strings) {
this.strings = strings;
}
public Teacher[] getTeachers() {
return teachers;
}
public void setTeachers(Teacher[] teachers) {
this.teachers = teachers;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public List<Teacher> getTeacherList() {
return teacherList;
}
public void setTeacherList(List<Teacher> teacherList) {
this.teacherList = teacherList;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public Map<String, Teacher> getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
this.teacherMap = teacherMap;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", teacher=" + teacher +
", strings=" + Arrays.toString(strings) +
", teachers=" + Arrays.toString(teachers) +
", lists=" + lists +
", teacherList=" + teacherList +
", maps=" + maps +
", teacherMap=" + teacherMap +
'}';
}
}
1.6、创建Teacher类
package com.sgz.di.pojo;
/**
* 作者:沈公子
* 日期:2022/8/4 - 19:58
* 需求:
*/
public class Teacher {
private Integer id;
private String name;
public Teacher() {
}
public Teacher(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
1.7、创建applicationContext.xml配置文件(修改2~7)
<?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">
</beans>
1.8、创建com.sgz.di.text测试包
1.9、创建text测试类
package com.sgz.di.test;
import com.sgz.di.pojo.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 作者:沈公子
* 日期:2022/8/4 - 19:31
* 需求:
*/
public class TestDI{
@Test
public void testDI(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) ioc.getBean("student");
System.out.println(student);
}
}
2、setting注入
<?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-->
<!--
property:通过成员变量的set方法进行赋值
name:设置需要赋值的属性名(这个属性名是get,set方法定义的,和成员变量无关)
value:设置为属性所赋的值
-->
<bean id="student" class="com.sgz.di.pojo.Student">
<property name="id" value="1001"/>
<property name="name" value="张三"/>
<property name="age" value="20"/>
<property name="gender" value="男"/>
</bean>
</beans>
3、构造器注入
<?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">
<!--
constructor-arg:构造器注入
value:设置构造器的值
-->
<bean id="student" class="com.sgz.io.pojo.Student">
<constructor-arg value="1002"></constructor-arg>
<constructor-arg value="兮兮"></constructor-arg>
<constructor-arg value="22" ></constructor-arg>
<constructor-arg value="女"></constructor-arg>
<!--解决构造器冲突,假如实体类中含有多个构造器的参数数量一样,可以使用name属性指定需要赋值的参数名-->
<!-- <constructor-arg value="22" name="age"></constructor-arg> -->
</bean>
</beans>
4、类类型的注入
4.1、引用外部bean
<?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.sgz.di.pojo.Student">
<property name="id" value="1002"/>
<property name="name" value="李四"/>
<property name="age" value="21"/>
<property name="gender" value="男"/>
<!--引用外部的bean-->
<!--
name:实体类中有该属性名
ref:引用某个bean中的id
-->
<property name="teacher" ref="teacherOne"/>
</bean>
<!--外部的bean-->
<bean id="teacherOne" class="com.sgz.di.pojo.Teacher">
<property name="id" value="1111"/>
<property name="name" value="王者"/>
</bean>
</beans>
4.2、级联方式
<?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.sgz.di.pojo.Student">
<property name="id" value="1002"/>
<property name="name" value="李四"/>
<property name="age" value="21"/>
<property name="gender" value="男"/>
<!-- 级联方式,但是需要保证提前给clazz属性赋值或者实例化 -->
<property name="teacher" ref="teacherOne"/>
<property name="teacher.id" value="2222"/>
<property name="teacher.name" value="荣誉"/>
</bean>
<bean id="teacherOne" class="com.sgz.di.pojo.Teacher"/>
</beans>
4.3、内部bean
<?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.sgz.di.pojo.Student">
<property name="id" value="1003"/>
<property name="name" value="王五"/>
<property name="age" value="26"/>
<property name="gender" value="男"/>
<!--内部bean-->
<property name="teacher">
<!--内部bean,只能在当前bean的内部使用,无法通过IOC容器获取-->
<bean class="com.sgz.di.pojo.Teacher">
<property name="id" value="3333"/>
<property name="name" value="钻石"/>
</bean>
</property>
</bean>
</beans>
5、数组类型的注入
5.1、字面量类型
<?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.sgz.di.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
<!--数组类型属性赋值-->
<property name="strings">
<array>
<!--字面量类型-->
<value>吃</value>
<value>喝</value>
<value>玩</value>
<value>乐</value>
</array>
</property>
</bean>
</beans>
5.2、类类型
<?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="teacherOne" class="com.sgz.di.pojo.Teacher">
<property name="id" value="1111"></property>
<property name="name" value="王者"></property>
</bean>
<bean id="teacherTwo" class="com.sgz.di.pojo.Teacher">
<property name="id" value="2222"></property>
<property name="name" value="荣耀"></property>
</bean>
<!--数组类型赋值-->
<bean id="student" class="com.sgz.di.pojo.Student">
<property name="id" value="1001"></property>
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="gender" value="男"></property>
<property name="teachers">
<array>
<!--类类型-->
<ref bean="teacherOne"></ref>
<ref bean="teacherTwo"></ref>
</array>
</property>
</bean>
</beans>
6、List集合类型注入
6.1、字面量类型
<?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">
<!--List集合类型赋值-->
<bean id="student" class="com.sgz.di.pojo.Student">
<property name="id" value="1111"></property>
<property name="name" value="兮兮"></property>
<property name="age" value="20"></property>
<property name="gender" value="女"></property>
<property name="lists">
<list>
<!--字面量-->
<value>唱歌</value>
<value>直播</value>
<value>化妆</value>
</list>
</property>
</bean>
</beans>
6.2、类类型
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="teacherOne" class="com.sgz.di.pojo.Teacher">
<property name="id" value="1111"></property>
<property name="name" value="王者"></property>
</bean>
<bean id="teacherTwo" class="com.sgz.di.pojo.Teacher">
<property name="id" value="2222"></property>
<property name="name" value="荣耀"></property>
</bean>
<!--List集合类型赋值-->
<bean id="student" class="com.sgz.di.pojo.Student">
<property name="id" value="1002"></property>
<property name="name" value="李四"></property>
<property name="age" value="21"></property>
<property name="gender" value="男"></property>
<property name="teacherList">
<list>
<!--类类型-->
<ref bean="teacherOne"></ref>
<ref bean="teacherTwo"></ref>
</list>
</property>
</bean>
</beans>
6.3、util:list标签
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="teacherOne" class="com.sgz.di.pojo.Teacher">
<property name="id" value="1111"></property>
<property name="name" value="最强王者"></property>
</bean>
<bean id="teacherTwo" class="com.sgz.di.pojo.Teacher">
<property name="id" value="1111"></property>
<property name="name" value="最强王者"></property>
</bean>
<!--配置一个集合类型的bean,需要使用util的约束-->
<util:list id="teacherList">
<ref bean="teacherOne"></ref>
<ref bean="teacherTwo"></ref>
</util:list>
<bean id="student" class="com.sgz.di.pojo.Student">
<property name="id" value="1001"></property>
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="gender" value="男"></property>
<property name="teachers" ref="teacherList"></property>
</bean>
</beans>
7、Map集合类型注入
7.1、字面量类型
<?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.sgz.di.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
<property name="maps">
<map>
<entry key="1111" value="第一"/>
<entry key="2222" value="第二"/>
<entry key="333" value="第三"/>
</map>
</property>
</bean>
</beans>
7.2、类类型
<?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.sgz.di.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
<property name="teacherMap">
<map>
<entry key="10086" value-ref="teacherOne"></entry>
<entry key="10010" value-ref="teacherTwo"></entry>
</map>
</property>
</bean>
<bean id="teacherOne" class="com.sgz.di.pojo.Teacher">
<property name="id" value="10086"></property>
<property name="name" value="大宝"></property>
</bean>
<bean id="teacherTwo" class="com.sgz.di.pojo.Teacher">
<property name="id" value="10010"></property>
<property name="name" value="小宝"></property>
</bean>
</beans>
7.3、util:map标签
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="student" class="com.sgz.di.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
<property name="teacherMap" ref="teacherMap"></property>
</bean>
<bean id="teacherOne" class="com.sgz.di.pojo.Teacher">
<property name="id" value="10086"></property>
<property name="name" value="大宝"></property>
</bean>
<bean id="teacherTwo" class="com.sgz.di.pojo.Teacher">
<property name="id" value="10010"></property>
<property name="name" value="小宝"></property>
</bean>
<!--Map集合类型赋值-->
<util:map id="teacherMap">
<entry key="10086" value-ref="teacherOne"></entry>
<entry key="10010" value-ref="teacherTwo"></entry>
</util:map>
</beans>
8、特殊值处理
8.1、null
<!-- 赋值为字符串null -->
<property name="gender" value="null"></property>
<!-- 赋值为null空 -->
<property name="gender">
<null/>
</property>
8.2、特殊符号 <>
<!--
对特殊符号的处理
<:<
>:>
-->
<property name="name" value="<小明>"></property>
8.3、CDATA节 <![CDATA[...]]>
<!--
CDATA节其中的内容会原样解析 <![CDATA[...]]>
CDATA节是xml中一个特殊的标签,因此不能写在一个属性中,应写在标签中
快捷键:输入CD回车
-->
<property name="name">
<value><![CDATA[<小明>]]></value>
</property>
三、基于XML的自动装配 DI
1、简介
- 根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类型属性赋值
- bean的id唯一
2、代码案例
2.1、案例一(传统set注入+包扫描)
2.1.1、创建Maven项目
2.1.2、创建Maven的Module
2.1.3、导入jar包,配置pom.xml,并下载
<!--配置jar包-->
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
2.1.4、创建com.sgz.di.pojo 包
2.1.5、创建Boss类
package com.sgz.di.pojo;
import org.springframework.stereotype.Component;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:22
* 需求:
*/
@Component
public class Boss {
String name = "我是老板";
@Override
public String toString() {
return "Boss{" +
"name='" + name + '\'' +
'}';
}
}
2.1.6、创建Staff类
package com.sgz.di.pojo;
import org.springframework.stereotype.Component;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:23
* 需求:
*/
@Component
public class Staff {
String name = "我是员工";
// 1、绑定两个类间的关系,在一个类中定义另外一个类的变量
private Boss boss;
// 2、get / set 方法
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
// 3、toString用来测试显示数据
@Override
public String toString() {
return "Staff{" +
"name='" + name + '\'' +
", boss=" + boss +
'}';
}
}
2.1.7、创建配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sgz.di.pojo"/>
</beans>
2.1.8、创建com.sgz.di.text测试包
2.1.9、创建测试类TestDI
package com.sgz.di.test;
import com.sgz.di.pojo.Boss;
import com.sgz.di.pojo.Staff;
import org.junit.Test;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:26
* 需求:实现传统DI
*/
public class TestDI {
@Test
public void test(){
// Spring的DI传统实现(get,set)
Boss boss = new Boss();
System.out.println(boss); // Boss{name='我是老板'}
Staff staff = new Staff(); // Staff{name='我是员工', boss=null}
System.out.println(staff);
// DI:把两个对象间的关系依赖注入,实现DI
staff.setBoss(boss);
System.out.println(staff); // Staff{name='我是员工', boss=Boss{name='我是老板'}}
}
}
2.2、案例二(配置bean)
2.2.1、创建Maven项目
2.2.2、创建Maven的Module
2.2.3、导入jar包,配置pom.xml,并下载
<!--配置jar包-->
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
2.2.4、创建com.sgz.di…包,结构在下方图
2.2.5、创建UserDao接口
package com.sgz.di.dao;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:41
* 需求:
*/
public interface UserDao {
void VIPUser();
}
2.2.6、创建UserDaoImpl实现类
package com.sgz.di.dao.impl;
import com.sgz.di.dao.UserDao;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
public class UserDaoImpl implements UserDao {
@Override
public void VIPUser() {
System.out.println("我是VIP用户");
}
}
2.2.7、创建UserService接口
package com.sgz.di.service;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
public interface UserService {
void VIPUser();
}
2.2.8、创建UserServiceImpl实现类
package com.sgz.di.service.impl;
import com.sgz.di.dao.UserDao;
import com.sgz.di.service.UserService;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
public class UserServiceImpl implements UserService {
// 绑定两个类间的关系,在一个类中定义另外一个类的变量
private UserDao userDao;
// set get
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void VIPUser() {
userDao.VIPUser();
}
}
2.2.9、创建UserController类
package com.sgz.di.controller;
import com.sgz.di.service.UserService;
import org.springframework.stereotype.Controller;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
public class UserController {
// 绑定两个类间的关系,在一个类中定义另外一个类的变量
private UserService userService;
// set get
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void VIPUser(){
userService.VIPUser();
}
}
2.2.10、创建配置文件applicationContext.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="userController" class="com.sgz.di.controller.UserController">
<property name="userService" ref="userService"/>
</bean>
<bean id="userService" class="com.sgz.di.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userDao" class="com.sgz.di.dao.impl.UserDaoImpl"/>
</beans>
2.2.11、创建com.sgz.di.text测试包
2.2.12、创建测试类
package com.sgz.di.test;
import com.sgz.di.controller.UserController;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:49
* 需求:
*/
public class TestDIXML {
@Test
public void test(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController controller = ioc.getBean(UserController.class);
controller.VIPUser();
}
}
四、基于注解的自动装配 DI
1、注解
- @Autowired:自动装配,Spring中DI注解,想用哪个类的功能就 DI 过来(get / set),使用前必须加上IOC四个注解之一
- 在成员变量上,此时不需要设置成员变量的set方法(常用)
- 在set方法上
- 为当前成员变量赋值的有参构造上
2、代码案例
2.1、案例一(注解+包扫描)
2.1.1、创建Maven项目
2.1.2、创建Maven的Module
2.1.3、导入jar包,配置pom.xml,并下载
<!--配置jar包-->
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
2.1.4、创建com.sgz.di.pojo 包
2.1.5、创建Boss类
package com.sgz.di.pojo;
import org.springframework.stereotype.Component;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:22
* 需求:
*/
@Component
public class Boss {
String name = "我是老板";
@Override
public String toString() {
return "Boss{" +
"name='" + name + '\'' +
'}';
}
}
2.1.6、创建Staff类
package com.sgz.di.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:23
* 需求:
*/
@Component
public class Staff {
String name = "我是员工";
// 1、绑定两个类间的关系,在一个类中定义另外一个类的变量
/*
自动装配,该注解可以实现DI,会把数据注入,不需要 get / set
使用DI前,必须有IOC,才可以DI,Component -> Autowired
*/
@Autowired
private Boss boss;
@Override
public String toString() {
return "Staff{" +
"name='" + name + '\'' +
", boss=" + boss +
'}';
}
}
2.1.7、创建配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sgz.di.pojo"/>
</beans>
2.1.8、创建com.sgz.di.text测试包
2.1.9、创建测试类TestDI
package com.sgz.di.test;
import com.sgz.di.pojo.Boss;
import com.sgz.di.pojo.Staff;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:26
* 需求:
*/
public class TestDI {
@Test
public void test(){
// 1、读取配置文件
ClassPathXmlApplicationContext spring = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2、Spring的DI注解实现
Staff staff = (Staff) spring.getBean("staff");
System.out.println(staff);
}
}
2.2、案例二(注解+包扫描)
2.2.1、创建Maven项目
2.2.2、创建Maven的Module
2.2.3、导入jar包,配置pom.xml,并下载
<!--配置jar包-->
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
2.2.4、创建com.sgz.di…包,结构在下方图
2.2.5、创建UserDao接口
package com.sgz.di.dao;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:41
* 需求:
*/
public interface UserDao {
void VIPUser();
}
2.2.6、创建UserDaoImpl实现类
package com.sgz.di.dao.impl;
import com.sgz.di.dao.UserDao;
import org.springframework.stereotype.Repository;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void VIPUser() {
System.out.println("我是VIP用户");
}
}
2.2.7、创建UserService接口
package com.sgz.di.service;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
public interface UserService {
void VIPUser();
}
2.2.8、创建UserServiceImpl实现类
package com.sgz.di.service.impl;
import com.sgz.di.dao.UserDao;
import com.sgz.di.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
@Service
public class UserServiceImpl implements UserService {
// 绑定两个类间的关系,在一个类中定义另外一个类的变量
@Autowired
private UserDao userDao;
@Override
public void VIPUser() {
userDao.VIPUser();
}
}
2.2.9、创建UserController类
package com.sgz.di.controller;
import com.sgz.di.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:42
* 需求:
*/
@Controller
public class UserController {
// 绑定两个类间的关系,在一个类中定义另外一个类的变量
@Autowired
private UserService userService;
public void VIPUser(){
userService.VIPUser();
}
}
2.2.10、创建配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sgz.di"/>
</beans>
2.2.11、创建com.sgz.di.text测试包
2.2.12、创建测试类
package com.sgz.di.test;
import com.sgz.di.controller.UserController;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 作者:沈公子
* 日期:2022/8/5 - 11:49
* 需求:
*/
public class TestDIXML {
@Test
public void test(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController controller = (UserController) ioc.getBean("userController");
controller.VIPUser();
}
}