【Spring笔记二】关于IOC和DI

贴一篇大牛的文章:https://blog.csdn.net/javazejian/article/details/54561302

IOC(Inversion of Control,控制反转)

“控制”就是指对 对象的创建、维护、销毁等生命周期的控制,这个过程一般是由我们的程序去主动控制的,如使用new关键字去创建一个对象(创建),在使用过程中保持引用(维护),在失去全部引用后由GC去回收对象(销毁)。 
“反转”就是指对 对象的创建、维护、销毁等生命周期的控制由程序控制改为由IOC容器控制,需要某个对象时就直接通过名字去IOC容器中获取。

以获取对象的方式来进行比较

传统的方式: 
通过new 关键字主动创建一个对象
IOC方式
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。 

打个比喻:
传统方式:我们拿菜篮子去菜市场买菜,得要我们自己手动每一样一样菜放进篮子。   

用 IOC:相当于IOC是一个高科技篮子,你只要设置好你需要什么菜,IOC会自动帮你把菜放进篮子里,不用你主动去拿菜了。

实际例子,创建项目,导入jar包后

步骤一,准备pojo Category

package com.how2java.pojo;
 
public class Category {
 
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private int id;
    private String name;
}

步骤二,在src目录下新建applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中

<?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"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
  
</beans>

步骤三 ,测试代码,演示通过spring获取Category对象,以及该对象被注入的name属性。

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.pojo.Category;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
 
        Category c = (Category) context.getBean("c");
         
        System.out.println(c.getName());
    }
}

 此时,观察控制台便可看到打印出了c的name属性为category 1

DI(Dependency Injection,依赖注入)

DI 是 IOC 的一种重要实现。可以这样理解DI:一个对象的创建往往会涉及到其他对象的创建,如果一个对象A需要使用另一个对象B才能实现某个功能,这是就可以说A对象依赖于B对象,而Spring容器在创建A对象时,会自动将A对象需要的B对象注入到A对象中,此过程就是依赖注入。依赖注入的作用就是在使用Spring框架创建对象时,动态地将其所依赖的对象注入到Bean组件中。

结合上面代码来说 DI,我们在配置 XML的时候,对Category这个类中的 name 这个属性进行了赋值,如下

<property name="name" value="category 1" />

我们可以称为是属性注入,而这个属性注入是依赖于 Category这个类中的 setName() 这个方法。

在看看对象的注入

步骤一,准备一个新的类Product,Product类中有对Category对象的setter getter

package com.how2java.pojo;
 
public class Product {
 
    private int id;
    private String name;
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
}

步骤二,修改applicationContext.xml ,在创建Product的时候注入一个Category对象
注意,这里要使用ref来注入另一个对象

<?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"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>
 
</beans>

步骤三,创建测试类,查看结果,通过Spring拿到的Product对象已经被注入了Category对象了

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.pojo.Product;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
 
        Product p = (Product) context.getBean("p");
 
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
    }
}

 TestSpring

用一句话来概括IOC和DI就是,IOC 就是一种新的思想,而 DI 是这种新思想的具体实现。

在 Spring中创建IOC容器的两种方式(两个接口)

1、BeanFactory

  是 Spring 框架的基础设施。

2、ApplicationContext(推荐使用)

  面向使用Spring框架的开发者,是BeanFactory的子接口。 

猜你喜欢

转载自blog.csdn.net/weixin_41866960/article/details/83930213