IOC容器在Spring中的实现(基于XML管理Bean)

  IOC容器在Spring中的实现

    基于XML管理Bean

               Spring 的 IOC 容器就是 IOC 思想的一个落地的产品实现。

               IOC 容器中管理的组件也叫做 bean。在创建 bean 之前,首先需要创建 IOC 容器。

        Spring 提供了 IOC 容器的两种实现方式:

              ①BeanFactory接口:

                             这是 IOC 容器的基本实现,是 Spring 内部使用的接口。

                              面向 Spring 本身,不提供给开发人员使用。

              ②ApplicationContext接口:

                                    BeanFactory 的子接口,提供了更多高级特性。面向 Spring 的使用者,

                                     几乎所有场合都使用 ApplicationContext 而不是底层的 BeanFactory。

                  以后在 Spring 环境下看到一个类或接口的名称中包含 ApplicationContext,

                 那基本就可以断定,这个类或接口与 IOC 容器有关。 对我们开发人员来说,

                 ApplicationContext就是代表整个IOC容器技术体系的顶级接口。

      ③ApplicationContext的主要实现类:

 

           FileSystemXmlApplicationContext:

                           以磁盘文件中某个xml通过一个磁盘路径获取当前ioc容器,

              所以这个不用: 因为java工程要打jar包,需要在其他电脑上执行,

                                      其他电脑上相对应磁盘路径中也要有当前文件但是不一定有,

               所以现在用ClassPathXmlApplicationContext。

 

类型名简介
ClassPathXmlApplicationContext 通过读取类路径下的 XML 格式的配置文件创建 IOC 容器对象
FileSystemXmlApplicationContext 通过文件系统路径读取 XML 格式的配置文件创建 IOC 容器对象
ConfigurableApplicationContext ApplicationContext 的子接口,包含一些扩展方法 refresh() 和 close() ,让 ApplicationContext 具有启动、关闭和刷新上下文的能力。
WebApplicationContext 专门为 Web 应用准备,基于 Web 环境创建 IOC 容器对象,并将对象引入存入 ServletContext 域中。

  实验一:创建bean

     1 . 目标

          由 Spring 的 IOC 容器创建类的对象。

 

      引入依赖:spring-webmvc(或者下面的)

<dependencies>
   <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
   <!--
   spring-context:上下文
       ApplicationContext:应用上下文
       -->
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>5.3.1</version>
   </dependency>
   
   <!-- junit测试 -->
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
       <scope>test</scope>
   </dependency>
   
   <!-- 日志 -->
   <dependency>
       <groupId>ch.qos.logback</groupId>
       <artifactId>logback-classic</artifactId>
       <version>1.2.3</version>
   </dependency>
   
   <!-- Lombok -->
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <version>1.18.12</version>
       <scope>provided</scope>
   </dependency>
</dependencies>

 

     2. 创建组件类

              这里我们创建的这个类,就是我们希望由IOC容器创建对象的类:

package com.atguigu.spring.pojo;

public class HelloWorld {
   public void sayHello(){
       System.out.println("hello,spring!");
  }
}

    3. 创建 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
bean:配置一个bean对象,将对象交给IOC容器管理
id:bean的唯一标识不能重复
class为什么要设置:因为一个bean对应一个对象:对象从哪来,从类而来
这个就是当前把HelloWorld类型的对象交给了IOC容器来管理
(class:设置bean对象所对应的类型)
-->
   <bean id="helloWorld" class="com.atguigu.spring.pojo.HelloWorld"></bean>
</beans>
  • bean标签:通过配置bean标签告诉IOC容器需要创建对象的组件是什么

  • id属性:bean的唯一标识

  • class属性:组件类的全类名

  4. 测试类

package com.atguigu.spring.test;
import com.atguigu.spring.pojo.HelloWorld;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloworldTest {
   @Test
   public void test(){
       //获取IOC容器
       ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
       //获取IOC容器中的bean对象
       //通过id获取的,不知道类型,要强转
       HelloWorld helloworld = (HelloWorld) ioc.getBean("helloWorld");
       helloworld.sayHello();
       /**
        * hello,spring!
        * 因为我们把对象交给了IOC管理,获取完IOC容器,就可以获取IOC容器对象,调用对象中的方法
        */
  }
}
 
posted @ 2022-11-10 19:54  zjw_rp  阅读(74)  评论(0)    收藏  举报