Spring Boot笔记十:IOC控制反转

时间:2023-03-09 07:29:26
Spring Boot笔记十:IOC控制反转

IOC控制反转和DI依赖注入

以前一直听说控制反转和依赖注入,一直不知道是什么,不理解。现在懂了

举个例子,我一个人想要穿衣服,用代码实现怎么做呢?

衣服类 衣服=new 衣服类();
人类 人=new 人类();
人.set衣服=衣服();

大概就是这样子,衣服有一个类,人有一个类,人这个类里面有一个穿衣服的方法set衣服,所以,为了实现一个人穿衣服的功能,我写了3个东西

  1. 创建衣服类
  2. 创建人类
  3. 人类依赖的衣服对象,我手动的设置赋值了(赋值就是注入)

这个不难理解吧,那么控制反转IOC到底是干嘛的呢?就是不需要我们手动的去创建类了,也不需要我们手动的给类的依赖进行赋值了也就是注入,用了IOC,代码就会变成这样

人类 人=IOC容器.get人类();

没了...完事!就是这么简单,你可能会问,衣服类不需要创建?不需要,IOC容器知道人类依赖衣服类,自己创建完成了,你可能会问,人类里面还得set衣服呢,不需要去赋值注入?不需要,IOC容器自动帮你注入了

Spring Boot笔记十:IOC控制反转

IOC容器这么好用?我要用!!

这就是控制反转IOC,其本质就是帮你创建一些对象,帮你去注入依赖的对象。

那么依赖注入DI是啥呢?其实就是IOC,这俩是一样的。只不过IOC这个名字听起来侧重于控制反转,就是容器帮你创建对象,DI这个名字听起来侧重于依赖注入,容器帮你注入依赖的对象。

IOC和DI就仿佛于许嵩和Vae一样,不同的名字,一样的人。

IOC实现Hello World

上面搞懂了概念,接下来用代码去实现一下IOC

使用maven导入包

有3个jar包是需要的,如下

  <!--https://mvnrepository.com/artifact/org.springframework/spring-beans-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!--https://mvnrepository.com/artifact/commons-logging/commons-logging-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

新建IOC配置文件

在resources目录下新建一个IOC的配置文件applicationContext.xml,等下需要通过这个配置文件去创建IOC容器

<?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 id="helloWorld" class="com.vae.springboot.bean.HelloWorld">
<property name="userName" value="许嵩"></property>
</bean> </beans>
注意:property标签里面给属性赋值的时候,name一定是字段名,这个字段一定要有set属性,否则无效

新建一个类HelloWorld

内容就写一个输出方法

package com.vae.springboot.bean;

public class HelloWorld {

    private String userName;
public void setUserName(String userName){
this.userName=userName;
} public void sayHello(){
System.out.println("Hello World "+userName);
} }

新建HelloWorld的测试类

这个测试类里面,我们会使用两种方式来输出sayHello方法,一种是普通的方式,一种是IOC方式

package com.vae.springboot.bean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class HelloWorldTest { //正常的,我们怎么去调用sayHello方法
@Test
public void sayHelloTest(){
HelloWorld helloWorld=new HelloWorld();
helloWorld.setUserName("许嵩");
helloWorld.sayHello();
//这就是最普遍的做法吧,自己创建对象,自己注入依赖值,啥都是自己干
} //来一个IOC的例子
@Test
public void sayHelloIOC(){
HelloWorld helloWorld=null;
//--------------------IOC开始了-------------------
//1.从classpath路径去寻找配置文件,加载我们的配置
Resource resources= new ClassPathResource("applicationContext.xml");
//2.加载配置文件之后,创建IOC容器
BeanFactory factory=new XmlBeanFactory(resources);
//3.从Spring IOC容器中获取指定名称的对象
helloWorld= (HelloWorld) factory.getBean("helloWorld");
//--------------------IOC结束了---------------------
helloWorld.sayHello();
} }

直接执行sayHelloTest方法,sayHelloIOC方法,结果是一样的,一个是普通的自己去创建对象,去注入依赖,一种是IOC容器帮你创建和注入。可能有人会说,为什么IOC的代码还多......看着IOC也没有简单啊,代码还多......别慌,往下看,到时候绝对惊喜。(现在就想知道的看下面的@Autowired)

先讲解一下上面的代码

BeanFactory:表示Spring IOC容器,专门生产bean对象的工厂,负责配置,创建和管理bean

bean:被Spring IOC容器管理的对象都是bean,可以理解为Spring下皆为bean

Spring IOC容器怎么知道哪些是管理的对象?

  1. xml配置文件
  2. 注解
  3. Java代码

其中xml配置文件的方式我们已经讲了,这也是最简单的方式了,后面的两种方式暂留到时候补在这里

暂留地...

