Spring IOC

一.IOC-DI概念

IOC(Inversion Of Control):控制反转 - 对象创建权力被反转

DI(Dependency Injection):依赖注入 - 将一个对象放入另一个对象

二.Spring IOC基于XML(Spring 1.0)

生产一批电脑(IOC XML实现)

1.在pom.xml文件中导入包(用来支持IOC)

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.1.4</version>
    </dependency>
</dependencies>

2.在resources文件夹中创建XML文件,在XML文件中编写(告诉Spring它要帮我们创建哪些对象,对象之间的依赖关系是什么样的)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--需要Spring创建对象使用<bean>标签,这里指明Spring创建Computer-->
    <bean class="org.gok.computer.Computer">
        <!--依赖注入,icpu这个属性设置为cpu1-->
        <property name="icpu" ref="cpu1"></property>
        <!--依赖注入,iMemory这个属性设置为memory1-->
        <property name="iMemory" ref="memory1"></property>
    </bean>
    <!--创建CPU1-->
    <bean class="org.gok.CPU.CPU1" name="cpu1"></bean>
    <!--创建Memory1-->
    <bean class="org.gok.memory.Memory1" name="memory1"></bean>

</beans>

4.创建Computer类(不写死new,添加setter和getter方法)

package org.gok.computer;

import org.gok.CPU.ICPU;
import org.gok.memory.IMemory;

public class Computer {
    private ICPU icpu;
    private IMemory iMemory;

    public ICPU getIcpu() {
        return icpu;
    }

    public void setIcpu(ICPU icpu) {
        this.icpu = icpu;
    }

    public IMemory getiMemory() {
        return iMemory;
    }

    public void setiMemory(IMemory iMemory) {
        this.iMemory = iMemory;
    }

    @Override
    public String toString() {
        System.out.println("Computer{" +
                "icpu=" + icpu +
                ", iMemory=" + iMemory +
                '}');
        return null;
    }
}

5. 创建Main类,试运行

package org.gok;

import org.gok.computer.Computer;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //创建ioc容器
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
        //获取computer对象
        Computer computer = ioc.getBean(Computer.class);

        computer.toString();
    }
}

运行结果:

三.Spring IOC基于注解(Spring 2.0)

生产一批电脑(IOC 注解实现)

1.基于二的XML实现

2.CPU1添加@Component注解(代表CPU1需要被Spring管理)

   Memory1添加@Component注解(代表Memory1需要被Spring管理)

   Computer一样添加@Component注解,同时使用@Autowired(代表属性需要依赖注入)

3.spring.xml修改:

所有<bean>标签都不需要,指定注解所在包位置即可

<context:component-scan base-package="org.gok"></context:component-scan>

运行结果:

四.Spring IOC基于配置类(Spring 3.0)

生产一批汽车(IOC 配置类实现)

1.删除spring.xml文件

2.创建带有@Configuration注解的SpringConfig.java代替spring.xml

package org.gok.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration //表示这个类是用来代替spring.xml
@ComponentScan("org.gok") //<context:component-scan base-package="org.gok">
public class SpringConfig {
}

3.修改Main

(AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext)

package org.gok;

import org.gok.computer.Computer;
import org.gok.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        //创建ioc容器
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class);
        //获取computer对象
        Computer computer = ioc.getBean(Computer.class);

        computer.toString();
    }
}

运行结果:

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值