返回介绍

6.5 功能扩展

发布于 2025-04-22 22:09:12 字数 2857 浏览 0 评论 0 收藏

进入函数 prepareBeanFactory 前,Spring 已经完成了对配置的解析,而 ApplicationContext 在功能上的扩展也由此展开。

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {

 //设置 beanFactory 的 classLoader 为当前 context 的 classLoader

 beanFactory.setBeanClassLoader(getClassLoader());

 //设置 beanFactory 的表达式语言处理器,Spring3 增加了表达式语言的支持,

 //默认可以使用#{bean.xxx}的形式来调用相关属性值。

  beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());

 //为 beanFactory 增加了一个默认的 propertyEditor,这个主要是对 bean 的属性等设置管理的一

 个工具

  beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this,

 getEnvironment()));

 /*

  * 添加 BeanPostProcessor,

  */

  beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

 //设置了几个忽略自动装配的接口

 beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);

 beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);

 beanFactory.ignoreDependencyInterface(MessageSourceAware.class);

 beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

 beanFactory.ignoreDependencyInterface(EnvironmentAware.class);

 //设置了几个自动装配的特殊规则

  beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);

  beanFactory.registerResolvableDependency(ResourceLoader.class, this);

  beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);

  beanFactory.registerResolvableDependency(ApplicationContext.class, this);

 //增加对 AspectJ 的支持

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

 }

 //添加默认的系统环境 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());

 }

}

上面函数中主要进行了几个方面的扩展。

增加对 SPEL 语言的支持。

增加对属性编辑器的支持。

增加对一些内置类,比如 EnvironmentAware、MessageSourceAware 的信息注入。

设置了依赖功能可忽略的接口。

注册一些固定依赖的属性。

增加 AspectJ 的支持(会在第 7 章中进行详细的讲解)。

将相关环境变量及属性注册以单例模式注册。

可能读者不是很理解每个步骤的具体含义,接下来我们会对各个步骤进行详细地分析。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。