Spring IOC(一)

Spring Framework

核心容器

Core Container
核心容器,在 Spring 环境下使用任何功能都必须基于 IOC 容器。
IOC:Inversion of Control,翻译过来是反转控制。
Spring 的 IOC 容器就是 IOC 思想的一个落地的产品实现。IOC 容器中管理的组件也叫做 bean。在创建bean 之前,首先需要创建 IOC 容器。Spring 提供了 IOC 容器的两种实现方式:
BeanFactory
这是 IOC 容器的基本实现,是 Spring 内部使用的接口。面向 Spring 本身,不提供给开发人员使用。
ApplicationContext
BeanFactory 的子接口,提供了更多高级特性。面向 Spring 的使用者,几乎所有场合都使用ApplicationContext 而不是底层的 BeanFactory。
在这里插入图片描述
ClassPathXmlApplicationContext
通过读取类路径下的 XML 格式的配置文件创建 IOC 容器对象
FileSystemXmlApplicationContext
通过文件系统路径读取 XML 格式的配置文件创建 IOC 容器对象
ConfigurableApplicationContext
ApplicationContext 的子接口,包含一些扩展方法refresh() 和 close() ,让 ApplicationContext 具有启动、关闭和刷新上下文的能力。
WebApplicationContext
专门为 Web 应用准备,基于 Web 环境创建 IOC 容器对
象,并将对象引入存入 ServletContext 域中。

基于XML管理bean

首先导入依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
      
    </dependency>

第二步 创建spring的配置文件
在这里插入图片描述
applicationContext.xml
不知道命名有什么特殊的要求没有

看一实体类 Student
在这里插入图片描述
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="StudentOne" class="com.atguigu.spring.pojo.Student" scope="prototype" ></bean>

    <bean id="Studenttwo" class="com.atguigu.spring.pojo.Student" scope="prototype" >
        <!--set注入
        property:通过成员变量的set方法完成赋值
        name需要赋值的成员变量的属性名
        value设置为属性所赋的值
        -->
        <property name="sid" value="1001"></property>
        <property name="sname" value="zmm"></property>
        <property name="gender" value=""></property>
        <property name="age" value="23"></property>
    </bean>
    <bean id="StudentThree" class="com.atguigu.spring.pojo.Student" scope="prototype" >
        <!--
        构造器注入-->
        <constructor-arg name="age" value="23"></constructor-arg>
        <constructor-arg name="gender" value=""></constructor-arg>
        <constructor-arg name="sid" value="1011"></constructor-arg>
        <constructor-arg name="sname" >
            <value><![CDATA[<张雄安>]]></value>
        </constructor-arg>
    </bean>
</beans>

在这里插入图片描述
scope决定了spring管理的bean是单例还是多例
默认是单例
scope="singleton"单例scope="prototype"多例

setter注入 constructor注入

所谓属性注入就是给一个对象所依赖的对象注入属性
比如你想造一辆车就要有轮子,车架,车灯,
轮子多大呢?什么样的车架?也就是说你不能单单说需要什么,而应该具体。
在这里就是你创建一个实体student对象,你给出学生的各个属性默认值。
至于怎么给,spring通过调用student的set方法就称为setter注入,使用构造器就是构造器注入。所以使用这两种注入方法你要给出对用的set和构造器吧。
在这里插入图片描述

通过核心容器获取对象

通过这个实现类ClassPathXmlApplicationContext获取核心容器
然后再通过核心容器获取交由spring管理的bean
获取bean三种方式
1.通过唯一标识,也就是spring配置对象的id。
2.通过全类名.class获取对象
但是有一个问题 : 如果有配置相对应的多个类的。那么spring也不知道把哪一个返回了
3.通过唯一标识+全类名.class

package com.test;


import com.atguigu.spring.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCtest {
    
    
    /**
     * 获取bean三种方式
     * 根据唯一标识id获取  注意,根据类型获取besan时,要求IOC中有且只有一个类型匹配的bean
     * 根据类型获取 全类名.class
     *
     */
    @Test
    public void testIOC(){
    
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据id获取bean对象
        Student studentOne =(Student) applicationContext.getBean("StudentOne");
        System.out.println(studentOne);
        Student student = applicationContext.getBean(Student.class);
        System.out.println(student==studentOne);

    }

    /**
     * 测试set注入
     */
    @Test
    public void testIOCSet(){
    
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据id获取bean对象
        Student studentOne =applicationContext.getBean("Studenttwo",Student.class);
        System.out.println(studentOne);
    }
    /**
     * 测试构造器注入
     */
    @Test
    public void testIOCconstor(){
    
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据id获取bean对象
        Student student =applicationContext.getBean("StudentThree",Student.class);
        System.out.println(student);
    }
}

DI依赖注入

依赖注入就是将一个类的某个属性完成赋值的一个过程。setter和构造器注入之间把简单类型的值做一个值注入。还有为类类型的依赖注入,数组的依赖注入,集合的依赖注入。

bean的作用域

在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围
singleton默认) 在IOC容器中,这个bean的对象始终为单实例 。 创建对象的时机在IOC容器初始化时。
prototype 这个bean在IOC容器中有多个实例 。创建对象的时机在获取bean时。

如果是在WebApplicationContext环境下还会有另外两个作用域(但不常用)
request 在一个请求范围内有效
session 在一个会话范围内有效

猜你喜欢

转载自blog.csdn.net/zmm0628/article/details/127271885
今日推荐