Spring全家桶(四)Bean的生命周期

十、Bean生命周期

10.1 Bean的生命周期

Spring IOC容器可以管理Bean的生命周期,也允许在Bean生命周期的特定点执行定制的任务。

Spring IOC容器对Bean的生命周期进行管理的过程如下:

  1. 通过构造器或者工厂方法创建Bean实例
  2. 为Bean的属性设置值和对其他Bean的引用
  3. 调用Bean的初始化方法
  4. 使用Bean
  5. 容器关闭时,调用Bean的销毁方法

在Bean的声明里设置init-method和destory-method属性,为Bean指定初始化和销毁方法。

测试,创建一个Car类:

package com.stuspring.cycle;

/**
 * Created by bee on 17/4/26.
 */
public class Car {
    public Car() {
        System.out.println("Constructor....");
    }

    private String brand;

    public void setBrand(String brand) {
        System.out.println("set Brand...");
        this.brand = brand;
    }

    public void initMethod(){
        System.out.println("init method....");
    }

    public void destoryMethod(){
        System.out.println("destory method...");
    }

}

创建beans-cycle.xml并配置一个bean:

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="com.stuspring.cycle.Car" p:brand="Audi" init-method="initMethod"
          destroy-method="destoryMethod"/>
</bean

测试:

package com.stuspring.cycle;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by bee on 17/4/26.
 */
public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
        Car car = (Car) ctx.getBean("car");
        System.out.println(car);
        ((ConfigurableApplicationContext) ctx).close();
    }
}

运行结果:

Constructor....
set Brand...
init method....
com.stuspring.cycle.Car@604ed9f0
destory method...

10.2 Bean的后置处理器

Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理,Bean后置处理器会对IOC容器中的所有Bean实例逐一处理,要想针对特定Bean进行处理可以根据beanName参数进行判断。Bean后置处理器的典型应用是检查Bean属性的正确性或者根据特定的标准更改Bean的属性。

使用Bean后置处理器需要实现BeanPostProcessor接口,在Bean初始化方法被调用前后,Spring将把每个Bean实例分别传递给Bean接口的Object postProcessBeforeInitialization(Object bean, String beanName)Object postProcessAfterInitialization(Object bean, String beanName)方法。

下面是测试例子:
新建一个Car类:

 package com.stuspring.cycle;

/**
 * Created by bee on 17/4/26.
 */
public class Car {
    private String brand;

    public Car() {
        System.out.println("Constructor....");
    }

    public void setBrand(String brand) {
        System.out.println("set Brand...");
        this.brand = brand;
    }

    public void initMethod() {
        System.out.println("init method....");
    }

    public void destoryMethod() {
        System.out.println("destory method...");
    }


    public String getBrand() {
        return brand;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                '}';
    }
}

创建BeanPostProcessor的实现类:

package com.stuspring.cycle;

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

/**
 * Created by bee on 17/4/27.
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization:    " + bean + "," + beanName);


        if ("car".equals(beanName)){
            Car car=new Car();
            car.setBrand("BaoMa");
            return car;
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        System.out.println("postProcessAfterInitialization: " + bean + "," + beanName);
        return bean;
    }
}

创建Bean:

    <bean id="car" class="com.stuspring.cycle.Car" p:brand="Audi" init-method="initMethod"
          destroy-method="destoryMethod"/>
    <bean class="com.stuspring.cycle.MyBeanPostProcessor"/>

Main函数中测试:

package com.stuspring.cycle;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by bee on 17/4/26.
 */
public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
        Car car = (Car) ctx.getBean("car");
        System.out.println(car.toString());
        System.out.println(car.getBrand());
        ((ConfigurableApplicationContext) ctx).close();
    }
}

运行结果:

Constructor....
set Brand...
postProcessBeforeInitialization:    Car{brand='Audi'},car
Constructor....
set Brand...
init method....
postProcessAfterInitialization: Car{brand='BaoMa'},car
Car{brand='BaoMa'}
BaoMa
destory method...

添加Bean的后置处理器以后Bean的生命周期如下;

  1. 通过构造器或者工厂方法创建Bean实例
  2. 为Bean的属性设置值和对其他Bean的引用
  3. 将Bean实例传递给Bean后置处理器的postProcessBeforeInitialization方法
  4. 调用Bean的初始化方法
  5. 将Bean实例传递给Bean后置处理器的postProcessAfterInitialization方法
  6. 使用Bean
  7. 容器关闭时,调用Bean的销毁方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

esc_ai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值