摘要:Spring笔记_04-2021-06-28
1.什么是依赖注入?
**官方说明:**https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-dependencies
依赖注入:
-
依赖注入(DI)是一个过程,对象仅通过构造函数参数、工厂方法的参数或在对象实例构造或从工厂方法返回后在对象实例上设置的属性来定义它们的依赖(即与它们一起工作的其他对象)
-
Spring容器在创建bean时注入这些依赖项。这个过程基本上是bean本身的逆过程(因此称为控制反转),通过使用类的直接构造或服务定位器模式来控制其依赖项的实例化或位置
-
使用DI原则,代码更干净,当对象具有依赖关系时,解耦更有效。对象不查找其依赖项,也不知道依赖项的位置或类。因此,类变得更容易测试,特别是当依赖关系在接口或抽象基类上时,这些接口或抽象基类允许在单元测试中使用存根或模拟实现
DI有两个主要的方式:
- 基于构造函数的依赖注入
- 基于Setter的依赖注入
- 其它方式
2.基于构造函数的依赖注入
2.1 搭建环境
1.新建一个maven项目,导入依赖
2.导入容器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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
3.编写接口UserDao和实现类UserDaoImpl、UserDaoImpl2、UserDaoImpl3
package com.riove.dao;
public interface UserDao {
void getUser();
}
package com.riove.dao;
public class UserDaoImpl implements UserDao{
public void getUser() {
System.out.println("SqlServer数据库!");
}
}
package com.riove.dao;
public class UserDaoImpl2 implements UserDao{
public void getUser() {
System.out.println("MySql数据库!");
}
}
package com.riove.dao;
public class UserDaoImpl3 implements UserDao{
public void getUser() {
System.out.println("Oracle数据库!");
}
}
4.编写接口UserService和实现类UserServiceImpl
package com.riove.service;
import com.riove.dao.UserDao;
public interface UserService {
void getUser();
void setUserDao(UserDao userDao);
}
package com.riove.service;
import com.riove.dao.UserDao;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserServiceImpl() {
}
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUser() {
userDao.getUser();
}
}
5.实体类(备用)
package com.riove.pojo;
/**
* @Author Riove
* @Create 2021/6/28 20:05
*/
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.2 构造函数参数解析
<!--2.1 构造函数参数解析-->
<!--参数解析:
前提:当我们已经知道传入的参数结构,比如该例:需要传的是这三个对象UserDaoImpl、UserDaoImpl2、UserDaoImpl3其中之一
重点:这三个对象已经在Spring容器中创建,即由bean对象创建,方能引用
rel:当传入参数为已知对象的id
关键:需要传入参数的类中必须含有构造方法,比如该例:UserServiceImpl中必须存在构造方法
-->
<bean id="userServiceImpl" class="com.riove.service.UserServiceImpl">
<constructor-arg ref="userDaoImpl"/>
</bean>
<!--在容器中创建对象-->
<bean id="userDaoImpl" class="com.riove.dao.UserDaoImpl"/>
<bean id="userDaoImpl2" class="com.riove.dao.UserDaoImpl2"/>
<bean id="userDaoImpl3" class="com.riove.dao.UserDaoImpl3"/>
public class DITest {
@Test
public void test1() {
//1.获取上下文
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
*2.通过容器,我们可以做我们想做的任何业务,比如:获取bean对象
*
*Tip:getBean();这个方法可以传入多个属性值
* 当我们知道该对象返回值类型,只需要在传入的参数对应对象类型,这样就不用强转了
*/
UserServiceImpl userServiceImpl = context.getBean("userServiceImpl", UserServiceImpl.class);
userServiceImpl.getUser();
}
}
2.3 构造函数参数类型匹配
<!--2.2 构造函数参数类型匹配-->
<!--参数类型匹配:
前提:当我们已经知道传入的参数的类型,比如:public User(int id, String username, String password)
重点:注意参数的个数,有多少个参数就必须注入多少个值,如构造函数User中有3个参数,那么必须对应有3个<constructor-arg type="" value=""/>
type:参数类型
value:赋值
-->
<bean id="user" class="com.riove.pojo.User">
<constructor-arg type="int" value="123456"/>
<constructor-arg type="java.lang.String" value="admin"/>
<constructor-arg type="java.lang.String" value="admin"/>
</bean>
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
*2.通过容器,我们可以做我们想做的任何业务,比如:获取bean对象
*
*Tip:getBean();这个方法可以传入多个属性值
* 当我们知道该对象返回值类型,只需要在传入的参数对应对象类型,这样就不用强转了
*/
User user = context.getBean("user", User.class);
System.out.println(user);
}
2.4 构造函数参数索引
`<!--2.3 构造函数参数索引-->
<!--参数索引:
前提:当我们已经知道传入的参数的个数,我觉得还得知道它们的属性,和需要传入的值,不过通过索引:个数:是重点
重点:参数的个数,有多少个参数就必须注入多少个值,如User中有3个参数,那么必须对应有3个<constructor-arg index="" value=""/>
index:索引,类似我们所学的数组:index[0]、index[1]、index[3]
value:赋值
-->
<bean id="user" class="com.riove.pojo.User">
<constructor-arg index="0" value="123456"/>
<constructor-arg index="1" value="admin"/>
<constructor-arg index="2" value="admin"/>
</bean>
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
*2.通过容器,我们可以做我们想做的任何业务,比如:获取bean对象
*
*Tip:getBean();这个方法可以传入多个属性值
* 当我们知道该对象返回值类型,只需要在传入的参数对应对象类型,这样就不用强转了
*/
User user = context.getBean("user", User.class);
System.out.println(user);
}
2.5 构造函数参数名称
<!--2.3 构造函数参数名称-->
<!--参数索引:
前提:当我们已经知道传入的参数的参数名(标识符)
重点:参数名、变量名,有多少个参数就必须注入多少个值,如User中有3个参数,那么必须对应有3个<constructor-arg name="" value=""/>
name:参数名,自定义属性名又称变量名、标识符
value:赋值
-->
<bean id="user" class="com.riove.pojo.User">
<constructor-arg name="id" value="123456"/>
<constructor-arg name="username" value="admin"/>
<constructor-arg name="password" value="admin"/>
</bean>
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
*2.通过容器,我们可以做我们想做的任何业务,比如:获取bean对象
*
*Tip:getBean();这个方法可以传入多个属性值
* 当我们知道该对象返回值类型,只需要在传入的参数对应对象类型,这样就不用强转了
*/
User user = context.getBean("user", User.class);
System.out.println(user);
}
3.基于Setter的依赖注入
3.1 实体类(Student.java)
package com.riove.pojo;
import com.riove.dao.UserDaoImpl;
import java.util.*;
/**
* @Author Riove
* @Create 2021/6/28 20:05
*/
public class Student {
//基本类型:int/string/double...
private String name;
//数组类型
private String[] books;
//对象类型
private UserDaoImpl userDao;
//集合类型
private List<String> list;
//map类型
private Map<String,String> map;
//set类型
private Set<String> set;
//properties类型
private Properties properties;
public Student() {
}
public Student(String name, String[] books, UserDaoImpl userDao, List<String> list, Map<String, String> map, Set<String> set, Properties properties) {
this.name = name;
this.books = books;
this.userDao = userDao;
this.list = list;
this.map = map;
this.set = set;
this.properties = properties;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public UserDaoImpl getUserDao() {
return userDao;
}
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", books=" + Arrays.toString(books) +
", userDao=" + userDao +
", list=" + list +
", map=" + map +
", set=" + set +
", properties=" + properties +
'}';
}
}
3.2 Setter的依赖注入
<!--基于Setter的依赖注入-->
<bean id="student" class="com.riove.pojo.Student">
<!--
1.基本类型:int/string/double...
name:属性名
value:赋值
-->
<property name="name" value="张三"/>
<!--
2.数组类型
name:属性名
array:数组类型
value-type:数据类型
value:赋值
-->
<property name="books">
<array value-type="java.lang.String">
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--
3.对象类型
name:属性名
ref:对象类型
-->
<property name="userDao" ref="userDaoImpl"/>
<!--
4.集合类型
name:属性名
list:集合类型
value-type:泛型信息
value:赋值
-->
<property name="list">
<list value-type="java.lang.String">
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<!--
5.map类型
name:属性名
map:集合类型
key-type:键的类型
value-type:值得类型
entry:(key,value)
-->
<property name="map">
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="user1" value="张三"/>
<entry key="user2" value="李四"/>
<entry key="user3" value="王五"/>
</map>
</property>
<!--
6.set类型
name:属性名
set:set类型
value-type:类型
value:赋值
-->
<property name="set">
<set value-type="java.lang.String">
<value>张三</value>
<value>李四</value>
</set>
</property>
<!--
7.properties类型
name:属性名
props:properties类型
prop:
key:
-->
<property name="properties">
<props>
<prop key="driver">com.mysql.Driver</prop>
<prop key="url">jdbc:mysql:3306//localhost/database</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
@Test
public void test3() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
*2.通过容器,我们可以做我们想做的任何业务,比如:获取bean对象
*
*Tip:getBean();这个方法可以传入多个属性值
* 当我们知道该对象返回值类型,只需要在传入的参数对应对象类型,这样就不用强转了
*/
Student student = context.getBean("student", Student.class);
System.out.println(student);
}