首页 > 编程语言 > 详细

【spring源码学习】BeanPostProcessor接口学习

时间:2017-07-19 17:20:12      阅读:195      评论:0      收藏:0      [点我收藏+]

一:含义作用

==>BeanPostProcessor接口是众多Spring提供给开发者的bean生命周期内自定义逻辑拓展接口中的一个

 

二:接口定义

技术分享
package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;

public interface BeanPostProcessor {

    /**
     *IOC容器中的bean实例化之前执行
     */
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

    /**
     *IOC容器中的bean实例化之后执行
     */
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}
View Code

三:自定义实例(在ioc实例化的时候执行,并打印内容。xml配置bean,或用注解注释,即可生效)

技术分享
package com.mobile.thinks.manages.study;

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

@Component
public class MyBeanPostProcessor implements BeanPostProcessor{

    /**
     * IOC容器中bean在被实例化之前执行该方法
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
            Class<?>  cls=bean.getClass();
            System.out.println("sxf  【自定义实例化之前】postProcessBeforeInitialization类路径==>"+cls);
            System.out.println("sxf  【自定义实例化之前】postProcessBeforeInitialization初始化对象的名字==>"+beanName);
        return bean;
    }

    
    /**
     * IOC容器中bean在被实例化之后执行该方法
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        Class<?>  cls=bean.getClass();
        System.out.println("sxf  【自定义实例化之后】postProcessBeforeInitialization类路径==>"+cls);
        System.out.println("sxf  【自定义实例化之后】postProcessBeforeInitialization初始化对象的名字==>"+beanName);
        return bean;
    }
    
    

}
View Code

 

四:spring内部例子

【spring源码学习】BeanPostProcessor接口学习

原文:http://www.cnblogs.com/shangxiaofei/p/7206620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!