Spring中IoC的理解

目录

一、IOC的核心概念

IoC(Inversion of Control:控制反转) 是一种设计思想,而不是一个具体的技术实现。IoC 的思想就是将原本在程序中手动创建对象的控制权,交由 Spring 框架来管理,由Spring容器管理bean的整个生命周期。通俗来说就是IoC是设计思想,DI是实现方式。

通过反射实现对其他对象的控制,包括初始化、创建、销毁等,解放手动创建对象的过程,同时降低类之间的耦合度。

spring框架集中管理的是Bean对象,程序中的类将创建对象的权利交给了IoC容器,所以程序中的类需要注册为Bean类。

二、Bean注入的方式

1、通过xml文件进行注入Bean

在使用这个方法注入bean的时候,我们需要再项目resources目录下中创建一个xml文件如下图:

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">

</beans>

在中注入bean需要如下:

xml 复制代码
    <bean id="cat" class="com.example.entity.Cat"></bean>

id:Bean 的唯一标识符,用于在容器中查找或引用该 Bean。

class :指定 Bean 的具体实现类,Spring 会实例化它。

Cat类定义如下:

测试代码如下:

运行结果如下;

类被注入到IoC容器中之后,IoC只会创建一个对象,所有用到该对象的时候都是这个对象。

验证IoC容器中每个类中只会创建一个对象的代码如下:

java 复制代码
    @Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
        Cat cat = (Cat) applicationContext.getBean("cat");
        Cat cat1 = (Cat) applicationContext.getBean("cat");
        System.out.println(cat);
        System.out.println(cat == cat1);
    }

验证结果如下:

根据System.out.println(cat == cat1);为true,因为==比较的是对象的地址,地址为true,所以cat和cat1是相同的对象,因此IoC容器中只会存在一个bean类的对象。

2、利用context:component-scan注入IoC容器

Spring框架为了更好的标识web应用程序开发当中,bean对象到底归属于哪一层,又提供了@Component的衍生注解:

利用context:component-scan扫描需要配合对应包中的类的@component以及衍生注解。

Cat类如下:

java 复制代码
package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Controller;

@Controller
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Cat {
    public void jump(){
        System.out.println("跳跳跳");
    }
    private String name;
    private int age;

}

Dog类如下:

java 复制代码
package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Controller;

@Controller
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dog {
    private String name;
    private int age;
    public void pao(){
        System.out.println("跑跑跑");
    }
}

Person类如下:

java 复制代码
package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private String name;
    private int age;
    private Cat cat;
    private int[] arr;
    private List<Integer> list;
    public void eat()
    {
        System.out.println("吃吃吃");
    }
}

需要将所有的类中都加入对应的@component以及衍生的注解。并且在spring.xml中加入,代码如下:

xml 复制代码
    <context:component-scan base-package="com.example"></context:component-scan>

测试代码如下:

java 复制代码
    @Test
    public void test2() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
        Cat cat = (Cat) applicationContext.getBean("cat");
        Dog dog = (Dog) applicationContext.getBean("dog");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(cat);
        System.out.println(dog);
        System.out.println(person);
    }

运行结果如下:

当Dog类中去掉@Controller之后,再一次执行代码可以得到结果如下:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dog' available 找不到可以使用的bean对象。

3、通过全注解进行注入bean

利用全注解的情况注入bean的形式需要创建一个config目录,用来单独存放config类,config被称为配置类,下面的config类定义如下:

java 复制代码
package com.example.config;

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

@Configuration
@ComponentScan(value = "com.example.entity")
public class ApplicationConfig {
}

@Configuration:用来标记是否是配置类

用来扫描某一个目录下的文件并且获得他们的全类名,并且将他们注入到IoC容器当中。

定义好配置类文件之后,仍需要在对应的文件中加入@component以及衍生的注解

最后在测试文件定义如下:

java 复制代码
    @Test
    public void test3() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        Cat cat = (Cat)applicationContext.getBean("cat");
        System.out.println(cat);
    }

测试结果:

三、依赖注入(DI)

依赖注入(Dependency Injection, DI)是一种设计模式,也是Spring框架的核心概念之一。其作用是去除Java类之间的依赖关系,实现松耦合,以便于开发测试。为了更好地理解DI,先了解DI要解决的问题。

对象的依赖关系由外部容器(如 Spring)管理,而不是由对象自己创建或查找依赖。

通过"注入"的方式(构造器、Setter、字段等)将依赖对象传递给目标对象。

1、Setter 注入

通过setter将数据注入到对应的bean对象,需要再xml中进行配置,配置如下:

xml 复制代码
    <bean id="Dog" class="com.example.entity.Dog">
        <property name="name" value="123"></property>
        <property name="age" value="12"></property>
    </bean>
xml 复制代码
 <property name="name" value="123"></property>

每一个property对应每一个类中不同的属性并且一一对应,而且需要根据属性的形式发生改变。

需要注意的是复杂数据类型的情况如:对象、数组、列表和哈希表等。

类的定义如下:

需要的情况如下定义:

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: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">
    <bean id="Dog" class="com.example.entity.Dog">
        <property name="name" value="123"></property>
        <property name="age" value="12"></property>
    </bean>
    <bean id="cat" class="com.example.entity.Cat"></bean>
    <bean id="person" class="com.example.entity.Person">
        <property name="name" value="123"></property>
        <property name="age" value="12"></property>
        <property name="cat" ref="cat"></property>
        <property name="arr">
            <array>
                <value>1</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>12</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="123" value="12"></entry>
            </map>
        </property>
    </bean>
</beans>

2、构造器注入

需要在bean类中定义好对应的构造器,使用方法如下:

xml 复制代码
    <bean id="person" class="com.example.entity.Person">
        <constructor-arg name="name" value="123"></constructor-arg>
        <constructor-arg name="age" value="12"></constructor-arg>
    </bean>

3、字段注入的形式

字段注入需要用到@value和@Autowired两种方式,@value适用于String、int、等数据类型,@Autowired适用于较简单的复杂数据类型,会自动识别,但是很复杂的无法识别。

Cat中的使用@value的使用情况:

Test类中定义如下:

java 复制代码
    @Test
    public void test3() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        Cat cat = (Cat)applicationContext.getBean("cat");
        System.out.println(cat);
    }

结果如下: