版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011171125/article/details/85226696
Spring入门学习 第九节
Bean的生命周期
- 创建需要的类
Car.java
package com.fafa.spring.beans.cycle; public class Car { public Car(){ System.out.println("Car's constructor..."); } private String brand; public void setBrand(String brand) { System.out.println("setBrand..."); this.brand = brand; } public void init2(){ System.out.println("init..."); } public void destroy(){ System.out.println("destroy...."); } @Override public String toString() { return "Car [brand=" + brand + "]"; } }
- 创建配置文件
beans-cycle.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.xsd"> <bean id="car" class="com.fafa.spring.beans.cycle.Car" init-method="init2" destroy-method="destroy"> <property name="brand" value="Audi"></property> </bean> <!-- 实现 BeanPostProcessor 接口,并具体提供 Object postProcessBeforeInitialization(Object bean, String beanName):init-method 之前被调用 Object postProcessAfterInitialization(Object bean, String beanName):init-method 之后被调用 两个方法的实现 bean:bean实例本身 beanName:IOC容器配置的bean名字 返回值:是实际上返回给用户的那个bean,注意:可以在以上两个方法中修改返回的bean,甚至返回一个新的bean --> </beans>
- 创建测试方法:
测试结果:@Test public void testCycle(){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:beans-cycle.xml"); Car car = (Car) ctx.getBean("car"); System.out.println(car); ctx.close(); }
Car's constructor... setBrand... init... Car [brand=Audi] 十二月 31, 2018 8:13:08 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@25ed7c33: startup date [Mon Dec 31 20:13:08 CST 2018]; root of context hierarchy destroy....
- 创建一个
MyBeanPostProcessor
类继承自BeanPostProcessor
类,此时可以对特定的一些bean做处理:
在XML中加入:package com.fafa.spring.beans.cycle; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization:" + bean + ", " + beanName); if("car".equals(beanName)){ //... } return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization:" + bean + ", " + beanName); Car car = new Car(); car.setBrand("Ford"); return car; } }
<!-- 配置bean的后置处理器,不需要配置id,IOC容器自动识别是一个BeanPostProcessor--> <bean class="com.fafa.spring.beans.cycle.MyBeanPostProcessor"></bean>
测试结果:
Car's constructor... setBrand... postProcessBeforeInitialization:Car [brand=Audi], car init... postProcessAfterInitialization:Car [brand=Audi], car Car's constructor... setBrand... Car [brand=Ford] destroy....