ApplicationContextAware-api

来源:互联网 发布:3306端口被占用 编辑:程序博客网 时间:2024/05/20 20:44
  

/**
 * Interface to be implemented by any object that wishes to be notified
 * of the {@link ApplicationContext} that it runs in.
 *翻译:

 如果一个对象希望可以取得这个对象运行所在的容器信息(ApplicationContext)的话,这个对象

不妨实现这个接口(ApplicationContextAware)。
 * <p>Implementing this interface makes sense for example when an object
 * requires access to a set of collaborating beans. Note that configuration
 * via bean references is preferable to implementing this interface just
 * for bean lookup purposes.

翻译:

如果一个对象要求取得一组相关联的beans的话,就不妨实现这个接口(ApplicationContextAware)。需要注意的是,

引用bean的配置方式最好是通过实现这个接口,达到寻找bean的目的。

 *
 * <p>This interface can also be implemented if an object needs access to file
 * resources, i.e. wants to call <code>getResource</code>, wants to publish
 * an application event, or requires access to the MessageSource. However,
 * it is preferable to implement the more specific {@link ResourceLoaderAware},
 * {@link ApplicationEventPublisherAware} or {@link MessageSourceAware} interface
 * in such a specific scenario.

翻译:

另外,以下提到的一些目的都可以通过实现这个接口的方式来实现。

例如,一个对象需要得到一个文件资源,就可以通过调用getResource的方法实现;一个对象

需要发布一个application event;一个对象需要得到一个MessageSource。但是,在特定的情况下,我们

实现更加专门的接口,例如: ResourceLoaderAware,ApplicationEventPublisherAware,MessageSourceAware。
 *
 * <p>Note that file resource dependencies can also be exposed as bean properties
 * of type {@link org.springframework.core.io.Resource}, populated via Strings
 * with automatic type conversion by the bean factory. This removes the need
 * for implementing any callback interface just for the purpose of accessing
 * a specific file resource.

翻译:

注意。文件资源的依赖(就是说,可以调用什么类型的文件)通过作为org.springframework.core.io.Resource类型的

bean属性的方式进行设置的,其中Strings类型是默认支持的。这样子,就避免单纯为了支持一种特殊的文件类型,就另外

实现一个能够callback的接口。
 *
 * <p>{@link org.springframework.context.support.ApplicationObjectSupport} is a
 * convenience base class for application objects, implementing this interface.
 *

翻译:

org.springframework.context.support.ApplicationObjectSupport是实现了这个接口(ApplicationContextAware )而且

针对application objects通常使用到的一个类。
 * <p>For a list of all bean lifecycle methods, see the
 * {@link org.springframework.beans.factory.BeanFactory BeanFactory javadocs}.

翻译:

如果想了解bean的所有生命周期的函数,可以参考org.springframework.beans.factory.BeanFactory 的介绍。
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see ResourceLoaderAware
 * @see ApplicationEventPublisherAware
 * @see MessageSourceAware
 * @see org.springframework.context.support.ApplicationObjectSupport
 * @see org.springframework.beans.factory.BeanFactoryAware
 */
public interface ApplicationContextAware {
 
 /**
  * Set the ApplicationContext that this object runs in.
  * Normally this call will be used to initialize the object.
  * <p>Invoked after population of normal bean properties but before an init callback such
  * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
  * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
  * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
  * {@link MessageSourceAware}, if applicable.
  * @param applicationContext the ApplicationContext object to be used by this object
  * @throws ApplicationContextException in case of context initialization errors
  * @throws BeansException if thrown by application context methods
  * @see org.springframework.beans.factory.BeanInitializationException
  */

翻译:

这个方法是用来设置这个对象运行所在的ApplicationContext。通常,调用这个方法来初始化对象的。

这个方法会在普通bean的属性注入之后,在初始化返回回调信息(回调信息:A调用函数B,B会返回一个信息给A,告知

A函数执行得怎么样),例如org.springframework.beans.factory.InitializingBean#afterPropertiesSet()方法,或者自定初始化方法之前

得到调用。如果需要的话,可以在{@link ResourceLoaderAware#setResourceLoader}, {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
 {@link MessageSourceAware}之后得到调用。
 void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

原创粉丝点击