IOC
1.概述
- IOC:Inversion of Control 控制反转,可以让容器负责对象的创建以及销毁操作,对象在容器中叫 bean
2.回顾问题
-
问题:写了太多与业务无关的代码
- 耦合度非常高,写了很多和业务无关的代码
- 不利于项目的升级迭代
-
思考的解决方案
-
能够直接获取 mapper 接口,而不必去关心底层的获取方式
-
3.bean 配置
3.1.创建 spring01 项目
-
项目结构如下
-
添加 Spring 依赖
<dependencies> <!-- 添加 spring 依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.17.RELEASE</version></dependency> <!-- 添加 junit 依赖--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version> <!-- <scope>test</scope>--></dependency></dependencies>
3.2.添加 Student 类
-
创建空学生类
public class Student { }
3.3.添加 Spring 配置文件
-
添加方式如下
-
开始配置 javabean
<?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"> <!-- 配置 Student 学生类--><bean id="student" name="stu" class="cn.sycoder.domian.Student"></bean> </beans>
4.属性介绍
属性名称 | 说明 |
---|---|
id | 给 bean 起名字(定义id 不能重复) |
name | 给 bean 起别名 |
class | 类全限定类名 |
4.容器创建
4.1.ClassPathXmlApplicationContext
- 从classPath下寻找
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(context);
4.2.FileSystemXmlApplicationContext
- 从系统文件下寻找
ApplicationContext ctx = new FileSystemXmlApplicationContext("绝对路径地址");
-
使用 ClassPathXmlApplicationContext 获取方式会出现如下问题
5.从容器中获取 bean
5.1.根据id 获取
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//从容器中根据 id 获取 bean 对象
Student stu = (Student)context.getBean("student");//通过别名获取 bean 对象
Student stuByName = (Student)context.getBean("stu");
- 注意:如果id重复会有如下问题
5.2.根据id和类型
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
final Student student = context.getBean("student", Student.class);
5.3.根据类型获取bean
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
final Student bean = context.getBean(Student.class);
- 注意:使用类型获取的时候,一定要保证容器中只有一个 javabean 对象
5.4.注意点
-
bean 的配置spring 是使用反射调用对象的无参构造器实现的。所以必须提供无参构造器,否则会出现如下错误
6.设计模式
6.1.单例模式
-
概述:属于创建型模式,提供了创建对象的最佳方式。单例模式只能有一个单一的类
-
懒汉式单例模式:需要用的时候,再去把对象创建出来
public class SingleObject {//线程不安全的懒汉式单例模式private static SingleObject instance;public static SingleObject getInstance(){if(instance == null){instance =new SingleObject();}return instance;}}
-
饿汉式单例模式:不管你有没有,我先创建出来
public class SingleObjectE {//线程不安全的饿汉式单例模式private static SingleObjectE instance = new SingleObjectE();public static SingleObjectE getInstance(){return instance;} }
6.2.工厂模式
-
概述:也是属于创建型模式,目的也是提供创建对象的最佳方式
-
静态工厂
public class BeanFactory {public static Student getBean() {return new Student();}public static Object getBean(String name) {if ("Student".equals(name))return new Student();else if("SingleObject".equals(name)) {return new SingleObject();}else{return new Object();}}}
-
实例工厂
public class BeanFactory {public Object getBean(){return new Student();} }
6.3.通过反射获取 xml 配置创建对象、
-
模拟实现
IOC
获取对象 -
导依赖
<dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.3</version> </dependency> <dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.2.0</version> </dependency>
-
代码实现
public class XmlCreateBean {/*** 模拟spring 容器创建 javabean* @param configName* @param id* @return*/public static Object getBean(String configName,String id){final SAXReader saxReader = new SAXReader();try {final Document dom = saxReader.read(configName);final Element element = dom.getRootElement();final List<Element> beans = element.elements("bean");//获取每一个 beanfor (Element bean : beans) {if(id.equals(bean.attributeValue("id"))){final String aClass = bean.attributeValue("class");final Class<?> clz = Class.forName(aClass);return clz.newInstance();}}} catch (Exception e) {e.printStackTrace();}return null;} }
7. bean实例化
- bean 交给 spring 创建,底层究竟是怎么创建的?
- 实例化 bean 三种方式:
- 构造器(常用)
- 静态工厂方法
- 实例工厂方法
- 实现 FactoryBean(常用)
1.无参构造器实例化
-
新建 person 类,底层是通过 clz.getDeclaredClasses() 获取构造器
public class Person {public Person(){}}
-
配置 Person bean
<bean id="person" class="cn.sycoder.domian.Person"></bean>
-
从容器中获取 bean
@Testpublic void testConstructorInit(){final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Person p = (Person) context.getBean("person");}
-
修改构造器,添加参数测试,提示找不到无参构造器
2.静态工厂方法实例化
-
创建 clintServer 类,提供静态工厂方法
public class ClientServer {//创建自身对象并且私有化private static ClientServer clientServer = new ClientServer();private ClientServer() {}public static ClientServer createInstance(){return clientServer;} }
-
配置bean 的 xml
<bean id="clientServer" class="cn.sycoder.domian.ClientServer" factory-method="createInstance"></bean>
-
获取 bean
@Testpublic void testFactoryStaticMethodInit(){final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");final ClientServer bean = context.getBean(ClientServer.class);}
-
配置关系
3.实例工厂方法实例化
-
创建实例工厂类
public class ClientServiceFactory {private static ClientService instance = new ClientService();private ClientServiceFactory(){}public ClientService getInstance(){return instance;} }public class ClientService { }
-
配置 bean
<!-- 配置工厂--><bean id="clientFactory" class="cn.sycoder.domian.ClientServiceFactory"></bean> <!-- 配置 clientService--><bean id="clientService" factory-bean="clientFactory" factory-method="getInstance"></bean>
-
获取bean
@Testpublic void testFactoryInstanceMethodInit(){final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");final ClientService bean = context.getBean(ClientService.class);}
-
配置关系
4.FactoryBean
-
创建员工类
public class Employee {public void check(){System.out.println("检查是否能够拿到员工类对象");} }
-
创建员工 factory 类实现 FactoryBean
public class EmployeeFactory implements FactoryBean<Employee> {public Employee getObject() throws Exception {System.out.println("获取 emp 对象");return new Employee();}public Class<?> getObjectType() {return Employee.class;}public boolean isSingleton() {return false;} }
-
配置工厂类(并没有直接配置 emp 类)
<bean id="employee" class="cn.sycoder.domian.EmployeeFactory"></bean>
-
获取 emp 对象
@Testpublic void testFactoryBeanInit(){final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");final Employee emp = (Employee)context.getBean("employee");emp.check();}
-
实现方法说明
-
isSingleton:如果是 true 返回单例的对象
public boolean isSingleton() {return true;}
-
getObject:进行对象创建的
public Employee getObject() throws Exception {System.out.println("获取 emp 对象");return new Employee(); }
-