IOC容器是怎么去管理对象的呢?到底是怎么帮助我们去创建对象的,到底是怎么帮助我们去自动注入的?我们在测试类里面去模拟一下IOC的工作原理,使用到的技术有两个,反射和内省

@Test
public void testIOC() throws Exception {
String className="com.vae.springboot.study.bean.HelloWorld";
HelloWorld helloWorld=null;
//--------------------模拟IOC开始了-------------------
//1.使用反射创建对象
Class clzz=Class.forName(className);
Constructor con=clzz.getConstructor();
con.setAccessible(true);//设置构造器可访问性为true
Object obj=con.newInstance(); //2.使用内省机制获取所有的属性名称
BeanInfo beanInfo=Introspector.getBeanInfo(clzz,Object.class);
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) {
String propertyName=pd.getName();
if ("userName".equals(propertyName)) {
pd.getWriteMethod().invoke(obj,"蜀云泉");
}
}
helloWorld=(HelloWorld)obj;
//--------------------模拟IOC结束了---------------------
helloWorld.sayHello();
}

执行测试方法,输出结果正是

Hello World 蜀云泉

这不就是和IOC的效果一样嘛,所以我们可以说,IOC的本质就是反射+内省机制

IOC容器getBean方法的三种签名

我们上面的IOC容器有一行是加载Bean的,以便于获取指定的对象,如下:

helloWorld= (HelloWorld) factory.getBean("helloWorld");

这里的helloWorld正是我们xml文件里面的bean的id,这个就叫做签名,有三种方式:

  1. 根据Bean对象在容器中的id来获取

    这个其实就是我们使用的方式,我们可以尝试着把xml文件中bean的id复制一个出来,当有两个id为helloWorld时,就会报错。所以我们Bean的id一定要是唯一的

    <?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 id="helloWorld" class="com.vae.springboot.bean.HelloWorld">
    <property name="userName" value="许嵩"></property>
    </bean>
    <bean id="helloWorld" class="com.vae.springboot.bean.HelloWorld">
    <property name="userName" value="许嵩"></property>
    </bean> </beans>
  2. 根据类型获取Bean

    这种方式的代码是这样的

     helloWorld= factory.getBean(HelloWord.class);

    根据类的类型获取Bean,连强转都不需要了,当然,这种方式也是有问题的,xml里面再赋值一下,两个bean的id不一样,class一样的时候,还是会报错,报类找到的Bean不是唯一的

    <?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 id="helloWorld" class="com.vae.springboot.bean.HelloWorld">
    <property name="userName" value="许嵩"></property>
    </bean>
    <bean id="helloWorld2" class="com.vae.springboot.bean.HelloWorld">
    <property name="userName" value="许嵩"></property>
    </bean> </beans>
  3. 根据id+类型来获取Bean

    这个就牛逼了,上面的1和2的综合体啊,妈妈再也不怕我获取Bean对象的时候报错了

    helloWorld= factory.getBean("helloWorld",HelloWord.class);

getBean的三种签名,我们以后就使用第三种。

xml配置文件的import导入

我们的applicationContext.xml里面写的是Bean,一个项目里面那么多需要控制反转的,难道我们写上千个Bean?这样都写在一个xml文件里,太大,也太乱,所以我们可以分开写每个包里面写个自己的xml,然后applicationContext.xml直接import导入就可以了

    <!--导入其他的配置文件-->
<import resource="classpath:com.vae.springboot.bean.HelloWorld.xml"></import>

大概就是这样,直接在拆分的xml文件上单击右键,Copy Reference就可以了,import默认是从classpath下面找的,所以我们加上一个classpath:,加不加都一样,默认就是这个,所以还是加上吧

@Autowired

牛逼的地方来了啊,我们上面写过了好几行的IOC代码,抱怨了IOC这么牛逼还要写好几行代码,现在@Autowired,来了

@Autowired:表示自动按照类型去Spring容器中找到对应的Bean对象,然后自动注入

再来贴一下我们上面写的IOC代码吧

 @Test
public void sayHelloIOC(){
HelloWorld helloWorld=null;
//--------------------IOC开始了-------------------
//1.从classpath路径去寻找配置文件,加载我们的配置
Resource resources= new ClassPathResource("applicationContext.xml");
//2.加载配置文件之后,创建IOC容器
BeanFactory factory=new XmlBeanFactory(resources);
//3.从Spring IOC容器中获取指定名称的对象
helloWorld= (HelloWorld) factory.getBean("helloWorld");
//--------------------IOC结束了---------------------
helloWorld.sayHello();
}

啧啧,惨不忍睹,这样写,好麻烦....来看看注解的方式

@ContextConfiguration("classpath:applicationContext.xml")

先在测试类头上加个这个,意思是找到我们的Spring容器,classpath就是resource目录

然后写测试方法

    @Autowired
private HelloWorld helloWorld;
@Test
public void sayHelloIOCNB(){
helloWorld.sayHello();
}

