您的位置:首页 > 汽车 > 时评 > 专业做小程序开发的公司_网页设计与制作教程电商_2023推广平台_seo网络营销推广

专业做小程序开发的公司_网页设计与制作教程电商_2023推广平台_seo网络营销推广

2024/12/28 18:17:46 来源:https://blog.csdn.net/qq_37398465/article/details/143169716  浏览:    关键词:专业做小程序开发的公司_网页设计与制作教程电商_2023推广平台_seo网络营销推广
专业做小程序开发的公司_网页设计与制作教程电商_2023推广平台_seo网络营销推广

概述

在refresh方法中的prepareBeanFactory方法中,有一个拓展点:addBeanPostProcessor。即通过注入Aware对象从而将容器中的某些值设置到某个bean中。

beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

aware接口调用

invokeAwareMethods

该方法在doCreateBean的initializeBean方法中。
只是BeanNameAware BeanClassLoaderAware BeanFactoryAware时,是通过invokeAwareMethods方法调用的。

	private void invokeAwareMethods(String beanName, Object bean) {if (bean instanceof Aware) {if (bean instanceof BeanNameAware) {((BeanNameAware) bean).setBeanName(beanName);}if (bean instanceof BeanClassLoaderAware) {ClassLoader bcl = getBeanClassLoader();if (bcl != null) {((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);}}if (bean instanceof BeanFactoryAware) {((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);}}}

若是其他aware接口则是通过实现BeanPostProcessor调用的,如ApplicationContextAwareProcessor

ApplicationContextAwareProcessor

class ApplicationContextAwareProcessor implements BeanPostProcessor 

重写了postProcessBeforeInitialization方法,在这里是具体的处理逻辑,调用逻辑是在invokeAwareMethods的applyBeanPostProcessorsBeforeInitialization方法中

	@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){return bean;}AccessControlContext acc = null;if (System.getSecurityManager() != null) {acc = this.applicationContext.getBeanFactory().getAccessControlContext();}if (acc != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareInterfaces(bean);return null;}, acc);}else {invokeAwareInterfaces(bean);}return bean;}

调用aware接口

private void invokeAwareInterfaces(Object bean) {if (bean instanceof EnvironmentAware) {((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());}if (bean instanceof EmbeddedValueResolverAware) {((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);}if (bean instanceof ResourceLoaderAware) {((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);}if (bean instanceof ApplicationEventPublisherAware) {((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);}if (bean instanceof MessageSourceAware) {((MessageSourceAware) bean).setMessageSource(this.applicationContext);}if (bean instanceof ApplicationContextAware) {((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);}}

applyBeanPostProcessorsBeforeInitialization

该方法也在initializeBean中

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareMethods(beanName, bean);return null;}, getAccessControlContext());}else {// 对实现了aware接口的bean执行aware实现方法invokeAwareMethods(beanName, bean);}Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {// 调用BeanPostProcessor的postProcessBeforeInitialization方法的实现wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}....

getBeanPostProcessors方法获取所有的BeanPostProcessors,然后遍历调用postProcessBeforeInitialization方法

	@Overridepublic Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)throws BeansException {Object result = existingBean;for (BeanPostProcessor processor : getBeanPostProcessors()) {Object current = processor.postProcessBeforeInitialization(result, beanName);if (current == null) {return result;}result = current;}return result;}

我们可以在该方法的实现中找到自定义的BeanPostProcessor从而实现拓展
在这里插入图片描述

其他

拓展点: parseCustomElement

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com