BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化、配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现,然后注册到容器中。、

比如如果我们使用spring整合hibernate,我们可以在容器实例化前先加载hibernate的配置文件:

public class MappingFactoryBean implements FactoryBean {
    private static Logger log = LoggerFactory
            .getLogger(MappingFactoryBean.class);

public Object getObject() throws Exception {
        List<String> configs = ApplicationContext.getContext().getApplication()
                .getMappingConfigs();
        log.debug("************* hibernate configs: " + configs);
        return configs.toArray(new String[configs.size()]);
    }

public Class<?> getObjectType() {
        return String[].class;
    }

public boolean isSingleton() {
        return true;
    }
}

public class MappingAutowiring implements BeanPostProcessor {
    private static Logger log = LoggerFactory
            .getLogger(MappingAutowiring.class);

private String[] mappingResources;

public String[] getMappingResources() {
        return mappingResources;
    }

public void setMappingResources(String[] mappingResources) {
        this.mappingResources = mappingResources;
    }

public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        if (bean instanceof LocalSessionFactoryBean) {
            log.debug("============ MappingAutowiring: " + mappingResources);
            ((LocalSessionFactoryBean) bean)
                    .setMappingResources(mappingResources);
        }
        return bean;
    }

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

beans.xml 配置如下:

<bean id="mappingAutowiring"
        class="xx.MappingAutowiring">
        <property name="mappingResources" ref="mappingResources" />
    </bean>
    <bean id="mappingResources"
        class="xx.MappingFactoryBean" />

关于Spring的BeanPostProcessor的更多相关文章

  1. Spring 的 BeanPostProcessor接口实现

    今天学习了一下Spring的BeanPostProcessor接口,该接口作用是:如果我们需要在Spring容器完成Bean的实例化,配置和其他的初始化后添加一些自己的逻辑处理,我们就可以定义一个或者 ...

  2. Spring中BeanPostProcessor

    Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...

  3. spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)

    在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...

  4. spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...

  5. spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...

  6. Spring之BeanPostProcessor(后置处理器)介绍

      为了弄清楚Spring框架,我们需要分别弄清楚相关核心接口的作用,本文来介绍下BeanPostProcessor接口 BeanPostProcessor   该接口我们也叫后置处理器,作用是在Be ...

  7. Spring 注册BeanPostProcessor 源码阅读

    回顾上一篇博客中,在AbstractApplicationContext这个抽象类中,Spring使用invokeBeanFactoryPostProcessors(beanFactory);执行Be ...

  8. 【框架】利用Spring的BeanPostProcessor来修改bean属性

    一.BeanPostProcessor是什么?什么时候触发?可以用来做什么? 1.它是什么? 首先它是一个接口,定义了两个方法: public interface BeanPostProcessor ...

  9. Spring的BeanPostProcessor和BeanFactoryPostProcessor区别

    Spring提供了两种后处理bean的扩展接口,分别为BeanPostProcessor和BeanFactoryPostProcessor,这两者在使用上是有所区别的. BeanPostProcess ...

随机推荐

  1. iOS 相关职位要求整理版

    在拉勾上找了20家,BOSS直聘找了10家感兴趣的在招聘 iOS 程序员的公司,把职位要求整理了一下. 初创公司一般要求1年以上开发经验,成长型或者成熟型公司一般要求最低2年以上开发经验.这里针对的是 ...

  2. 转: 关于异步promises

    迄今为止,可能每个JavaScript开发者和他们的祖母都听说过Promises.如果你没有,那么你即将会.promises的概念是由CommonJS小组的成员在 Promises/A规范 中提出来的 ...

  3. 59 pages的Delphi源码

    http://www.codesc.net/source/list_10_59.shtml

  4. QListWidget的QComboBox下拉列表添加复选框及消息处理

    要在QComboBox下拉列表项中添加复选框,并进行消息处理,在网上搜索了很久没有找到太多有用的信息和实际的例子,但从中还是找到了一些提示性的资料,根据这些简短的介绍,最终实现了这个功能. QComb ...

  5. php 求水仙花数优化

    水仙花数是指一个n位数(n>=3),它每一个位上数字的n次幂之和等于它本身,n为它的位数.(比如:1^3+5^3+3^3 = 153) 水仙花数又称阿姆斯特朗数. 三位的水仙花数有4个:153, ...

  6. iOS中Block介绍(二)内存管理与其他特性

    我们在前一章介绍了block的用法,而正确使用block必须要求正确理解block的内存管理问题.这一章,我们只陈述结果而不追寻原因,我们将在下一章深入其原因. 一.block放在哪里 我们针对不同情 ...

  7. HTML5API___manifest

    离线缓存 manifest 在html标签里面增加个属性 mainfest 就可以告诉浏览器缓存文件在哪里. <html manifest='show.manifest' xmlns=" ...

  8. oracle 获取系统时间(转)

    Oracle中如何获取系统当前时间   select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;    ORACLE里获取一个时间的年.季. ...

  9. iOS 中多线程的简单使用

    iOS中常用的多线程操作有( NSThread, NSOperation GCD ) 为了能更直观的展现多线程操作在SB中做如下的界面布局: 当点击下载的时候从网络上下载图片: - (void)loa ...

  10. [Swust OJ 581]--彩色的石子(状压dp)

    题目链接:http://acm.swust.edu.cn/problem/0581/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...