IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”,还有些书籍翻译成为“控制反向”或者“控制倒置”。
IOC是为了解决对象之间的耦合度过高的问题,实现对象之间的“解耦”
IOC是 控制反转,反转了什么?实际是获得依赖对象的过程被反转了。
反转前:需要编写程序代码自主创建,并且先创建依赖对象,对象之间关系紧密。
反转后:对象之间的依赖关系由IOC容器动态管理,且需要对象也由IOC容器负责。
降低耦合度;
有助于团队成员分工明确、责任明晰,容易将一个大的
任务划分为细小的任务;提高可复用性;
维护简单,只需要修改配置即可。
问题引入:
首先创建两个对象:
Student.java
package com.entity;import java.util.Date;/** * Created by Tired on 2018/4/3. */public class Student { private String number="";//学号 private String name="";//姓名 private char sex;//性别 private StudentClass myClass=null;//班级 private Date birthday;//出生年月 public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public StudentClass getMyClass() { return myClass; } public void setMyClass(StudentClass myClass) { this.myClass = myClass; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
StudentClass.java
package com.entity;/** * Created by Tired on 2018/4/3. */public class StudentClass { private String id="";//班级编号 private String name="";//班级名称 private int count=0;//班级人数 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
不用IOC的常规方法
/*常规方法*/ StudentClass myclass=new StudentClass();//实例化班级对象 myclass.setId("001"); myclass.setName("16软件技术1班"); myclass.setCount(0); Student stu=new Student(); stu.setName("Tom"); stu.setNumber("001"); stu.setSex('男'); stu.setBirthday(Date.valueOf("2000-1-1")); stu.setMyClass(myclass);
托管到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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="com.entity.StudentClass" id="myclass"> <property name="id" value="001"></property> <property name="name" value="16软件技术1班"></property> <property name="count" value="0"></property> </bean> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="stu" class="com.entity.Student"> <property name="number" value="001"></property> <property name="name" value="Tom"></property> <property name="sex" value="男"></property> <property name="birthday"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2000-01-01" /> </bean> </property> <property name="myClass" ref="myclass"></property> </bean> <bean id="stuService" class="com.biz.StudentServiceImpl" /> <bean id="watch" class="com.aop.WatchAop" /> <bean id="hello" class="com.aop.HelloWorldAspect" /> <!--AOP注解配置--> <context:component-scan base-package="com.aop"></context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true"/> <!--AOP配置 <aop:config proxy-target-class="true"> <aop:aspect id="mywatch" ref="watch"> <aop:pointcut id="display" expression="execution(* com.biz.StudentServiceImpl.display())" /> <aop:before method="before" pointcut-ref="display" /> <aop:after method="after" pointcut-ref="display" /> <aop:around method="around" pointcut-ref="display"/> <aop:after-throwing method="exception" pointcut-ref="display"/> </aop:aspect> <aop:aspect id="mywatch1" ref="watch"> <aop:pointcut id="save" expression="execution(* com.biz.StudentServiceImpl.save())" /> <aop:after-returning method="returning" pointcut-ref="save" returning="result"/> </aop:aspect> </aop:config>--></beans>
测试类:
public static void ioc() { /*IOC方法调用*/ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) context.getBean("stu"); System.out.println("学生姓名:" + student.getName()); }
作者:TiredHu
链接:https://www.jianshu.com/p/5dc0c4109235