spring#aware接口

spring容器中的bean通常而言是不会意识到spring容器的存在,但是有时候我们提供的bean渴望用到spring容器提供的一些功能。

这样我们的bean需要实现Aware系列的接口,就可以获得spring提供的一些功能。

package org.springframework.beans.factory;

/**
 * A marker superinterface indicating that a bean is eligible to be notified by the
 * Spring container of a particular framework object through a callback-style method.
 * The actual method signature is determined by individual subinterfaces but should
 * typically consist of just one void-returning method that accepts a single argument.
 *
 * <p>Note that merely implementing {@link Aware} provides no default functionality.
 * Rather, processing must be done explicitly, for example in a
 * {@link org.springframework.beans.factory.config.BeanPostProcessor}.
 * Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
 * for an example of processing specific {@code *Aware} interface callbacks.
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.1
 */
public interface Aware {
  // Aware是一个具有标识作用的超级接口,实现该接口的bean是具有被spring 容器通知的能力的,而被通知的方式就是通过回调。
// 白话:spring会在特定的时候调用一下实现特定接口的特定方法。如果我们的bean实现了某个接口,并覆写某个方法,这个方法就会在特定的时候被调用。 }

常见aware接口列举:

  • BeanNameAware 获得到容器中Bean的名称
  • BeanFactoryAware获得当前bean Factory
  • ApplicationContextAware获得当前的application context
  • ApplicationEventPublisherAware 应用事件发布器
  • ResourceLoaderAware 获取资源加载器,可以获得外部资源文件

猜你喜欢

转载自www.cnblogs.com/luohaonan/p/11840061.html