栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring的BeanPostProcessor扩展点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring的BeanPostProcessor扩展点

BeanPostProcessor postProcessBeforeInitialization
    
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
postProcessAfterInitialization
    
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
InstantiationAwareBeanPostProcessor

BeanPostProcessor的子接口,它添加了实例化前回调,以及实例化后但在设置显式属性或自动装配之前的回调。

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
}
postProcessBeforeInstantiation
    //在目标 bean 被实例化之前回调该方法
    //返回的 bean 对象可能是一个代理来代替目标 bean,有效地抑制目标 bean 的默认实例化。
	@Nullable
	default Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
		return null;
	}
postProcessAfterInstantiation
    //在bean被实例化(通过构造函数或工厂方法)之后,但在Spring属性填充(来自显式属性或自动装配)发生之前,回调该方法
    //这是在 Spring 的自动装配开始之前对给定 bean 实例执行自定义字段注入的理想回调。
    //返回true,表示应该在bean上设置属性,该接口的正常实现类应该返回true
    //返回false,表示应该跳过属性填充。
    //返回false还将阻止在此bean实例上调用任何后续InstantiationAwareBeanPostProcessor 实例。
	default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		return true;
	}
postProcessProperties
    
	@Nullable
	default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
			throws BeansException {

		return null;
	}
postProcessPropertyValues
    
    @Deprecated
	@Nullable
	default PropertyValues postProcessPropertyValues(
			PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
		return pvs;
	}
SmartInstantiationAwareBeanPostProcessor
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {}
predictBeanType
	//预测最终从该处理器的 {@link #postProcessBeforeInstantiation} 回调返回的 bean 的类型
    @Nullable
	default Class predictBeanType(Class beanClass, String beanName) throws BeansException {
		return null;
	}
determineCandidateConstructors
	//确定用于给定 bean 的候选构造函数
	@Nullable
	default Constructor[] determineCandidateConstructors(Class beanClass, String beanName)
			throws BeansException {
		return null;
	}
getEarlyBeanReference
	//获取对指定 bean 的早期访问的引用,通常用于解析循环引用。
    //回调方法使post-processors有机会尽早暴露包装器 - 即在目标 bean 实例完全初始化之前。
    //暴露的对象应该等同于 {@link #postProcessBeforeInitialization} / {@link #postProcessAfterInitialization} 否则会暴露的对象。
    //请注意,此方法返回的对象将用作 bean 引用,除非post-processor从所述后处理回调返回不同的包装器。
    default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
		return bean;
	}
CommonAnnotationBeanPostProcessor postProcessProperties
	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
		Injectionmetadata metadata = findResourcemetadata(beanName, bean.getClass(), pvs);
		try {
			metadata.inject(bean, beanName, pvs);
		}
		catch (Throwable ex) {
			throw new BeanCreationException(beanName, "Injection of resource dependencies failed", ex);
		}
		return pvs;
	}
postProcessPropertyValues
    @Deprecated
	@Override
	public PropertyValues postProcessPropertyValues(
			PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
		return postProcessProperties(pvs, bean, beanName);
	}
AutowiredAnnotationBeanPostProcessor postProcessProperties
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
   Injectionmetadata metadata = findAutowiringmetadata(beanName, bean.getClass(), pvs);
   try {
      metadata.inject(bean, beanName, pvs);
   }
   catch (BeanCreationException ex) {
      throw ex;
   }
   catch (Throwable ex) {
      throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
   }
   return pvs;
}
postProcessPropertyValues
	@Deprecated
	@Override
	public PropertyValues postProcessPropertyValues(
			PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {

		return postProcessProperties(pvs, bean, beanName);
	}
AbstractAutoProxyCreator

SmartInstantiationAwareBeanPostProcessor接口继承自InstantiationAwareBeanPostProcessor接口。

因此AbstractAutoProxyCreator一般是通过InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法和postProcessBeforeInstantiation方法来返回代理对象;

postProcessBeforeInstantiation阶段:前置处理器一般返回null,除非有TargetSource,就是对象已经创建了,则直接返回改beanpostProcessAfterInitialization阶段:如果在postProcessBeforeInstantiation阶段返回null,则待对象创建并初始化完成,再通过postProcessAfterInitialization方法使用动态代理根据实例对象创建增强的动态代理对象。

public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
}
postProcessBeforeInstantiation
	public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
		Object cacheKey = getCacheKey(beanClass, beanName);

		if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
			if (this.advisedBeans.containsKey(cacheKey)) {
				return null;
			}
			if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
				this.advisedBeans.put(cacheKey, Boolean.FALSE);
				return null;
			}
		}

		// Create proxy here if we have a custom TargetSource.
		// Suppresses unnecessary default instantiation of the target bean:
		// The TargetSource will handle target instances in a custom fashion.
        // 如果我们有一个自定义的TargetSource,则在这里创建代理
		TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
		if (targetSource != null) {
			if (StringUtils.hasLength(beanName)) {
				this.targetSourcedBeans.add(beanName);
			}
			Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
            // 创建动态代理对象
			Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}
		return null;
	}
postProcessAfterInstantiation
	public boolean postProcessAfterInstantiation(Object bean, String beanName) {
		return true;
	}
getEarlyBeanReference
   //获取提前暴露出来的bean的引用 
   public Object getEarlyBeanReference(Object bean, String beanName) {
        //生成该bean对应的cacheKey
        Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
        //将该bean放入到earlyProxyReferences中用于表示该Bean执行过AOP
        this.earlyProxyReferences.put(cacheKey, bean);
        //如果需要的话,对给定的bean进行封装,例如,该bean需要被代理
        return this.wrapIfNecessary(bean, beanName, cacheKey);
    }
postProcessAfterInitialization
    //Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.
    //如果 bean 被子类标识为代理,则使用配置的拦截器创建一个代理。
    public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
        if (bean != null) {
            Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
            //如果该bean未执行过AOP,则进行封装,如果执行过,则不再进行封装
            if (this.earlyProxyReferences.remove(cacheKey) != bean) {
                return this.wrapIfNecessary(bean, beanName, cacheKey);
            }
        }
        return bean;
    }
wrapIfNecessary
   
	protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
		if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
			return bean;
		}
		if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
			return bean;
		}
		if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
			this.advisedBeans.put(cacheKey, Boolean.FALSE);
			return bean;
		}

		// Create proxy if we have advice.
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
		if (specificInterceptors != DO_NOT_PROXY) {
			this.advisedBeans.put(cacheKey, Boolean.TRUE);
			Object proxy = createProxy(
					bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}

		this.advisedBeans.put(cacheKey, Boolean.FALSE);
		return bean;
	}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号