spring IOC理解

spring IoC理解

什么是IoC

IoC: Inversion of Control,控制反转

控制反转:

  • 控制
  • 反转

官网中定义:

IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.
Ioc也被称为依赖注入(DI)。对象依赖过程的定义:和其他对象一起使用时,对象被创建后,只能通过构造参数、工厂方法的参数或者是属性值设置到对象对象实例,或者从工厂方法返回。而在创建Bean的时候,spring容器负责代替了注入了这种依赖关系。这样的一个过程被定义为反转(inverse)

即:

  • 控制: 控制了创建对象角色能力,如创建张三、李四或者不允许创建
  • 反转: 对象之间依赖关系的注入由spring容器负责

举个栗子:

两个实体类User.javaAddress.java:

//address.java
public class Address {

    private String province;
    private String city;
    //getter and setter omit...
}

//user.java
public class User {

    private String name;
    private int age;
    private Address address;
    //getter and setter omit....
}

bean.xml内容:

<bean id="user" class="com.glm.pinlor.bean.ioc.User">
   <property name="name" value="小明"/>
   <property name="age" value="23"/>
   <property name="address" ref="address"/>

</bean>
<bean id="address" class="com.glm.pinlor.bean.ioc.Address">
   <property name="city" value="上海"/>
   <property name="province" value="浙江"/>
</bean>

测试用例:

@Test
public void iocTest(){

    System.out.println("========传统创建对象过程和依赖关系=======");
    User user = new User();
    user.setName("小明");
    user.setAge(23);
    Address address = new Address();
    address.setCity("上海");
    address.setProvince("浙江");
    user.setAddress(address);
    System.out.println(user);

    System.out.println("========Ioc方法=======");
    ApplicationContext act = new ClassPathXmlApplicationContext("classpath:bean.xml");
    User u = act.getBean(User.class);
    System.out.println(u);
}

结果:

========传统创建对象过程和依赖关系=======
User{name='小明', age=23, address=Address{province='浙江', city='上海'}}

========Ioc方法=======
User{name='小明', age=23, address=Address{province='浙江', city='上海'}}

可以看到两种方式都实现了同样的结果,但Ioc方法明显更简洁

结论:

  • 控制体现在: 调用者需要通过new方式创建User对象,而IoC方式已经将User Bean控制在容器中了,什么时候想用,就可以通过getBean()方法取出,不用也没有关系,spring容器帮你管理了Bean
  • 反转体现在: 即在传统方式创建对象中,对user设置属性只能通过setXXX方法,需要什么属性值,就必须设置什么属性值,前提是这个对象中存在的属性,如果要使用关联对象,在这里就是Address对象,就需要首先创建Address,调用者需要关心对象之间的关联关系。而对于IoC方式创建对象的方法,调用者只需要通过调用getBean()方法创建User就可以了,根本就不用关心User对象中是否依赖了其他对象关系(Address是否存在)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值