美文网首页
第二章 IoC容器概述

第二章 IoC容器概述

作者: 我是阿喵酱 | 来源:发表于2018-02-04 16:29 被阅读0次

IoC容器

Spring模块架构图-IoC容器 IoC容器处于Spring模块架构比较核心的地位,它向上层的AOP、数据访问、Web等框架提供基本的支持。

输入图片说明

IoC容器主要要完成的功能,主要创建对象依赖,组装成我们需要的业务对象。

输入图片说明

从容器角度观察IoC容器


输入图片说明

配置 空配置的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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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">

</beans>

注解配置Controller

package com.netease.course.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/hello")
public class HelloController {

    @RequestMapping(value = "/spring")
    public void spring(HttpServletResponse response) throws IOException {
        System.out.println("Class HelloController Method spring");
        response.getWriter().write("Hello, Spring Web!!");
    }
}

IoC容器 = ? ApplicationContext就是一个IoC容器。ApplicationContext属于org.springframework.context,属于spring-context模块。如果只适用IoC容器的相关功能则只需要在Maven中添加spring-context模块。

初始化IoC 我们只是在web.xml中,的context-param添加contextConfigLocation对象和对应xml路径,在Listener当中引用org.springframework.web.context.ContextLoaderListener,这种方式是初始化Spring的一种方式。 web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
        classpath:application-context.xml </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

ClassPathXmlApplicationContext

ApplicationContext context= new ClassPathXmlApplicationContext("application-context.xml");

FileSystemXmlXmlApplicationContext

ApplicationContext context= new FileSystemXmlApplicationContext("/home/zhanpeng/Workspace/J2ee/spring-web/src/resources/application-context.xml");

输入图片说明

Bean定义 如果定义Bean,在Spring配置中,通过简单的配置文件就可以配置Bean对象。例如如下代码

public class ScrewDriver{

    public void use(){
        System.out.println("Use screwDriver");
}

在Spring的配置文件中添加如下内容,这里id代表这个类Bean对象的唯一标识符,class代表这个类的路径,也就是说任何Java类都可以生成Bean对象,交给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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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="screwDriver" class="com.netease.course.ScrewDriver" >
    </bean>
</beans>

Bean使用

  • 首先需要初始化容器
  • 通过getBean方法,获取对象
  • 使用Bean对象的方法
//初始化容器
ApplicationContext context = new ClassPathXmlApplicationContext("application-context");

//获取对象
ScrewDriver screwDriver = context.getBean("screwDriver",ScrewDriver.class);

//使用Bean对象
screwDriver.use();
//

Bean作用域

什么是Bean作用域? 强调Bean的有效范围,我们也需要考虑Bean是什么时候创建、什么时候销毁的。

Spring Bean作用域

  • singleton
  • prototype

以下作用域依赖Web应用

  • request
  • session
  • global sesson
  • application

singleton 默认的作用域就是singleton,类似单例模式,对象只有单例

<bean id="screwDriver" class="com.netease.course.ScrewDriver"></bean>

明确定义singleton

<bean id="screwDriver" class="com.netease.course.ScrewDriver" scope="singleton"></bean>

这种方式保证对象在整个容器中是只有一个单例的,保证在整个生命周期中只会创建出一个实例
通过代码方式检查单例模式

        ScrewDriver screwDriver = context.getBean("screwDriver", ScrewDriver.class);
        System.out.println(screwDriver);
        ScrewDriver screwDriver2 = context.getBean("screwDriver", ScrewDriver.class);
        System.out.println(screwDriver2);

执行结果

com.netease.course.ScrewDriver@12405818
com.netease.course.ScrewDriver@12405818

prototype 每次引用都需要创建对象,就使用prototype,比如两次饮用时需要保存一些状态,但是在下次引用时并不需要该状态带入到实例中。

<bean id="screwDriver" class="com.netease.course.ScrewDriver" scope="prototype"></bean>

每次引用创建一个实例
通过代码方式检查多次引用

