springboot2.1.7启动 AbstractApplicationContext refresh(一)

目录

prepareRefresh()

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()

prepareBeanFactory(beanFactory)

postProcessBeanFactory(beanFactory)


prepareRefresh()

准备此上下文以进行刷新,设置其启动日期和活动标志以及执行属性源的任何初始化。

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()

告诉子类刷新内部bean工厂。beanFactory为DefaultListableBeanFactory。

prepareBeanFactory(beanFactory)

配置工厂的标准上下文特征(设置beanFactory的各种属性)

	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()));
        //添加一个PropertyEditorRegistrar以应用于所有bean创建过程。
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		// Configure the bean factory with context callbacks.
        //处理各种Aware接口的设置
		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);

		// Register early post-processor for detecting inner beans as ApplicationListeners.
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

		// Detect a LoadTimeWeaver and prepare for weaving, if found.
		if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			// Set a temporary ClassLoader for type matching.
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}

		// Register default environment beans.
        //注册默认环境bean
		if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
			beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
		}
		if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
			beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
		}
		if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
			beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
		}
	}
void registerResolvableDependency(Class<?> dependencyType, @Nullable Object autowiredValue):用相应的自动装配值注册一个专用的依赖类型。即当通过dependencyType(class)获取bean时,会获取到autowiredValued对象或其代理对象。下方会用示例说明。

postProcessBeanFactory(beanFactory)

允许在上下文子类中对bean工厂进行后处理。目前注册了一个BeanPostProcessor(WebApplicationContextServletContextAwareProcessor),使用WebApplicationContext所使用的给定BeanFactory注册特定于Web的范围("request", "session", "globalSession", "application")。该方法也会调用registerResolvableDependency方法,以ServletRequest作为例子简单说明下。

registerResolvableDependency示例:

beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory()) :可以看出ServletRequest对应的bean实例是RequestObjectFactory。下图是@Resource引入的HttpServletRequest依赖实例。

@Resource
private HttpServletRequest httpServletRequest

 从上图可以看出,httpServletRequest是一个动态代理对象。ObjectFactoryDelegatingInvocationHandler(AutowireUtils中的静态内部类)所持有的objectFactory真实对象是RequestObjectFactory(RequestObjectFactory是WebApplicationContextUtils中的静态内部类)。

	private static class ObjectFactoryDelegatingInvocationHandler implements InvocationHandler, Serializable {

		private final ObjectFactory<?> objectFactory;

		public ObjectFactoryDelegatingInvocationHandler(ObjectFactory<?> objectFactory) {
			this.objectFactory = objectFactory;
		}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值