目录
核心概念
- IoC(Inversion of Control)控制反转 —— 对象控制权的转移发生反转
使用对象时,在程序中不需要主动 new 产生对象而由外部提供对象,将对象的控制权由程序转移到外部,这种思想称之为控制反转。
Spring提供了一个容器,称为IoC容器(Spring容器),用来充当IoC思想中的“外部”。
IoC容器负责管理放在其中对象的创建和初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean。
- DI(Dependency Injection)依赖注入
在容器中建立的Bean与Bean之间的依赖关系的整个过程称为依赖注入。
目标:充分解耦
- 使用IoC容器管理Bean(IoC)
- 在IoC容器内将有依赖关系的Bean进行关系绑定(DI)
最终效果
使用对象时不仅可以直接从IoC容器中获取,并且获取到的Bean已经绑定了所有的依赖关系。
IoC入门案例分析
- IoC管理什么?( Service与Dao )
- 如何将被管理的对象告知IoC容器?( 配置 )
- 被管理的对象交给IoC容器,如何获取到IoC容器?( ApplicationContext接口 )
- IoC容器得到后,如何从容器中获取Bean?( 接口方法——getBean() )
【示例】
定义Spring管理的类(接口):
导入Spring的Maven依赖:
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
创建Spring的配置文件并将配置对应类告知IoC容器——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">
<!--1、导入Spring的坐标依赖:spring-context-->
<!--
2、配置bean——使用bean标签
注意:不是配置接口,而是配置实现类
bean标签的作用:用于配置让Spring容器创建对象
class : 要创建对象的全限定类名
————这里使用了name属性,也可以使用id————
name: 该对象在容器中的名字
id: 一个bean的唯一标识 , 命名格式必须符合XML ID属性的命名规范
name: 可以用特殊字符,并且一个bean可以用多个名称,例如 name="bean1,bean2,bean3"
如果没有id,则name的第一个名称默认是id
如果 一个 bean 标签未指定 id、name 属性,则 spring容器会给其一个默认的id,值为其类全名
如果有多个bean标签未指定 id、name 属性,但是class相同,则spring容器会按照其出现的次序,
分别给其指定 id 值为 “类全名#1”, “类全名#2”
-->
<bean id="bookMapper" class="com.xushilin.mapper.impl.BookMapperImpl"></bean>
<bean id="bookService" class="com.xushilin.service.impl.BookServiceImpl"></bean>
</beans>
致此,需要被管理的对象已经交给了IoC容器。
要想拿bean,得先拿容器,接下来就是初始化IoC容器(Spring核心容器/Spring容器),通过容器获取bean:
package com.xushilin;
import com.xushilin.mapper.BookMapper;
import com.xushilin.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App2 {
public static void main(String[] args) {
//3、获取IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//4、获取bean
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.save();
}
}
DI入门案例分析
- 基于IoC容器管理bean。
- 业务层中使用new形式创建的Dao对象是否保留?(写上实现类,耦合度必然高,因此,不能保留)
- 业务层中需要的Dao对象如何进入到业务层中?(提供set方法)
- 业务层中与Dao层的关系如何描述?(配置bean的属性property)
【示例】
删除业务层中使用new的方式创建的Dao对象:
//5、删除业务层中使用new的方式创建的Dao对象
//private BookMapper bookMapper = new BookMapperImpl();
private BookMapper bookMapper;
业务层中需要的Dao对象如何进入到业务层中?——在业务层实现类中提供一个对应的set方法:
public class BookServiceImpl implements BookService {
//5、删除业务层中使用new的方式创建的Dao对象
//private BookMapper bookMapper = new BookMapperImpl();
private BookMapper bookMapper;
//6、提供对应的set方法
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
}
到此,Dao对象进入到了业务层中!
接下来,描述业务层中与Dao层的关系:
<!--因为是Dao对象进入到业务层中,所以在此处我们要对service进行修改-->
<bean id="bookService" class="com.xushilin.service.impl.BookServiceImpl">
<!--配置业务层与Dao的关系-->
<!--property标签表示配置当前bean的属性-->
<!--name属性表示配置哪一个具体的属性-->
<!--ref属性表示参照哪一个bean-->
<property name="bookMapper" ref="bookMapper"></property>
</bean>