spring中的IOC与DI

IOC控制反转 Inversion Of Control

所谓IOD对于spring框架来说,就是由spring来负责控制对象的生命周期和对详见的关系。就比如找对象,原本我们需要了解对象的一些信息,然后new一个(或者从JNDI中查询一个),使用完之后还得将对象销毁,这里对象始终和其他类或接口耦合起来。
那么IOC就想一个婚介中心,管理了很多对象,我们可以向婚介中心提出想要怎么样的对象,如果婚介中心给我们的人选不符合要求,那么我们就会抛出异常。整个过程不再由我们自己控制。
就是所有的类都会在spring中登记,告诉spring你是个什么东西,需要什么东西,然后spring就会在系统运行到适当的时候,把你需要的东西给你,同时也把你交给其他需要你的东西。所有的类的创建、销毁都由spring控制,也就是说控制对象生命周期的不再是引用它的对象,而是spring。对于某个具体的对象而言,以前是它控制其他对象,现在是所有对象都被spring控制,这个就叫控制反转

IOC原理

其实就是反射+工厂模式
我们可以把IOC容器的工作模式看做是工厂模式的升华,可以把IOC容器看作是一个工厂,这个工厂里要生产的对象都在配置文件中给出定义,然后利用编程语言提供的反射机制,根据配置文件中给出的类名生成相应的对象。从实现来看,IOC是把以前在工厂方法里写死的对象生成代码,改变为由配置文件来定义,也就是把工厂和对象生成这两者独立分隔开来,目的就是提高灵活性和可维护性。
IOC是目的,DI是一种手段

IOC的好处

ioc思想最核心的就是,资源不由使用资源的双方管理,而由不使用资源的第三方管理

  1. 资源集中管理,实现资源的可配置和易管理
  2. 降低了使用资源双方的依赖程度,也就是耦合度
  3. 代码更容易复用
  4. 开发更方便组织分工
  5. 软件演化有更好的灵活性,能快速响应需求变化,维护代价更小
    窃以为你只需砥砺前行,专心做事,经磨炼锋宝剑,历苦寒香梅花,对象这种东西。。等分配吧orz
DI Dependency Injection 依赖注入

在系统运行过程中,动态的向某个对象注入它所依赖的对象。接上面的说法,婚介中心会在适当的时候给你一个对象,进入到你的生命中,这就是依赖注入。

DI的三种实现方式
  1. 设置注入—Setter Injection
  2. 构造方法注入—Constructor Injection
  3. 接口注入—Interface Injection

public class Person{
    private Dog dog;
    private Ingteger age;
    private String name;
    //setter
    //getter
}
<bean id="person" class="com.myspring.Person">
    <property name="age" value="18"/>
    <property name="name" value="胡八万"/>
    <property name="dog" ref="dog"/>
</bean>
<bean id="dog" class="com.myspring.Dog">
    <property name="name" value="二哈"/>
</bean>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
Person person=ctx.getBean("person",Person.class);

这种方法要求属性必须实现setter
ref=""表示对象的引用
ref用来指向其他的bean
空值标签
另外注入集合
< list >:注入一列值,允许重复
< set >:注入一列值,不允许重复
< map >:注入键(名)值对的集合,名称和值可以是任何类型
< props >:注入键(名)值对的集合,名称和值可以是任何类

private List list;
private Map map;
private Properties prop;
<bean id="mjc" class="com.myspring.JavaCollection">
    <property name="list">
        <!-- 注入集合 值可重复 set就不举例了-->
        <list>
            <value>list1</value>
            <value>list1</value>
        </list>
    </property>
    <property name="map">
        <!-- 注入map -->
        <map>
            <entry key="小红">
                <value>18</value>
            </entry>
        </map>
    </property>
    <property name="prop">
        <!-- 注入properties -->
        <props>
            <prop key="小李">男</prop>
        </props>
    </property>
</bean>

还有一种有趣的方式spring表达式,类似于EL表达式,可以读取一个bean对象或者集合中的内容:#{bean.属性}

<bean id="personClone" class="com.myspring.Person">
    	<property name="age" value="#{person.age}"/>
    	<property name="name" value="#{person.name}"/>
</bean>

public Person(Integer age,String name,Dog dog){
    this.age = age;
    //...
}
<bean id="person" class="com.oak.entity.Person">
    <constructor-arg index="1" value="八万"/>
    <constructor-arg index="2" value="18"/>
    <constructor-arg index="3" ref="dog"/>
</bean>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
Person person=ctx.getBean("person",Person.class);

构造方法中的参数必须与配置文件中的参数个数一直否则报错
由于java中不会维护形参的名称,所以使用name属性进行注入时可能会有风险

public Person(Integer age,String args0,Dog dog){}

Interface Injection
它是在一个接口中定义需要注入的信息,并通过接口完成注入。Apache Avalon是一个较为典型的接口注入形IOC容器
以用户注册为例,首先定义一个接口,它的用途是将一个返回一个Person实例

public interface PersonInterfaceInject {
   public Person getPerson();
}

同时,我们需要配置一个xml

<!-- Person的一个实例bean,定义每次返回不同的实例对象 -->
	<bean id="liangzai" class="com.myspring.Person" p:name="胡八万"
		p:age="18" scope="prototype" />
	<!-- 实施方法注入 -->
	<bean id="personInterfaceInject" class="com.myspring.PersonInterfaceInject">
		<lookup-method name="getPerson" bean="liangzai" /> 
	</bean>

