快速了解SpringIOC
文章目录
1. 概述
IOC(inversion of control)的中文翻译是"控制反转",它的作用是将对象的创建反转(转交)给spring框架来创建和管理,相当于一个容器.
控制反转,反转的是对象的创建工作.除了使用new的方式创建对象,在使用了springIOC之后,把对象的创建工作交给springIOC创建管理,我们只需要从springIOC拿即可,不需再自己new.
IOC的作用:削减计算机程序的耦合,解除代码中的依赖.
IOC的核心思想:
1.通过读取配置文件反射创建对象;
2.把已创建好的对象都存起来,当我们下次使用时可以直接从存储的位置获取.
2. 案例详解
2.1案例需求:
需求:账户的业务层和持久层的依赖关系解决(不再从service层new出到dao层的实例对象,从IOC容器获取)
思路(模拟,不创建实体类):
1.创建maven工程,导入依赖
2.创建接口和实现类;
3.配置文件;
4.测试.
2.2案例实现
-
创建maven工程
<groupId>com.itti</groupId> <artifactId>SpringIOCTest</artifactId> <version>1.0-SNAPSHOT</version>
-
引入依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
-
创建接口和实现类
- AccountDao.java
public interface AccountDao { /** * 保存账户(模拟) */ void save(); }
- AccountDaoImpl.java
public class AccountDaoImpl implements AccountDao { /** * 保存账户(模拟) */ @Override public void save() { System.out.println("AccountDaoImpl---saveAccount()"); } }
- AccountService.java
public interface AccountService { /** * 操作dao层 保存账户 */ void daoSave(); }
- AccountServiceImpl.java
public class AccountServiceImpl implements AccountService { /** * 保存账户 */ @Override public void daoSave() { //创建容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); //根据id创建对象 AccountDao accountDao = (AccountDao)applicationContext.getBean("accountDao"); //调用方法 accountDao.save(); } }
-
在resources包下创建spring的配置文件(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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--注册Service--> <bean id="accountService" class="com.itti.service.impl.AccountServiceImpl"></bean> <!--注册Dao--> <bean id="accountDao" class="com.itti.dao.impl.AccountDaoImpl"></bean> </beans>
-
测试
public class SpringIOCTest { /*** * 使用SpringIOC容器创建对象实例 * @throws Exception */ @Test public void testSpringContext() throws Exception { //1. 创建容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); //2. 根据id获得对象 AccountService accountService = (AccountService) applicationContext.getBean("accountService"); accountService.daoSave(); } }
-
控制台输出
2.3 分析
service层调用dao层不再通过new实例,通过IOC容器取出,实现了解耦.
3. Spring基于xml的IOC相关知识详解
3.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">
<!--注册Service-->
<bean id="accountService" class="com.itti.service.impl.AccountServiceImpl"></bean>
<!--注册Dao-->
<bean id="accountDao" class="com.itti.dao.impl.AccountDaoImpl"></bean>
</beans>
-
id/name属性:
用于标识bean,其实id 和 name都必须具备唯一标识,两种用哪一种都可以. 但是一定要唯一、 一般开发中使用id来声明.
-
class属性: 用来配置要实现化类的全限定名
-
scope属性: 用来描述bean的作用范围
singleton: 默认值,单例模式。spring创建bean对象时会以单例方式创建。(默认)
prototype: 多例模式。spring创建bean对象时会以多例模式创建
request: 针对Web应用。spring创建对象时,会将此对象存储到request作用域
session: 针对Web应用。spring创建对象时,会将此对象存储到session作用域
- init-method属性:spring为bean初始化提供的回调方法
- destroy-method属性:spring为bean销毁时提供的回调方法. 销毁方法针对的都是单例bean , 如果想销毁bean , 可以关闭容器
3.2 bean的作用范围和生命周期
-
单例对象: scope=“singleton”
一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:
对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。 -
多例对象: scope=“prototype”
每次访问对象时,都会重新创建对象实例。
生命周期:
对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。
对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了.