找回密码
 立即注册
首页 业界区 业界 Spring Aware 接口

Spring Aware 接口

倘伟 2025-11-23 21:30:01
Spring Aware 接口

Spring框架中的Aware接口是一个特殊的标记接口,用表示该 Bean 可以接受 Spring容器特定的对象。
in short 实现这些内置子接口的Bean, Spring 在实例化时过程中会调用其方法, 将Spring框架特定的对象注入
org.springframework.beans.factory.Aware
  1. /**
  2. * A marker superinterface indicating that a bean is eligible to be notified by the
  3. * Spring container of a particular framework object through a callback-style method.
  4. * The actual method signature is determined by individual subinterfaces but should
  5. * typically consist of just one void-returning method that accepts a single argument.
  6. *
  7. * Spring框架中的`Aware`接口是一个特殊的标记接口,用表示该 Bean 可以接受 Spring容器特定的对象。
  8. * 注意 `Aware` 接口仅作为一个标记, 实际的注入对象, 由各个子接口方法的签名确定
  9. *
  10. * <p>Note that merely implementing {@link Aware} provides no default functionality.
  11. * Rather, processing must be done explicitly, for example in a
  12. * {@link org.springframework.beans.factory.config.BeanPostProcessor}.
  13. * Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
  14. * for an example of processing specific {@code Aware} interface callbacks.
  15. *
  16. * @author Chris Beams
  17. * @author Juergen Hoeller
  18. * @since 3.1
  19. */
  20. public interface Aware {
  21. }
复制代码
Aware 的子接口

1.png

相关注入的源码

ApplicationContextAwareProcessor 是一个 BPP 它本身会在容器启动准备BeanFactory 的过程中org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory 添加到 beanFactory 中
  1. /**
  2. * Configure the factory's standard context characteristics,
  3. * such as the context's ClassLoader and post-processors.
  4. * @param beanFactory the BeanFactory to configure
  5. */
  6. protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  7.         // Tell the internal bean factory to use the context's class loader etc.
  8.         /**
  9.          * 为 beanFactory 设置 bean 的类加载器
  10.          */
  11.         beanFactory.setBeanClassLoader(getClassLoader());
  12.         /**
  13.          * 为 beanFactory 设置 bean 的表达式(Spring EL)解析器
  14.          */
  15.         beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
  16.         /**
  17.          *  为 beanFactory 设置 PropertyEditorRegistrar
  18.          */
  19.         beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
  20.         /**
  21.          * <!>添加一个 BeanPostProcessor 回调, 作用是如果Bean实现这些接口:
  22.          * (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
  23.          *                                 bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
  24.          *                                 bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
  25.          *                                 bean instanceof ApplicationStartupAware)
  26.          *  它负责调用其对应 Aware 的方法, 给Bean 注入对象
  27.          *
  28.          * in short 就是给 bean 设置 上下文, 内置的对象; 注意是 (postProcessBeforeInitialization)
  29.          */
  30.         // Configure the bean factory with context callbacks.
  31.         beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
  32. ......
