OCP原则:实现对扩展开发,对修改封闭。在扩展时尽量不要修改源代码。
一、IOC
IOC(Inverse of Control)控制反转,将原本在程序中手动创建,如Service类实例的控制权,交给spring框架管理。简单来说,就是对象创建的控制权反转到了spring框架。只需要通过IOC获得对象的实例,将IOC当成一个黑盒子、工厂。
不使用ioc,代码中创建一个对象直接操作接口实现类,并没有面向接口开发。
public class Controller{
//直接在controller中实例化一个service接口的实例
Service service = new ServiceImpl();
}
面向接口开发:调用接口的方法,只面向接口而不面向接口实现类,因为一个接口可能有多个实现类。
没有面向接口开发的问题:调用接口的类和接口实现类之间存在直接耦合。
解决方法:
将调用接口的类和接口实现类要解耦合。
可以通过将创建接口实现类对象的工作交给工厂来作。
1、IOC的底层实现
spring提供ioc容器,对 bean进行实例化。使用bean时候从容器中取。
springIOC底层实现
通过工厂+反射+配置文件实现
<!--配置文件-->
<bean id="" class=""/>
//工厂
public class BeanFactory{
/*
解析配置文件后,通过id查找配置文件中对应的class
再实现反射返回对应的bean实例
*/
public static Object getBean(String id){
//查找配置文件中的bean的class
//反射实现实例化bean
return ...
}
}
2、IOC的实现例子
编写接口和接口实现类
package com.zqh.service;
public interface UserService {
void save();
}
package com.zqh.service;
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("save");
}
}
配置applicationContext.xml
在里面配置spring管理的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="userService" class="com.zqh.service.UserServiceImpl"></bean>
</beans>
测试类
将bean的实例化交由spring来执行,通过配置文件bean 的id到IOC容器拿到bean的实例化对象,解决上述没有面向接口开发的问题。
package com.zqh.test;
import com.zqh.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserController {
@Test
public void test(){
// 创建spring容器的实例
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过容器实例对象获取bean实例
// 通过 bean的名称来获取
UserService userService = (UserService) applicationContext.getBean("userService");
userService.save();
}
}
3、ApplicationContext
ApplicationContext理解为spring容器的上下文,通过上下文操作容器中bean。
ClassPathXmlApplicationContext:加载classpath下的配置文件创建一个容器实例
FileSystemXmlApplicationContext:加载文件系统中任意目录 下的配置文件,创建一个容器实例
多文件的加载方法
//加载多个配置文件
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});
//通过统配符号加载
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean*.xml");
//两者合一
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","classpath:bean*.xml"});
二、DI
DI(Dependency Injection) 依赖注入,就是在spring创建某个bean对象的过程中,将这个bean对象所依赖的属性注入进去。
1、控制反转与属性注入
控制反转,哪些对象被反转,获得依赖对象的过程被反转了。
controller:调用service,controller依赖service,在controller中所依赖的service创建被反转到spring容器。
service:依赖dao,依赖的dao创建被反转到spring容器
2、如何实现注入
如service依赖dao:
1、spring要管理service(前提)
2、spring要管理dao(前提)
总结前提:依赖方(service)、被依赖方(dao)都需要被spring管理
3、根据依赖关系,service依赖dao,将dao实例注入至service的属性中。
底层原理:spring根据配置文件中配置依赖关系,首先获取被依赖的对象dao实例,调用service对象中set方法将dao实例设置(注入)到service属性。
3、依赖注入的方式
三、IOC和DI的区别
IOC:控制反转,将对象的创建权反转到ioc容器。
DI:依赖注入,将对象所依赖的对象注入到对象的属性中。就是IOC的具体 实现方法。
1、IOC就是一个容器
2、IOC容器中包括spring管理的所有bean。
3、IOC容器负责对bean进行实例化
4、IOC容器对bean进行实例化时候,检查有哪些依赖的属性,将依赖的属性注入到实例化的bean的属性中。
要实现依赖注入,需要spring管理依赖方和被依赖方(spring要对依赖方和被依赖方实例化)。