没了.....就这么简单...@Autowired牛逼(破音)

IOC容器

虽然我们讲过了牛逼的@Autowired,但是弱鸡的还是需要讲一下,ICO容器有俩

  1. BeanFactory:懒,懒得很
  2. ApplicationContext:用这个

我们在上面已经用过BeanFactory了,ApplicationContext这个其实是BeanFactory的一个子接口,我们再看看这俩的方式有啥不同

    @Test
public void sayHelloIOC(){
HelloWorld helloWorld=null;
//--------------------IOC开始了-------------------
//1.从classpath路径去寻找配置文件,加载我们的配置
Resource resources= new ClassPathResource("applicationContext.xml");
//2.加载配置文件之后,创建IOC容器
BeanFactory factory=new XmlBeanFactory(resources);
//3.从Spring IOC容器中获取指定名称的对象
System.out.println("上面的代码没有创建Bean,下面的代码获取Bean的时候才会创建Bean");
helloWorld= (HelloWorld) factory.getBean("helloWorld");
//--------------------IOC结束了---------------------
helloWorld.sayHello();
} @Test
public void sayHelloIOCctx(){
HelloWorld helloWorld=null;
//--------------------IOC开始了-------------------
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("上面的代码已经创建Bean对象了,下面的获取Bean,获取已有的Bean");
helloWorld= ctx.getBean("helloWorld",HelloWorld.class);
//--------------------IOC结束了---------------------
helloWorld.sayHello();
}

我在打印里面写了,这俩其实是一样的结果,但是BeanFactory懒,你不获取Bean,我就不创建Bean,ApplicationContext比较好,所以IOC容器,我选择ApplicationContext

Bean的作用域

  1. singltton:单例,在IOC容器中的Bean实例,都是唯一的
  2. prototype:多例,在IOC容器中的Bean,每次都返回一个新的对象
  3. ......

作用域有好几个,我这里只介绍两个,一个单例,一个多例的,xml配置如下

    <bean id="helloWorld1" class="com.vae.springboot.bean.HelloWorld" scope="singleton"></bean>
<bean id="helloWorld2" class="com.vae.springboot.bean.HelloWorld" scope="prototype"></bean>

Bean的初始化和销毁

我新建一个类,就叫MyDataSource

package com.vae.springboot.bean;

public class MyDataSource {

    public MyDataSource(){

    }

    public void open(){
System.out.println("初始化");
} public void dowork(){
System.out.println("工作");
} public void close(){
System.out.println("销毁");
}
}

然后新建一个测试类

package com.vae.springboot.bean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration("classpath:applicationContext.xml")
public class MyDataSourceTest { //最普通的方式
@Test
public void test1(){
MyDataSource myDataSource=new MyDataSource();
myDataSource.open();
myDataSource.dowork();
myDataSource.close();
} @Autowired
private MyDataSource myDataSource;
//IOC容器的方式
@Test
public void test2(){
myDataSource.dowork();
} }

普通方式和IOC容器的方式,都是有初始化和销毁的,原因是我们的xml写了这一行

  <bean id="myDataSource" class="com.vae.springboot.bean.MyDataSource"  init-method="open" destroy-method="close"></bean>

Bean的初始化和销毁,以后就用xml配置吧,就让IOC来管理吧,省事

人衣看DI

我们开头讲了一个人和衣服的例子,这次,我们使用DI(IOC)再来看看,还是这3种方式

  1. xml配置方式
  2. 注解方式
  3. Java代码方式

我们来一个一个的用代码实现一下

xml配置方式(不推荐使用,但是还是看会)

新建一个类,Person

package com.vae.bean;

public class Person {
private Clothes clothes; public void setClothes(Clothes clothes) {
this.clothes = clothes;
} @Override
public String toString() {
return "Person{" +
"clothes=" + clothes +
'}';
}
}

新建一个类,Clothes

package com.vae.bean;

public class Clothes {
public Clothes() {
System.out.println("我是一件衣服,我很方");
}
}

我们的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="clothes" class="com.vae.bean.Clothes"></bean>
<bean id="person" class="com.vae.bean.Person" autowire="byName"></bean> </beans>

bean里的autowire有好几个值,byName和byType都可以用,construct不能用,因为衣服类默认构造器没了

最后,我们的测试类

package com.vae.bean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration("classpath:applicationContext.xml")
public class PersonTest { @Autowired
private Person person; @Test
public void test(){
System.out.println(person);
}
}

执行一下,效果还是很棒的

如果Clothes类中还有一个类类型的变量,比如叫款式style,可以这样

    <bean id="style" class="com.vae.springboot.study.bean.style"/>
<bean id="helloWorld" class="com.vae.springboot.study.bean.HelloWord">
<property name="userName" ref="style"></property>
</bean>

使用ref可以注入类类型