Spring注解驱动开发实战 | 第十一篇:生命周期-BeanPostProcessor后置处理器的使用

bean的后置处理器,在bean初始化前后进行一些处理工作。
重载了接口BeanPostProcessor的两个方法postProcessBeforeInitialization和postProcessAfterInitialization
postProcessBeforeInitialization:在初始化之前工作
postProcessAfterInitialization:在初始化之后工作

 

在com.wsc.bean下创建MyBeanPostProcessor类并实现BeanPostProcessor接口,重载两个方法:postProcessBeforeInitialization和postProcessAfterInitialization,最后用@Component注册到容器中

package com.wsc.bean;

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

/**
 * 后置处理器:初始化前后进行处理工作
 * 将后置处理器加入到容器中
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

	
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
		return bean;
	}


	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
		return bean;
	}

}

在MainConfigOfLifeCycle.java中使用@ComponentScan("com.wsc.bean")注解扫描刚才注册的组件

package com.wsc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.wsc.bean.Car;
@ComponentScan("com.wsc.bean")
@Configuration
public class MainConfigOfLifeCycle {
	
	//@Scope("prototype")
	@Bean(initMethod="init",destroyMethod="detory")
	public Car car(){
		return new Car();
	}

}

在IOCTest_LifeCycle.java中添加测试方法testBeanPostProcessor

package com.wsc.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.wsc.config.MainConfigOfLifeCycle;

public class IOCTest_LifeCycle {

	@Test
	public void testLifeCycleSingleton(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		//关闭容器
		applicationContext.close();
	}
	
	@Test
	public void testLifeCyclePrototype(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		
		applicationContext.getBean("car");
		//关闭容器
		applicationContext.close();
	}
	
	@Test
	public void testInitializingBeanDisposableBean(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		
		//关闭容器
		applicationContext.close();
	}
	
	@Test
	public void testPostConstructPreDestroy(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		//关闭容器
		applicationContext.close();
	}
	
	@Test
	public void testBeanPostProcessor(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		//关闭容器
		applicationContext.close();
	}


}

运行测试方法testBeanPostProcessor

Spring注解驱动开发实战 | 第十一篇:生命周期-BeanPostProcessor后置处理器的使用_第1张图片

--------------------------------

源码下载

 

你可能感兴趣的:(spring注解,Spring注解驱动开发实战)