zoukankan      html  css  js  c++  java
  • BeanPostProcessor Bean实例的初始化前后的自定义修改

    BeanPostProcessor接口的行为方法

    public interface BeanPostProcessor {
    
        /**
         * Apply this BeanPostProcessor to the given new bean instance <i>before</i> 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 <i>after</i> 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.
         * <p>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.
         * <p>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();
        }
  • 相关阅读:
    得到内网可用的SqlServer 及某数据库下的表及其他的架构
    VS2005 XML注释生成XML文档文件
    华表 单元格公式设定与计算
    自定义控件开发示例二
    自定义控件的 Enum类和Color类 属性的公开设定
    入门者初试 Cell(华表)结合C#的应用
    VS2005 + VSS6.0 简单应用示例
    SQL2000联机丛书:使用和维护数据仓库
    VS2005 通过SMO(SQL Management Objects) 管理 数据库的作业 警报 备份 等任务
    SQL2000联机丛书:基本 MDX
  • 原文地址:https://www.cnblogs.com/BINGJJFLY/p/9055454.html
Copyright © 2011-2022 走看看