首页 > 代码库 > spring IOC(DI)

spring IOC(DI)

 软件152 刘昊翰

一、IOC(DI) 概念

IOC(Inversion of Control,控制倒转)Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。

 

DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。

 

依赖注入(DI)和控制反转(IOC)是从不同的角度的描述的同一件事情,就是指通过引入IOC容器,利用依赖关系注入的方式,实现对象之间的解耦。

 

3.2.2 IOC

1.USB.java代码:

 

package cqvie.com;

interface USB //USB接口

{

  public void insert();

  public String read();

  public void write(String s);

  public void pop();

}

 

2.UDisk.java代码:

 

package cqvie.com;

import org.springframework.stereotype.Component;

@Component

public class UDisk implements USB

{

  public void insert() {

      System.out.println("读入U盘");

      }

  public String read(){

      System.out.println("读取数据:");

      return "data";

  }

  public void write(String s){

      System.out.println("写入数据");

  }

  public void pop(){

      System.out.println("弹出U盘");

  }

}

 

3.Computer.java代码:

 

package cqvie.com;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;

@Component("theComputer")

public class Computer

{

  @Autowired

  //@Qualifier("UDisk")

  private USB usbDevice;

  public Computer(){ }  //这里不能构造USB设备

  public void saveData(String data) //实现智能设备的方法

  { //insert,write,pop

      usbDevice.insert();

      usbDevice.write(data);

      usbDevice.pop();

  }

  public void setUsbDevice(USB u){  usbDevice=u;  }   

}

 

4.test.java代码:

 

package cqvie.com;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

    public static void main(String[] args) {

          //Computer c=new Computer();

          //USB u=new UDisk();

          //c.setUsbDevice(u);

         ApplicationContext ctx = new ClassPathXmlApplicationContext(

                "applicationContext.xml");

             Computer c=(Computer) ctx.getBean("theComputer");

             c.saveData("个人简历");

             

           Computer1 c1=new Computer1();

              HDD mp=new movHDD();

              c1.setMpDevice(mp);

              c1.saveData("移动硬盘");             

    }

}

 

5.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:p="http://www.springframework.org/schema/p"

    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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="u1" class="cqvie.com.UDisk"></bean>

    <bean id="theComputer" class="cqvie.com.Computer">

        <property name="usbDevice" ref="u1"></property>

    </bean>

</beans>

 6.运行结果如图

技术分享

 

spring IOC(DI)