spring回调机制-aware接口

本文解析了Spring框架中的回调机制,特别是通过Aware接口实现的回调过程。文章深入探讨了AbstractApplicationContext类中的prepareBeanFactory方法如何配置Bean工厂,并通过BeanPostProcessor接口实现Aware接口的回调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring回调机制-aware接口

定义

由启动容器类进行分析,AbstractApplicationContext中的prepareBeanFactory方法

    /**
	 * Configure the factory's standard context characteristics,
	 * such as the context's ClassLoader and post-processors.
	 * @param beanFactory the BeanFactory to configure
	 */
	protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		// Tell the internal bean factory to use the context's class loader etc.
		beanFactory.setBeanClassLoader(getClassLoader());
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		// Configure the bean factory with context callbacks.
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

		// BeanFactory interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
		beanFactory.registerResolvableDependency(ApplicationContext.class, this);
        }

其中

		// Configure the bean factory with context callbacks.
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

标注了回调方式为ApplicationContextAwareProcessor接口,属于BeanPostProcessor的一种,
初始化调用栈如下

************************************************************
java.lang.Thread.getStackTrace() 1,559 <- 
org.github.hoorf.spring.aware.SpringContextUtil.printTrack() 22 <- 
org.github.hoorf.spring.aware.SpringContextUtil.setApplicationContext() 15 <- 
org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces() 123 <- 
org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization() 100 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization() 416 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean() 1,788 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean() 595 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean() 517 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0() 323 <- 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton() 222 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean() 321 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean() 202 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons() 882 <- 
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization() 878 <- 
org.springframework.context.support.AbstractApplicationContext.refresh() 550 <- 
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh() 141 <- 
org.springframework.boot.SpringApplication.refresh() 747 <- 
org.springframework.boot.SpringApplication.refreshContext() 397 <- 
org.springframework.boot.SpringApplication.run() 315 <- 
org.springframework.boot.SpringApplication.run() 1,226 <- 
org.springframework.boot.SpringApplication.run() 1,215 <- 
org.github.hoorf.spring.BootApplication.main() 9
************************************************************

可以看到最终的调用链上的方法applyBeanPostProcessorsBeforeInstantiationapplyBeanPostProcessorsAfterInitialization

	protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
		Object bean = null;
		if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
			// Make sure bean class is actually resolved at this point.
			if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
				Class<?> targetType = determineTargetType(beanName, mbd);
				if (targetType != null) {
					bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
					if (bean != null) {
						bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
					}
				}
			}
			mbd.beforeInstantiationResolved = (bean != null);
		}
		return bean;
	}

最终可以看到其实现机制是实现了BeanPostProcessor,进行调用,再看下上文提到aware所实现ApplicationContextAwareProcessor

    @Override
	@Nullable
	public 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;
	}

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

可以看到回调的aware种类

  • EnvironmentAware
  • EmbeddedValueResolverAware
  • ResourceLoaderAware
  • ApplicationEventPublisherAware
  • MessageSourceAware
  • ApplicationContextAware

总结

aware实现的机制也是BeanPostProcessor

public interface BeanPostProcessor {


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

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

}

主要实现方法postProcessBeforeInitialization,在bean初始化之前执行,然后对相关实现接口进行回调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值