【尚硅谷】spring学习笔记(9):spring IOC 容器可以管理 Bean 的生命周期

  • Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务. 
Spring IOC 容器对 Bean 的生命周期进行管理的过程:
  • 通过构造器或工厂方法创建 Bean 实例
  • 为 Bean 的属性设置值和对其他 Bean 的引用
  • 调用 Bean 的初始化方法
  • Bean 可以使用了


  • 当容器关闭时, 调用 Bean 的销毁方法
  • 在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法.

  • Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理.
  • Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理, 而非单一实例. 其典型应用是: 检查 Bean 属性的正确性或根据特定的标准更改 Bean 的属性.
  • 对Bean 后置处理器而言, 需要实现BeanPostProcessor接口. 在初始化方法被调用前后, Spring 将把每个 Bean 实例分别传递给上述接口的以下两个方法:

public class Car {
	
	public Car() {
		System.out.println("Car的无参构造器");
	}
	
	public void setBrand(String brand) {
		System.out.println("setBrand......");
		this.brand = brand;
	}

	private String brand;
	
	public void init() {
		System.out.println("init......");
	}
	
	public void destroy() {
		System.out.println("destroy......");
	}

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

package com.atguigu.spring.beans.cycle;

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

public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessAfterInitialization:" + bean + "," + beanName);
		Car car = new Car();
		car.setBrand("吉利");
		return car;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessBeforeInitialization:" + bean + "," + beanName);
		if("car".equals(beanName)){
			//...
		}
		return 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id= "car" class="com.atguigu.spring.beans.cycle.Car"
    init-method="init"
    destroy-method="destroy">
         <property name="brand" value="aodi"></property>
    </bean>
    
    <!-- 实现BeanPostProcessor这个接口,并具体提供两个方法的实现
         postProcessBeforeInitialization(Object bean, String beanName)方法:init-method之前被调用
         postProcessAfterInitialization(Object bean, String beanName)方法:init-method之后被调用
         
         bean:bean实例本身
         beanName:IOC容器配置的bean 的名字.
                  还回值:是实际上还回给用户的那个bean,注意:可以在以上两个方法中修改还回的bean,甚至还回一个新的bean
      -->
    <!-- 配置bean的后置处理器:不需要配置id,IOC容器自动识别是一个BeanPostProcessor -->
    <bean class ="com.atguigu.spring.beans.cycle.MyBeanPostProcessor"></bean>
    

    

</beans>

package com.atguigu.spring.beans.cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
	ClassPathXmlApplicationContext apc = new  ClassPathXmlApplicationContext("beans-cycle.xml");
     
		Car car = (Car) apc.getBean("car");
		System.out.println(car);
		
		//关闭IOC
		apc.close();
	}	
}

结果:

六月 10, 2018 5:35:01 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Sun Jun 10 17:35:01 CST 2018]; root of context hierarchy
六月 10, 2018 5:35:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Car的无参构造器
setBrand......
postProcessBeforeInitialization:Car [brand=aodi],car
init......
postProcessAfterInitialization:Car [brand=aodi],car
Car的无参构造器
setBrand......
Car [brand=吉利]
六月 10, 2018 5:35:02 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Sun Jun 10 17:35:01 CST 2018]; root of context hierarchy
destroy......

猜你喜欢

转载自blog.csdn.net/oqkdws/article/details/80643011