springIOC详解

目录

什么是 IOC

传统程序开发

控制反转式程序开发

Spring的创建和使用

项目创建

存储Bean对象

创建 Bean

将 Bean 注册到容器

获取并使⽤ Bean 对象

创建 Spring 上下⽂

获取指定的 Bean 对象

getBean ⽅法的更多⽤法

 使⽤ Bean


什么是 IOC

IoC = Inversion of Control 翻译成中⽂是“控制反转”的意思,也就是说 Spring 是⼀个“控制反转”的容器。
既然 Spring 是⼀个 IoC(控制反转)容器,重点还在“容器”⼆字上,那么它就具备两个最基础的功
能:
  • 将对象存⼊到容器;
  • 从容器中取出对象。
也就是说学 Spring 最核⼼的功能,就是学如何将对象存⼊到 Spring 中,再从 Spring 中获取对象的过程。

传统程序开发

假如,我们现在构建⼀辆“⻋”的程序,我们的实现思路是这样的:

构建⼀辆⻋(Car Class),然⽽⻋需要依赖⻋身(FrameWork Class),⽽⻋身需要依赖底盘(
Bottom Class),⽽底盘需要依赖轮胎(Tire Class),最终程序的实现代码如下:
public class NewCarExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.init();
    }
    /**
     * 汽⻋对象
     */
    static class Car {
        public void init() {
            // 依赖⻋身
            Framework framework = new Framework();
            framework.init();
        }
    }
    /**
     * ⻋身类
     */
    static class Framework {
        public void init() {
            // 依赖底盘
            Bottom bottom = new Bottom();
            bottom.init();
        }
    }
    /**
     * 底盘类
     */
    static class Bottom {
        public void init() {
            // 依赖轮胎
            Tire tire = new Tire();
            tire.init();
        }
    }
    /**
     * 轮胎类
     */
    static class Tire {
        // 尺⼨
        private int size = 30;
        public void init() {
            System.out.println("轮胎尺⼨:" + size);
        }
    }
}
传统程序开发的缺陷:
以上程序中,轮胎的尺⼨的固定的,然⽽随着对的⻋的需求量越来越⼤,个性化需求也会越来越多,这时候我们就需要加⼯多种尺⼨的轮胎,那这个时候就要对上⾯的程序进⾏修改了,修改后的代码如下所示:
public class NewCarUpdateExample {
    public static void main(String[] args) {
        Car car = new Car(20);
        car.run();
    }
    /**
     * 汽⻋对象
     */
    static class Car {
        private Framework framework;
        public Car(int size) {
            framework = new Framework(size);
        }
        public void run() {
            // 依赖⻋身
            framework.init();
        }
    }
    /**
     * ⻋身类
     */
    static class Framework {
        private Bottom bottom;
        public Framework(int size) {
            bottom = new Bottom(size);
        }
        public void init() {
            // 依赖底盘
            bottom.init();
        }
    }
    /**
     * 底盘类
     */
    static class Bottom {
        private Tire tire;
        public Bottom(int size) {
            tire = new Tire(size);
        }
        public void init() {
            // 依赖轮胎
            tire.init();
        }
    }
    /**
     * 轮胎类
     */
    static class Tire {
        // 尺⼨
        private int size;
        public Tire(int size) {
            this.size = size;
        }
        public void init() {
            System.out.println("轮胎尺⼨:" + size);
        }
    }
}
从以上代码可以看出,以上程序的问题是:当最底层代码改动之后,整个调⽤链上的所有代码都需要修改。

 

解决传统开发中的缺陷
我们可以尝试不在每个类中⾃⼰创建下级类,如果⾃⼰创建下级类就会出现当下级类发⽣改变操作,⾃⼰也要跟着修改。此时,我们只需要将原来由⾃⼰创建的下级类,改为传递的⽅式(也就是注⼊的⽅式),因为我们不需要在当前类中创建下级类了,所以下级类即使发⽣变化(创建或减少参数),当前类本身也⽆需修改任何代码,这样就完成了程序的解耦。

控制反转式程序开发

基于以上思路,我们把调⽤汽⻋的程序示例改造⼀下,把创建⼦类的⽅式,改为注⼊传递的⽅式,具体实现代码如下:
public class IocCarExample {
    public static void main(String[] args) {
        Tire tire = new Tire(20);
        Bottom bottom = new Bottom(tire);
        Framework framework = new Framework(bottom);
        Car car = new Car(framework);
        car.run();
    }
    static class Car {
        private Framework framework;
        public Car(Framework framework) {
            this.framework = framework;
        }
        public void run() {
            framework.init();
        }
    }
    static class Framework {
        private Bottom bottom;
        public Framework(Bottom bottom) {
            this.bottom = bottom;
        }
        public void init() {
            bottom.init();
        }
    }
    static class Bottom {
        private Tire tire;
        public Bottom(Tire tire) {
            this.tire = tire;
        }
        public void init() {
            tire.init();
        }
    }
    static class Tire {
        private int size;
        public Tire(int size) {
            this.size = size;
        }
        public void init() {
            System.out.println("轮胎:" + size);
        }
    }
}
代码经过以上调整,⽆论底层类如何变化,整个调⽤链是不⽤做任何改变的,这样就完成了代码之间的解耦,从⽽实现了更加灵活、通⽤的程序设计了。

 