最后测试一下

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");	
PersonInterfaceInject personInterfaceInject=applicationContext.getBean("personInterfaceInject",Person.class);
Person person1=personInterfaceInject.getPerson();
Person Person2=personInterfaceInject.getPerson();
System.out.println(person1);
System.out.println(person2);
System.out.println("person2==person1?:"+(person2==person1));
name:胡八万 age:18
name:胡八万 age:18
person2==person1?:false

最后输出可以看到person1和person2是不同实例
理解:“Lookup方法”可以使Spring替换一个bean原有的,获取其它对象具体的方法,并自动返回在容器中的查找结果。

IOC自动化装配

set注入和构造注入有时在做配置时比较麻烦。所以框架为了提高开发效率,提供自动装配功能,简化配置。Spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中标签的autowire属性。自动装配属性有5个值可选,分别代表不同的含义。

  1. byName
    从Spring环境中获取目标对象时,目标对象中的属性会根据名称在整个Spring环境中查找标签的id属性值。如果有相同的,那么获取这个对象,实现关联。
    整个Spring环境:表示所有的spring配置文件中查找,那么id不能有重复的。
  2. byType
    从Spring环境中获取目标对象时,目标对象中的属性会根据类型在整个spring环境中查找标签的class属性值。如果有相同的,那么获取这个对象,实现关联。
    缺点:如果存在多个相同类型的bean对象,会出错。
    如果属性为单一类型的数据,那么查找到多个关联对象会发生错误。
    如果属性为数组或集合(泛型)类型,那么查找到多个关联对象不会发生异常。
  3. constructor(3以上已不能用)
    使用构造方法完成对象注入,其实也是根据构造方法的参数类型进行对象查找,相当于采用byType的方式
    如果没找到则抛出异常
  4. autodetect
    自动选择:如果对象没有无参数的构造方法,那么自动选择constructor的自动装配方式进行构造注入。如果对象含有无参数的构造方法,那么自动选择byType的自动装配方式进行setter注入。
  5. no
    默认情况下不自动装配
学生类
public class Student {
	private String ID;
	private String name;
	private int age;
	private String sex;
    //setter getter
}
教师类
public class Teacher {
	private String name;
	private int age;
	private String sex;
    //setter getter
}
教学档案类
public class TeachFile {
	private Teacher teacher;
	private Student student;
	public TeachFile() {
	}
	public TeachFile(Teacher teacher, Student student) {
		this.teacher = teacher;
		this.student = student;
	}
	public void print() {
		System.out.println("------教师信息------");
		System.out.println("姓名:" + teacher.getName());
		System.out.println("年龄:" + teacher.getAge());
		System.out.println("性别:" + teacher.getSex());
		System.out.println();
		System.out.println("------学生信息------");
		System.out.println("学号:" + student.getID());
		System.out.println("姓名:" + student.getName());
		System.out.println("年龄:" + student.getAge());
		System.out.println("性别:" + student.getSex());
	}
    //setter getter
}
XML中对TeachFile自动装配
<bean id="student" class="com.autobean.Student">
    <property name="ID" value="80" /> 
    <property name="name" value="阿王" /> 
    <property name="age" value="23" /> 
    <property name="sex" value="男" /> 
</bean>
<bean id="teacher" class="com.autobean.Teacher">
    <property name="name" value="何老师" /> 
    <property name="age" value="43" /> 
    <property name="sex" value="女" /> 
</bean>
<!-- 默认情况下,通过'ref’来装配bean -->
<bean id="teachFile1" class="com.autobean.TeachFile" >
    <property name="teacher" ref="teacher" />
    <property name="student" ref="student" />
</bean>
<!--根据byName自动装配bean -->
<bean id="teachFile2" autowire="byName"  class="com.autobean.TeachFile" />
<!--根据byType自动装配bean -->
<bean id="teachFile3" autowire="byType" class="com.autobean.TeachFile" />
  <!--根据constructor自动装配bean -->
<bean id="teachFile4" autowire="constructor" class="com.autobean.TeachFile"/>
测试
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(res);
TeachFile tf1 = (TeachFile) bf.getBean("teachFile1");
TeachFile tf2 = (TeachFile) bf.getBean("teachFile2");
TeachFile tf3 = (TeachFile) bf.getBean("teachFile3");
TeachFile tf4 = (TeachFile) bf.getBean("teachFile4");
System.out.println("默认情况下,通过'ref’来装配bean");
tf1.print();
System.out.println("根据byName自动装配bean");
tf2.print();
System.out.println("根据byType自动装配bean");
tf3.print();
System.out.println("根据constructor自动装配bean");
tf4.print();
输出:
默认情况下,通过'ref’来装配bean
------教师信息------
姓名:何老师
年龄:43
性别:女

------学生信息------
学号:80
姓名:阿王
年龄:23
性别:男
根据byName自动装配bean
------教师信息------
姓名:何老师
年龄:43
性别:女

------学生信息------
学号:80
姓名:阿王
年龄:23
性别:男
根据byType自动装配bean
------教师信息------
姓名:何老师
年龄:43
性别:女

------学生信息------
学号:80
姓名:阿王
年龄:23
性别:男
根据constructor自动装配bean
------教师信息------
姓名:何老师
年龄:43
性别:女

------学生信息------
学号:80
姓名:阿王
年龄:23
性别:男
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值