文章目录
33.3 控制反转-Inversion Of Control
33.3.1 存哪去?
- 分析: 由于我们是很多对象,肯定要找个集合来存。这时候有 Map 和 List 供选择。 到底选 Map 还是 List就看我们有没有查找需求。有查找需求,选 Map。
- 所以我们的答案就是
- 在应用加载时,创建一个 Map,用于存放三层对象。 我们把这个 map 称之为容器。
33.3.2 还是没解释什么是工厂?
- 工厂 就是负责给我们从容器中获取指定对象的类。这时候我们获取对象的方式发生了改变。
- 原来: 我们在获取对象时,都是采用 new 的方式。是主动的。
- 现在: 我们获取对象时,同时跟工厂要,有工厂为我们查找或者创建对象。是被动的。
- 这种被动接收的方式获取对象的思想就是控制反转,它是 spring 框架的核心之一。
- 控制反转(IOC) :控制反转把创建对象的权利交给框架,是框架的重要特征,并非面向对象编程的专业术语。它包括依赖注入(Dependency Injection,简称DI)和以来查找(Dependency Lookup)。
- 明确 ioc 的作用: 削减计算机程序的耦合(解除我们代码中的依赖关系)。
33.4 使用 spring 的 IOC 解决程序耦合
33.4.1 前期准备
-
案例 :账户的业务层和持久层的依赖关系解决。
- 在开始 spring 的配置之前,我们要先准备 一下环境。由于我们是使用 spring解决依赖关系,并不是真正的要做增删改查操作,所以此时我们没必要写实体 类。并且我们在此处使用的是 java 工程,不是 java web工程。
-
官网:
-
下载地址: http://repo.springsource.org/libs-release-local/org/springframework/spring
-
创建业务层接口和实现类
package com.itheima.service;
/**
* 账户业务层的接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao ;
public AccountServiceImpl(){
System.out.println("对象创建了");
}
public void saveAccount(){
accountDao.saveAccount();
}
}
- 创建持久层接口和实现类
package com.itheima.dao;
/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 模拟保存账户
*/
void saveAccount();
}
package com.itheima.dao.impl;
import com.itheima.dao.IAccountDao;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
public void saveAccount(){
System.out.println("保存了账户");
}
}
- 导入spring依赖
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xww</groupId>
<artifactId>day33_spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
- 在resources路径下创建一个任意名称的 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">
</beans>
- 让 spring 管理资源,在配置文件中配置 service 和 dao
<?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">
<!--把对象的创建交给spring来管理-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>
- 测试配置是否成功
/**
* 模拟一个表现层
*/
public class Client {
/**
* 使用 main 方法获取容器测试执行
* */
public static void main(String[] args) {
//1.使用 ApplicationContext 接口,就是在获取 spring 容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据 bean 的 id 获取对象
IAccountService aService = (IAccountService) ac.getBean("accountService");
System.out.println(aService);
IAccountDao aDao = (IAccountDao) ac.getBean("accountDao");
System.out.println(aDao);
}
}
33.5 ApplicationContext的三个常用实现类
- ClassPathXmlApplicationContext: 它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)
- FileSystemXmlApplicationContext: 它可以加载磁盘任意路径下的配置文件(必须有访问权限)
- AnnotationConfigApplicationContext: 它是用于读取注解创建容器的。
33.6 BeanFactory 和 ApplicationContext 的区别
- ApplicationContext: 单例对象适用
- 它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
- BeanFactory: 多例对象使用
- 它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
33.7 实例化 Bean 的三种方式
33.7.1 第一种方式:使用默认无参构造函数
<!--在默认情况下:
它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"/>
33.7.2 第二种方式:spring 管理静态工厂-使用静态工厂的方法创建对象
/**
* 模拟一个静态工厂,创建业务层实现类
*/
public class StaticFactory {
public static IAccountService createAccountService(){
return new AccountServiceImpl();
}
}
<!-- 此种方式是:
使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器
id 属性:指定 bean 的 id,用于从容器中获取
class 属性:指定静态工厂的全限定类名
factory-method 属性:指定生产对象的静态方法
-->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="createAccountService"></bean>
33.7.3 第三种方式:spring 管理实例工厂-使用实例工厂的方法创建对象
/**
* 模拟一个实例工厂,创建业务层实现类
* 此工厂创建对象,必须现有工厂实例对象,再调用方法
*/
public class InstanceFactory {
public IAccountService createAccountService(){
return new AccountServiceImpl();
}
}
<!-- 此种方式是:
先把工厂的创建交给 spring 来管理。
然后在使用工厂的 bean 来调用里面的方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。
-->
<bean id="instancFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instancFactory" factory-method="createAccountService"></bean>
33.8 IOC 中 bean 标签和管理对象细节
33.8.1 bean 标签
- 作用:
- 用于配置对象让 spring 来创建的。
- 默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
- 属性:
- id:给对象在容器中提供一个唯一标识。用于获取对象。
- class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
- scope:指定对象的作用范围。
- singleton :默认值,单例的.
- prototype :多例的.
- request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
- session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
- global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么globalSession 相当于 session.
- init-method:指定类中的初始化方法名称。
- destroy-method:指定类中销毁方法名称。
33.8.2 bean 的作用范围和生命周期
- 单例对象:
scope="singleton"
- 一个应用只有一个对象的实例。它的作用范围就是整个引用。
- 生命周期:
- 对象出生:当应用加载,创建容器时,对象就被创建了。
- 对象活着:只要容器在,对象一直活着。
- 对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
- 多例对象:
scope="prototype"
- 每次访问对象时,都会重新创建对象实例。
- 生命周期:
- 对象出生:当使用对象时,创建新的对象实例。
- 对象活着:只要对象在使用中,就一直活着。
- 对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。
33.9 spring 的依赖注入
33.9.1 依赖注入的概念
- 依赖注入: Dependency Injection。它是 spring 框架核心 ioc 的具体实现。
- 能注入的数据:有三类
- 基本类型和String
- 其他bean类型(在配置文件中或者注解配置过的bean)
- 复杂类型/集合类型
- 注入的方式:有三种
- 第一种:使用构造函数提供
- 第二种:使用set方法提供
- 第三种:使用注解提供
- 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。 ioc解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。
- 那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。
- 简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
33.9.2 构造函数注入
- 要求: 类中需要提供一个对应参数列表的构造函数。
- 涉及的标签: constructor-arg
- 属性:
- index:指定参数在构造函数参数列表的索引
- type:指定参数在构造函数中的数据类型
- name:指定参数在构造函数中的名称 用这个找给谁赋值
- value:它能赋的值是基本数据类型和 String 类型
- ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
<bean id="accountService" class="com.xww.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="你好"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
33.9.3 set 方法注入
- 通过配置文件给 bean 中的属性传值: 使用 set 方法的方式
- 涉及的标签: property
- 属性:
- name:找的是类中 set 方法后面的部分
- ref:给属性赋值是其他 bean 类型的
- value:给属性赋值是基本数据类型和 string 类型的
<bean id="accountService2" class="com.xww.service.impl.AccountServiceImpl2">
<property name="name" value="tetst" ></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
33.9.4 注入集合属性
- 给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。
- List 结构的: array,list,set
- Map 结构的: map,entry,props,prop
package com.itheima.service.impl;
import com.itheima.service.IAccountService;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Map;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl3 implements IAccountService {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
public void saveAccount(){
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
}
}
<bean id="accountService3" class="com.xww.service.impl.AccountServiceImpl3">
<property name="myStrs">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<property name="myList">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<property name="mySet">
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
<property name="myMap">
<props>
<prop key="testC">ccc</prop>
<prop key="testD">ddd</prop>
</props>
</property>
<property name="myProps">
<map>
<entry key="testA" value="aaa"></entry>
<entry key="testB">
<value>BBB</value>
</entry>
</map>
</property>
</bean>