复制代码
org.springframework.context.support.ApplicationContextAwareProcessor 的源码
  1. package org.springframework.context.support;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.Aware;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5. import org.springframework.beans.factory.config.EmbeddedValueResolver;
  6. import org.springframework.context.ApplicationContextAware;
  7. import org.springframework.context.ApplicationEventPublisherAware;
  8. import org.springframework.context.ApplicationStartupAware;
  9. import org.springframework.context.ConfigurableApplicationContext;
  10. import org.springframework.context.EmbeddedValueResolverAware;
  11. import org.springframework.context.EnvironmentAware;
  12. import org.springframework.context.MessageSourceAware;
  13. import org.springframework.context.ResourceLoaderAware;
  14. import org.springframework.lang.Nullable;
  15. import org.springframework.util.StringValueResolver;
  16. /**
  17. * {@link BeanPostProcessor} implementation that supplies the {@code ApplicationContext},
  18. * {@link org.springframework.core.env.Environment Environment}, or
  19. * {@link StringValueResolver} for the {@code ApplicationContext} to beans that
  20. * implement the {@link EnvironmentAware}, {@link EmbeddedValueResolverAware},
  21. * {@link ResourceLoaderAware}, {@link ApplicationEventPublisherAware},
  22. * {@link MessageSourceAware}, and/or {@link ApplicationContextAware} interfaces.
  23. *
  24. * <p>Implemented interfaces are satisfied in the order in which they are
  25. * mentioned above.
  26. *
  27. * <p>Application contexts will automatically register this with their
  28. * underlying bean factory. Applications do not use this directly.
  29. *
  30. * @author Juergen Hoeller
  31. * @author Costin Leau
  32. * @author Chris Beams
  33. * @author Sam Brannen
  34. * @since 10.10.2003
  35. * @see org.springframework.context.EnvironmentAware
  36. * @see org.springframework.context.EmbeddedValueResolverAware
  37. * @see org.springframework.context.ResourceLoaderAware
  38. * @see org.springframework.context.ApplicationEventPublisherAware
  39. * @see org.springframework.context.MessageSourceAware
  40. * @see org.springframework.context.ApplicationContextAware
  41. * @see org.springframework.context.support.AbstractApplicationContext#refresh()
  42. */
  43. class ApplicationContextAwareProcessor implements BeanPostProcessor {
  44.         private final ConfigurableApplicationContext applicationContext;
  45.         private final StringValueResolver embeddedValueResolver;
  46.         /**
  47.          * Create a new ApplicationContextAwareProcessor for the given context.
  48.          */
  49.         public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
  50.                 this.applicationContext = applicationContext;
  51.                 this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
  52.         }
  53.         @Override
  54.         @Nullable
  55.         public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  56.                 /**
  57.                  * 这里只处理以下的7种对象的 Aware注入, 若不是则返回不处理
  58.                  */
  59.                 if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
  60.                                 bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
  61.                                 bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
  62.                                 bean instanceof ApplicationStartupAware)) {
  63.                         return bean;
  64.                 }
  65.                 //
  66.                 invokeAwareInterfaces(bean);
  67.                 return bean;
  68.         }
  69.         private void invokeAwareInterfaces(Object bean) {
  70.                 if (bean instanceof Aware) {
  71.                         if (bean instanceof EnvironmentAware environmentAware) {
  72.                                 environmentAware.setEnvironment(this.applicationContext.getEnvironment());
  73.                         }
  74.                         if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
  75.                                 embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
  76.                         }
  77.                         if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
  78.                                 resourceLoaderAware.setResourceLoader(this.applicationContext);
  79.                         }
  80.                         if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
  81.                                 applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);
  82.                         }
  83.                         if (bean instanceof MessageSourceAware messageSourceAware) {
  84.                                 messageSourceAware.setMessageSource(this.applicationContext);
  85.                         }
  86.                         if (bean instanceof ApplicationStartupAware applicationStartupAware) {
  87.                                 applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());
  88.                         }
  89.                         if (bean instanceof ApplicationContextAware applicationContextAware) {
  90.                                 applicationContextAware.setApplicationContext(this.applicationContext);
  91.                         }
  92.                 }
  93.         }
  94. }
复制代码

  • Environment 对象。Environment对象代表了当前应用运行的环境,它可以用来获取配置属性(如系统属性、环境变量、配置文件中的属性等)。通过Environment,可以方便地访问各种配置信息,并且支持 profiles 和 properties 的解析。
  • EmbeddedValueResolver对象。这个对象用于解析字符串中的占位符(如${...})和SpEL表达式(如#{...})。它通常用于注解驱动或XML配置中,用于解析嵌入在字符串中的值。
  • ResourceLoader 是用于加载资源(如类路径下的文件、文件系统中的文件、URL资源等)的策略接口。ApplicationContext 本身就是一个ResourceLoader,所以这里将applicationContext作为ResourceLoader注入。
  • ApplicationEventPublisher 对象。这个对象用于发布应用事件。Spring的事件机制是基于观察者模式的,通过ApplicationEventPublisher发布事件,然后由相应的ApplicationListener监听器处理。这里同样使用 applicationContext 作为事件发布器,因为ApplicationContext实现了ApplicationEventPublisher接口。
  • MessageSource 是用于国际化(i18n)的消息接口,它可以解析消息代码并返回对应的消息字符串。通过MessageSource,可以实现多语言支持。
  • ApplicationStartup 是Spring 5.3引入的,用于跟踪应用启动过程中的各个步骤,从而可以收集启动性能指标。它允许开发者监控应用启动的各个阶段。
  • ApplicationContext 是Spring容器的核心接口,它提供了配置应用上下文的功能,包括获取bean、发布事件、访问环境、国际化等功能。通过实现这个接口,bean可以直接访问容器,但通常不推荐这样做,因为这样会导致代码与Spring框架耦合。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册