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(); }