IoC学习

IoC概述

      IoC(Inverse of Control)控制反转,是一种设计思想。依赖对象的创建由IoC容器创建并注入到被注入对象中。这时不需要被注入对象不需要管理依赖对象,只需要被动使用即可。
      控制什么?Java开发中,对象A依赖对象B,一般都是对象A之间创建对象B,是由对象A主动创建的。而IoC会专门负责依赖对象的创建,由IoC容器控制外部资源的获取。
      反转了什么?客户端依赖的对象由自己主动创建变成容器创建和装配依赖对象,依赖对象的获取被反转

IoC分类
DL依赖查找

不常用的方式,使用API自己查找资源和组装对象。

DI依赖注入

常用方式,主要分为接口注入,构造器注入和setter注入。

接口注入

接口注入需要实现一个接口,接口提供一个方法,将依赖对象注入进去。

// 汽车类,依赖对象
public class Car {
    public void driver(){
        System.out.println("The car is running!");
    }
}
//要实现的接口
public interface CarInterface {
    void injectCar(Car car);
}
//被注入的对象,需要实现接口,实现注入方法
public class Man implements CarInterface{
    private Car car;
        @Override
    public void injectCar(Car car) {
        this.car = car;
    }
        public void driver(){
        car.driver();
    }
}
构造器注入
// 汽车类,依赖对象
public class Car {
    public void driver(){
        System.out.println("The car is running!");
    }
}
//构造器注入必须提供一个构造器
public class Unknow {
    private Car car;
    public Unknow(Car car){
        this.car = car;
    }
    public void driver(){
        car.driver();
    }
}
setter注入
// 汽车类,依赖对象
public class Car {
    public void driver(){
        System.out.println("The car is running!");
    }
}
//提供setter方法注入对象
public class Woman {
    private Car car;
    public void driver(){
        car.driver();
    }
    public Car getCar() {
        return car;
    }
        public void setCar(Car car) {
        this.car = car;
    }
}
Spring的DI

Spirng中支持构造器注入和setter注入。
主要由两类IoC容器:BeanFactory和ApplicationContext。

XML配置方式
构造器注入

配置文件beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="com.tetris.Car" id="car"/>
    <bean class="com.tetris.Man" id="man">
        <constructor-arg name="car" ref="car"/>
    </bean>
</beans>

代码

// 汽车类,依赖对象
public class Car {
    public void driver(){
        System.out.println("The car is running!");
    }
}
public class Man {
    private Car car;
    public Man(Car car){
        this.car = car;
    }
    public void driver(){
        car.run();
    }
}
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
        Man man = (Man) context.getBean("man");
        man.driver();
    }
}
setter注入

配置文件beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="com.tetris.Car" id="car"/>
    <bean class="com.tetris.Man" id="man">
        <property name="car" ref="car"/>
    </bean>
</beans>

Java编码

// 汽车类,依赖对象
public class Car {
    public void driver(){
        System.out.println("The car is running!");
    }
}
//提供setter方法注入对象
public class Woman {
    private Car car;
    public void driver(){
        car.driver();
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
}
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
        Man man = (Man) context.getBean("man");
        man.driver();
    }
}
注解配置方式
构造器注入

配置文件,开启注解扫描

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.tetris"/>
</beans>

Java编码

//使用注解
@Component
public class Car {
    public void run(){
        System.out.println("The car is runnig!");
    }
}
//
@Component
public class Man {
    private Car car;
    @Autowired
    public Man(Car car) {
        this.car = car;
    }
        public void driver() {
        car.run();
    }
}
//
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
        Man man = (Man) context.getBean("man");
        man.driver();
    }
}
setter注入

配置文件,开启注解扫描

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.tetris"/>
</beans>

Java编码

//使用注解
@Component
public class Car {
    public void run(){
        System.out.println("The car is runnig!");
    }
}
@Component
public class Man {
    private Car car;
        public void driver() {
        car.run();
    }
        public Car getCar() {
        return car;
    }
    @Autowired
    public void setCar(Car car) {
        this.car = car;
    }
}
//
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
        Man man = (Man) context.getBean("man");
        man.driver();
    }
}

@Autowired可以用在类成员变量,构造器和方法上。
默认是使用byType注入,有且只能有一个依赖对象,否则会报错,可以使用required设置为false;如果要使用byName注入,可以使用@Qualifier注解配合使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值