博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BeanPostProcessor Bean实例的初始化前后的自定义修改
阅读量:4954 次
发布时间:2019-06-12

本文共 4541 字,大约阅读时间需要 15 分钟。

BeanPostProcessor接口的行为方法

public interface BeanPostProcessor {    /**     * Apply this BeanPostProcessor to the given new bean instance before any bean     * initialization callbacks (like InitializingBean's {
@code afterPropertiesSet} * or a custom init-method). The bean will already be populated with property values. * The returned bean instance may be a wrapper around the original. * @param bean the new bean instance * @param beanName the name of the bean * @return the bean instance to use, either the original or a wrapped one; if * {
@code null}, no subsequent BeanPostProcessors will be invoked * @throws org.springframework.beans.BeansException in case of errors * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet */ Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; /** * Apply this BeanPostProcessor to the given new bean instance after any bean * initialization callbacks (like InitializingBean's {
@code afterPropertiesSet} * or a custom init-method). The bean will already be populated with property values. * The returned bean instance may be a wrapper around the original. *

In case of a FactoryBean, this callback will be invoked for both the FactoryBean * instance and the objects created by the FactoryBean (as of Spring 2.0). The * post-processor can decide whether to apply to either the FactoryBean or created * objects or both through corresponding {

@code bean instanceof FactoryBean} checks. *

This callback will also be invoked after a short-circuiting triggered by a * {

@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, * in contrast to all other BeanPostProcessor callbacks. * @param bean the new bean instance * @param beanName the name of the bean * @return the bean instance to use, either the original or a wrapped one; if * {
@code null}, no subsequent BeanPostProcessors will be invoked * @throws org.springframework.beans.BeansException in case of errors * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet * @see org.springframework.beans.factory.FactoryBean */ Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;}

自定义的BeanPostProcessor

package com.wjz.spring;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import com.wjz.core.CustomInitializable;public class CustomBeanPostProcessor implements BeanPostProcessor {    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {        if (bean instanceof CustomInitializable) {            System.out.println("before init......");            ((CustomInitializable) bean).init();        }        return bean;    }    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {        System.out.println("after init......");        return bean;    }}

自定义的修改

package com.wjz.core;public abstract interface CustomInitializable {    abstract void init();}
package com.wjz.core;public class Realm implements CustomInitializable {    public void init() {        System.out.println("init......");    }}

关于Shiro框架对于BeanPostProcessor的使用

LifecycleBeanPostProcessor

 

public Object postProcessBeforeInitialization(Object object, String name) throws BeansException {        if (object instanceof Initializable) {            try {                if (log.isDebugEnabled()) {                    log.debug("Initializing bean [" + name + "]...");                }                ((Initializable) object).init();            } catch (Exception e) {                throw new FatalBeanException("Error initializing bean [" + name + "]", e);            }        }        return object;    }    /**     * Does nothing - merely returns the object argument immediately.     */    public Object postProcessAfterInitialization(Object object, String name) throws BeansException {        // Does nothing after initialization        return object;    }
public abstract interface org.apache.shiro.util.Initializable {      public abstract void init() throws org.apache.shiro.ShiroException;}

AuthenticatingRealm

public final void init() {        //trigger obtaining the authorization cache if possible 如果可能,触发获得授权缓存        getAvailableAuthenticationCache();        onInit();    }

 

转载于:https://www.cnblogs.com/BINGJJFLY/p/9055454.html

你可能感兴趣的文章
SONY VPCS138EC降级安装XP
查看>>
被放逐的皇后 金建云
查看>>
Javascript 有用参考函数
查看>>
点群的判别(三)
查看>>
GNSS 使用DFT算法 能量损耗仿真
查看>>
【转】Simulink模型架构指导
查看>>
MYSQL数据库的导出的几种方法
查看>>
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
MapReduce 重要组件——Recordreader组件 [转]
查看>>
webdriver api
查看>>
转载-FileZilla Server源码分析(1)
查看>>
apache 实现图标缓存客户端
查看>>
MediaWiki左侧导航栏通过特殊页面就可以设置。
查看>>