1. 什么是 IOC
控制反转(IoC=Inversion of Control),控制反转(或依赖注入) ,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IOC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
下面举一个测试的demo供大家理解,代码如下:
假如有个接口IUserBiz,在这个接口中要实现一个方法,需要调用UserBizImpl中的doSomething()方法来实现。
我们的正常做法是在IUserBiz中先new UserBizImpl(),然后再调用UserBizImpl中相关方法。
正常做法的代码如下:
package com.practice.ioc.web;
import com.practice.ioc.biz.IUserBiz;
import com.practice.ioc.biz.impl.UserBizImpl;
public class UserAction {
private IUserBiz ub = new UserBizImpl();
public void thing1() {
this.ub.doSomeThing();
}
}
而IoC的思想即是,在你需要依赖对象的时候,IoC容器即可提供一个已经实例化好的对象给你,这样你就不需要自己去新建相应的依赖类。
看下面这段代码,来感受下IoC的过程:
package com.practice.ioc.web;
import com.practice.ioc.biz.IUserBiz;
import com.practice.ioc.biz.impl.UserBizImpl;
public class UserAction {
private IUserBiz ub;
public IUserBiz getUb() {
return ub;
}
public void setUb(IUserBiz ub) {
this.ub = ub;
}
public void thing1() {
this.ub.doSomeThing();
}
}
IUserBiz中声明的 ub 无需再实例化,但是要提供set方法
2.在spring的配置文件spring-context.xml中配置FileDaoImpl 与FileService(spring-context.xml配置文件是自己创建的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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byName"
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.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- bean Javabean实体 -->
<bean class="com.practice.ioc.biz.impl.UserBizImpl" id="userBizImpl"></bean>
<bean class="com.practice.ioc.web.UserAction" id="userAction">
<!-- name为当前实体属性名,ref依赖的实体的id
通过setter方法注入
-->
<property name="ub" ref="userBizImpl"></property>
</bean>
</beans>
测试方法调用IUserBiz的doSomething方法:
package com.practice.ioc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.practice.ioc.web.ClzAction;
import com.practice.ioc.web.CustomerAction;
import com.practice.ioc.web.StudentAction;
import com.practice.ioc.web.UserAction;
public class IocTest {
public static void main(String[] args) {
//通过对spring-context.xml进行建模,获取spring上下文
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
//获取JavaBean实体,通过文件配置的ID获取
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.thing1();
}
}
2. 三种注入方式(这里使用另一个测试StudentAction)
· set方法注入
上面的实例用的就是set方法注入
· 构造方法注入
StudentAction.java中将set方法改为构造函数
package com.practice.ioc.web;
import java.util.List;
public class StudentAction {
/*
* setter/getter方式注入
* 1.基本数据类型注入 2.集合注入 3.对象注入
*/
private Integer sid;//基本数据类型的引用类型
private String sname;//基本数据类型
private List<String> hobbies;//hobbies复数形式,改Y为I再加es
/*public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}*/
public StudentAction() {
super();
}
public StudentAction(Integer sid, String sname, List<String> hobbies) {
super();
this.sid = sid;
this.sname = sname;
this.hobbies = hobbies;
}
public void test() {
System.out.println(sid+"你的名字是:"+sname+"\n爱好:"+hobbies);
}
}
spring-context.xml中将property标签改为constructor-arg标签 (xml文件放在最下面)
· 自动装配
注:StudentAction.java中需要提供set方法
spring-context.xml中的beans标签中设置 default-autowire 属性,默认值为default,注释中对default-autowire的值有解释:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byName"
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.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- bean Javabean实体 -->
<bean class="com.practice.ioc.biz.impl.UserBizImpl" id="userBizImpl"></bean>
<bean class="com.practice.ioc.biz.impl.UserBizImpl2" id="userBizImpl2"></bean>
<bean class="com.practice.ioc.web.UserAction" id="userAction">
<!-- name为当前实体属性名,ref依赖的实体的id -->
<property name="ub" ref="userBizImpl"></property>
</bean>
<bean class="com.practice.ioc.web.CustomerAction" id="customerAction">
<property name="ub" ref="userBizImpl2"></property>
</bean>
<bean class="com.practice.ioc.web.StudentAction" id="studentAction">
<!-- 构造注入 通过实体提供的有无参构造器获取值 -->
<constructor-arg name="sid" value="76"></constructor-arg> <constructor-arg
name="sname" value="龙"></constructor-arg> <constructor-arg name="hobbies">
<list> <value>爱三叶</value> <value>恒心</value> </list> </constructor-arg>
<!-- setter/getter注入 通过提供属性的S/G方法获取值 -->
<!-- <property name="sid" value="67"></property> <property name="sname"
value="三叶"></property> <property name="hobbies"> <list> <value>爱龙</value>
<value>恒心</value> </list> </property> -->
</bean>
<!-- 自动装配 为解决一个实体实现多个接口的复杂配置问题 -->
<!-- byType 根据管理的Javabean的接口属性,在spring的上下文中自动寻找实现类去注入,当找到两个或两个以上时会报错
与spring上下文的ID无关 -->
<!-- byName 根据管理的Javabean的接口名,在spring上下文中去寻找同名的ID进行注入 -->
<bean class="com.practice.ioc.web.ClzAction" id="clzAction"></bean>
</beans>