        ScrewDriver screwDriver = context.getBean("screwDriver", ScrewDriver.class);
        System.out.println(screwDriver);
        ScrewDriver screwDriver2 = context.getBean("screwDriver", ScrewDriver.class);
        System.out.println(screwDriver2);

执行结果

com.netease.course.ScrewDriver@12405818
com.netease.course.ScrewDriver@314c508a

每次引用创建一个实例

Web场景下的Bean作用域

request request scope,作为访问者访问Web服务就是为一个request scope,在请求处理过程中,需要创建bean,但是在另外一个访问Web服务的请求时,创建另外的Bean。

session session scope包含多个request scope,表示某个用户在访问过程中,会共享一些Bean对象,例如sessionBean,比如保存用户的会话信息或者状态信息。在用户没有退出的时候,这些Bean都是有效的。多个Session的话会使用不同的sessionBean。

application 在整个应用状态下会共享某个单独的Bean,比如一些ServiceBean。

输入图片说明

Bean生命周期回调 有了Bean的作用域,就可以控制Bean的创建和销毁,但并不知道我们在创建时可以做那些事情,也不知道销毁时可以做那些事情。一般我们在编写一些和资源打交道的服务和功能的时候,很有可能在创建或者销毁的时候做些额外的事情,比如当创建时申请资源:申请线程池或者申请文件系统资源,当销毁时,我们对资源进行释放。

  • 创建
    • 申请资源
  • 销毁
    • 释放资源

Spring生命周期回调

  • 创建
public interface InitializingBean{
    void afterPropertiesSet() throws Exception;
}

在初始化结束后(在Spring对该对象设置完成属性后)添加一些执行内容。
我们可以通过简单的xml方式配置

<bean id="screwDriver" class="com.netease.course.ScrewDriver" init-method="init"></bean>

xml方式所对应的初始化代码如下:

package com.netease.course;
import javax.annotation.Resource;

public class ScrewDriver {
    public void init()
    {
        System.out.println("Init screwDriver");
    }
}

  • 销毁
public interface DisposableBean{

    void destroy() throws Exception;
}
```我们可以通过简单的xml方式配置

<bean id="screwDriver" class="com.netease.course.ScrewDriver" destroy-method="cleanup"></bean>

xml方式所对应的初始化代码如下:

package com.netease.course; import javax.annotation.Resource;

public class ScrewDriver { public void cleanup() { System.out.println("Destroy screwDriver"); } }


##通过代码查看回调情况

package com.netease.course;

import javax.annotation.Resource;

public class ScrewDriver {

private String color = "red";

public void use() {

// System.out.println("Use " + color + " screwdriver"); }

public void setColor(String color)
{
    this.color = color;
}

public void init()
{
    System.out.println("Init " + this);
}

public void cleanup()
{
    System.out.println("Destroy " + this);
}

}

执行结果,并没有看到销毁Bean对象

Init com.netease.course.ScrewDriver@25359ed8 use red screwdriver

这是由于我们并没有有效的关闭容器,导致容器没有办法调用到destory-method,这里需要手动添加手动关闭容器。ApplicationContext本身并不支持关闭,我们需要把它强制转化到支持close的方式并close。    
**注意**:笔者这里也没有调用到销毁。

转自:https://my.oschina.net/hava/blog/757200

相关文章

  • spring-core 5.1.7文档 目录

    目录 1.IOC容器 1.1 介绍Spring IoC容器和Beans 1.2 容器概述 1.3 Bean概述 1...

  • 一、Spring核心篇

    第2章Spring Framework的核心:IoC容器的实现 2.1Spring IoC容器概述 1.IOC容器...

  • Spring学习手册(3)—— bean实例化配置

    Spring学习手册(2)—— IOC容器介绍了IOC概念以及Spring关于IOC容器的实现方式,最后概述了Sp...

  • Ioc容器概述

    Ioc概念 很多初学者在刚听到Ioc和DI等概念的时候,往往一头雾水,用了很久才理解它们的意思,这里我说一下我对它...

  • 第二章 IoC容器概述

    IoC容器 Spring模块架构图-IoC容器 IoC容器处于Spring模块架构比较核心的地位,它向上层的AOP...

  • spring-ioc容器

    IOC(inverse of control,控制-反转) 一、ioc概述 1.ioc的类型 2.通过容器完成依赖...

  • 2018-10-28

    IOC容器概述:容器是什么在Spring中,org.springframwork.context.Applicat...

  • 资深架构师带你详细了解,Spring之IoC容器

    一、 IoC概述 IoC(Inverse of Control,控制反转)是Spring容器的内核,AOP、声明式...

  • Spring IOC容器概述

    1.反射: Java语言允许通过程序化的方式间接对Class对象实例操作,Class文件由类装载器装在后,在JVM...

  • Spring 框架学习(三):IoC 容器

    Spring 框架学习(三):IoC 容器 概述 IoC 容器的核心是依赖反转模式。许多应用都是由两个或多个类通过...

网友评论

      本文标题:第二章 IoC容器概述

      本文链接:https://www.haomeiwen.com/subject/cdkrzxtx.html