我们发现了⼀个规律:通⽤程序的实现代码,类的创建顺序是反的,传统代码是 Car 控制并创建了 Framework,Framework 创建并创建了 Bottom,依次往下,⽽改进之后的控制权发⽣的反转,不再是上级对象创建并控制下级对象了,⽽是下级对象把注⼊将当前对象中,下级的控制权不再由上级类控制了,这样即使下级类发⽣任何改变,当前类都是不受影响的,这就是典型的控制反转,也就是 IoC 的实现思想。

Spring的创建和使用

项目创建

创建一个Maven项目

添加 Spring 框架⽀持
在项⽬的 pom.xml 中添加 Spring 框架的⽀持,xml 配置如下:
<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
</dependencies>

 创建启动类

最后在创建好的项⽬ java ⽂件夹下创建⼀个启动类,包含 main ⽅法即可:
public class App {
 public static void main(String[] args) {
 
 }
}

存储Bean对象

存储 Bean 分为以下 2 步:
1. 存储 Bean 之前,先得有 Bean 才⾏,因此先要创建⼀个 Bean。
2. 将创建的 Bean 注册到 Spring 容器中。
具体实现如下:

创建 Bean

所谓的 Bean 就是 Java 语⾔中的⼀个普通对象,实现代码如下:
public class User {
 public String sayHi(String name) {
 return name + " hello!";
 }
}

将 Bean 注册到容器

在创建好的项⽬中添加 Spring 配置⽂件 spring-config.xml,将此⽂件放到 resources 的根⽬录下

 

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 htt
p://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
接下来,再将 User 对象注册到 Spring 中就可以,具体操作是在 <beans> 中添加如下配置:
<beans>
 <bean id="user" class="com.bit.User"></bean>
</beans>

获取并使⽤ Bean 对象

获取并使⽤ Bean 对象,分为以下 3 步:
1. 得到 Spring 上下⽂对象,因为对象都交给 Spring 管理了,所以获取对象要从 Spring 中获取,那么就得先得到 Spring 的上下⽂。
2. 通过 Spring 上下⽂,获取某⼀个指定的 Bean 对象。
3. 使⽤ Bean 对象。

创建 Spring 上下⽂

Spring 上下⽂对象可使⽤ ApplicationContext,实现代码如下:
// 1.得到 Spring 的上下⽂对象,创建的时候需要配置 Spring 配置信息
ApplicationContext context = new ClassPathXmlApplicationContext("spring-con
fig.xml");
除了 ApplicationContext 之外,我们还可以使⽤ BeanFactory 来作为 Spring 的上下⽂,如下代码所示:
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("springconfig.xml"));
ApplicationContext 和 BeanFactory 效果是⼀样的,ApplicationContext 属于 BeanFactory 的⼦
类,它们的区别如下。
ApplicationContext VS BeanFactory(常⻅⾯试题)
  • 继承关系和功能⽅⾯来说:Spring 容器有两个顶级的接⼝:BeanFactory 和 ApplicationContext。其中 BeanFactory 提供了基础的访问容器的能⼒,⽽ ApplicationContext 属于 BeanFactory 的⼦类,它除了继承了 BeanFactory 的所有功能之外,它还拥有独特的特性,还添加了对国际化⽀持、资源访问⽀持、以及事件传播等⽅⾯的⽀持。
  • 从性能⽅⾯来说:ApplicationContext 是⼀次性加载并初始化所有的 Bean 对象,⽽ BeanFactory 是需要那个才去加载那个,因此更加轻量

获取指定的 Bean 对象

// 1.得到 Spring 上下⽂对象
ApplicationContext context = new ClassPathXmlApplicationContext("spring-con
fig.xml");
// 2.加载某个 bean
User user = (User) context.getBean("user");

 Bean 的 Id 要⼀⼀对应

getBean ⽅法的更多⽤法

getBean() ⽅法有很多种重载⽅法,我们也可以使⽤其他⽅式来获取 Bean 对象,⽐如以下这两种:
根据类型获取 Bean:
UserController user = context.getBean(UserController.class); 
或以下这种,名称 + 类型获取 Bean:
UserController user = context.getBean("user", UserController.class); 
⼆者的区别:当有⼀个类型被重复注册到 spring-config.xml 中时,只能使⽤根据名称获取了,⽐如以下程序:

 

以上场景就会导致程序报错,如下图所示: 

 使⽤ Bean

public class App {
 public static void main(String[] args) {
 // 1.得到 Spring 上下⽂对象
 ApplicationContext context =
 new ClassPathXmlApplicationContext("spring-config.xml");
 // 2.加载某个 bean
 User user = (User) context.getBean("user");
 // 3.调⽤相应的⽅法
 System.out.println(user.sayHi("Java"));
 }
}

总结

1. 操作容器之前,先要有容器,所以先要得到容器。
2. 存对象
      a. 创建 Bean(普通类)。
      b. 将 Bean 注册(配置)到 spring-confing.xml 中
3. 取对象
      a. 得到 Spring 上下⽂,并读取到 Spring 的配置⽂件。
      b. 获取某⼀个 Bean 对象。
      c. 使⽤ Bean 对象。
操作流程如下图所示:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值