码迷,mamicode.com
首页 > 编程语言 > 详细

Spring IoC概念

时间:2020-01-04 23:58:22      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:开发者   spring框架   throws   创建   odi   好的   调用   内容   err   

Spring Ioc是Spring框架的基础,本文会简单的介绍下Spring Ioc。

Sprong Ioc即控制反转,它是一种通过描述(在java中可以是XML或注解)并通过第三方去产生或获取特定对象的方式。

Spring IoC容器

1、Spring IoC容器的设计

       Spring IoC容器的设计主要是基于BeanFactory和ApplicationContext这两个接口,其中ApplicationContext是BeanFactory的一个子接口。也就是说,BeanFactory是Spring IoC容器定义的最底层接口,而ApplicationContext是其高级接口之一,因此大部分情况下会使用后者作为Spring IoC容器。

1.1 ClassPathXmlAppLicationContext

       首先我们来认识一下ApplicationContext的子类ClassPathXmlAppLicationContext。先创建一个.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-4.0.xsd">
    <bean id="source" class="com.ssm.chapter.pojo.Source">
        <property name="fruit" value="橙汁" />
        <property name="sugar" value="少糖" />
        <property name="size" value="大杯" />
    </bean>
    <bean id="juiceMaker" class="com.ssm.chapter.pojo.JuiceMaker" >
        <property name="beverageShop" value="贡茶" />
        <property name="source" ref="source" />
    </bean>
    </beans>

这里定义了两个bean,这样Spring IoC容器在初始化的时候就可以找到它们,然后使用ClassPathXmlAppLicationContext容器就可以将其初始化,代码清单如下:

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml");
        JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean("juiceMaker");
        System.out.println(juiceMaker.makeJuice());

这样就会使用Application的实现类ClassPathXmlAppLicationContext去初始化Spring IoC,然后开发者就可以通过Ioc容器获取资源了。

1.2 Spring Bean的生命周期

       Spring IoC容器的本质就是为了管理Bean。生命周期主要是为了了解Spring IoC容器初始化和销毁Bean的过程,通过对它的学习就可以知道如何在初始和销毁的时候加入自定义的方法,以满足特定的需求。注:Spring IoC容器初始化和销毁Bean的过程我这里就不介绍了啊,在网上很容易找到,这里主要是通过代码去实现生命周期的过程。

       除了了解生命周期的步骤之外,还要知道生命周期的接口是针对设么而言的,首先介绍生命周期的步骤:

      ①如果Bean实现了接口BeanNameAware,那么就会调用setBeanName方法。

      ②如果Bean实现了接口BeanFactoryAware,那么就会调用setBeanFactory方法。

      ③如果Bean实现了接口ApplicationContextAware,且Spring IoC容器也是ApplicationContext的一个实现类,那么就会调用setApplicationContext方法。

      ④如果Bean实现了接口BeanPostProcessor的,那么就会调用postProcessBeforeInitialization方法。

      ⑤如果Bean实现了接口BeanFactoryPostProcess,那么就会调用afterPropertiesSet方法。

      ⑥如果Bean自定义了初始化方法,它就会地用用已定义的初始化方法。

      ⑦如果Bean实现了接口BeanPostProcessor,那么就会调用postProcessAfterInitialization方法,之后这个bean就会完成了初始化,开发者就可以从Spring IoC中获取Bean的服务。

      ⑧如果Bean实现了接口DisposableBean,那么就会调用destroy的方法。

      ⑨如果定义了自定义销毁方法,那么就会调用它。

此外,上面大部分的接口是针对单个Bean而言的;而BeanPostProcessor接口则是针对所有Bean而言的。为了测试BeanPostProcessor接口,可以写一个实现类:

package com.ssm.chapter.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class BeanPostProcessorImpl implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("[" + bean.getClass().getSimpleName() + "]对象" + beanName + "开始初始化");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("[" + bean.getClass().getSimpleName() + "]对象" + beanName + "实例化完成");
        return bean;
    }
}

这样BeanPostProcessor就被我们用代码实现了,他会处理Spring IoC容器中的所有Bean。

为了更好的展示生命周期的内容,将上面的代码中JuiceMaker类进行修改:

package com.ssm.chapter.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class JuiceMaker implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean{

    private String beverageShop = null;

    private Source source = null;

    public String getBeverageShop() {
        return beverageShop;
    }

    public void setBeverageShop(String beverageShop) {
        this.beverageShop = beverageShop;
    }

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    public void init() {
        System.out.println("[" + this.getClass().getSimpleName() + "]执行自定义初始化方法");
    }

    public void myDestroy() {
        System.out.println("[" + this.getClass().getSimpleName() + "]执行自定义销毁方法");
    }

    public String makeJuice() {
        String juice = "这是一杯由" + beverageShop + "饮品店,提供的" + source.getSize() +source.getSugar() +
                source.getFruit();
        return juice;
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("[" + this.getClass().getSimpleName() + "]调用BeanNameAware接口的setBeanName方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("[" + this.getClass().getSimpleName() + "]调用BeanFactoryAware接口的setBeanFactory方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("[" + this.getClass().getSimpleName() + "]调用ApplicationContextAware接口的setApplicationContext方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("[" + this.getClass().getSimpleName() + "]调用DisposableBean接口的destroy方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("[" + this.getClass().getSimpleName() + "]调用InitializingBean接口的afterPropertiesSet方法");
    }
}

这个类实现了所以生命周期中的方法,以便以观察生命周期,其中init方法是自定义的初始化方法,而myDestroy方法是自定义的销毁方法,为了进一步使用这两个自定义方法,在描述Bean的时候,也要在.xml中进行如下声明:

<bean id="beanPostProcessor"
          class="com.ssm.chapter.bean.BeanPostProcessorImpl" />
    <bean id="source" class="com.ssm.chapter.pojo.Source">
        <property name="fruit" value="橙汁" />
        <property name="sugar" value="少糖" />
        <property name="size" value="大杯" />
    </bean>
    <bean id="juiceMaker" class="com.ssm.chapter.pojo.JuiceMaker" init-method="init" destroy-method="myDestroy">
        <property name="beverageShop" value="贡茶" />
        <property name="source" ref="source" />
    </bean>

这里定义了id为JuiceMaker的Bean,其属性init-menth就是自定义的初始化方法,而destroy-method为自定义的销毁方法。下面是测试代码清单:

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml");
        JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean("juiceMaker");
        System.out.println(juiceMaker.makeJuice());
        ctx.close();

日志如下:

[Source]对象source开始初始化
[Source]对象source实例化完成
[JuiceMaker]调用BeanNameAware接口的setBeanName方法
[JuiceMaker]调用BeanFactoryAware接口的setBeanFactory方法
[JuiceMaker]调用ApplicationContextAware接口的setApplicationContext方法
[JuiceMaker]对象juiceMaker开始初始化
[JuiceMaker]调用InitializingBean接口的afterPropertiesSet方法
[JuiceMaker]执行自定义初始化方法
[JuiceMaker]对象juiceMaker实例化完成
这是一杯由贡茶饮品店,提供的大杯少糖橙汁
[JuiceMaker]调用DisposableBean接口的destroy方法
[JuiceMaker]执行自定义销毁方法

从日志中可以看出,生命周期中的方法都被执行了。也可以看到BeanPostProcessor针对的是全部Bean。我们也可以自定义初始化和销毁Bean的方法。

Spring IoC概念

标签:开发者   spring框架   throws   创建   odi   好的   调用   内容   err   

原文地址:https://www.cnblogs.com/pusan/p/12150701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!