Spring Framework Documentation (5.3.10)
Core | IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP. |
1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)
1.2. Container Overview (容器概览)
1.6. Customizing the Nature of a Bean (自定义bean的性质)
1.6.1. Lifecycle Callbacks 生命周期回调
1.6.1.1. Initialization Callbacks 初始化回调
1.6.1.2. Destruction Callbacks 销毁回调
1.6.1.3. Default Initialization and Destroy Methods默认初始化和销毁方法
1.6.1.4. Combining Lifecycle Mechanisms (合并生命周期机制)
1.6.1.5. Startup and Shutdown Callbacks 启动和停止回调
1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序优雅地关闭Spr
1.6.2. ApplicationContextAware and BeanNameAware
1.6.3. Other Aware Interfaces 其它Aware接口
1.7. Bean Definition Inheritance(Bean定义继承)
1.8. Container Extension Points (容器扩展点)
下载此文档精编完整版
No. | 内容 | 下载地址 | 文档内容目录 |
1 | 中英双语精编版 第一部分 | PDF下载 | 内容目录 |
2 | 中英双语精编版 第二部分 | PDF下载 | 内容目录 |
3 | 中文精编版 第一部分 | PDF下载 | 内容目录 |
4 | 中文精编版 第二部分 | PDF下载 | 内容目录 |
更多章节内容,请点击查看: Core Technologies
1.6.2. ApplicationContextAware
and BeanNameAware
When an ApplicationContext creates an object instance that implements the org.springframework.context.ApplicationContextAware interface, the instance is provided with a reference to that ApplicationContext. The following listing shows the definition of the ApplicationContextAware interface:
当ApplicationContext 创建一个实现org.springframework.context.ApplicationContextAware接口的对象实例时,会向该实例提供对该ApplicationContext的引用。下表展示了ApplicationContextAware 接口的定义:
public interface ApplicationContextAware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
Thus, beans can programmatically manipulate the ApplicationContext that created them, through the ApplicationContext interface or by casting the reference to a known subclass of this interface (such as ConfigurableApplicationContext, which exposes additional functionality). One use would be the programmatic retrieval of other beans. Sometimes this capability is useful. However, in general, you should avoid it, because it couples the code to Spring and does not follow the Inversion of Control style, where collaborators are provided to beans as properties. Other methods of the ApplicationContext provide access to file resources, publishing application events, and accessing a MessageSource. These additional features are described in Additional Capabilities of the ApplicationContext.
因此,bean可以通过ApplicationContext 接口或通过强制引用该接口的已知子类(如ConfigurableApplicationContext,它公开了其他功能),以编程方式操纵创建bean的ApplicationContext 。一个用途是对其他bean进行编程检索。有时这种能力是有用的。但是,一般来说,您应该避免使用它,因为它将代码耦合到Spring,并且不遵循控制反转样式(Inversion of Control style),即协作者(collaborator)作为属性提供给bean。ApplicationContext的其他方法提供对文件资源的访问、发布应用程序事件和访问MessageSource。ApplicationContext的附加功能(Additional Capabilities of the ApplicationContext)中描述了这些附加功能。
Autowiring is another alternative to obtain a reference to the ApplicationContext. The traditional constructor and byType autowiring modes (as described in Autowiring Collaborators) can provide a dependency of type ApplicationContext for a constructor argument or a setter method parameter, respectively. For more flexibility, including the ability to autowire fields and multiple parameter methods, use the annotation-based autowiring features. If you do, the ApplicationContext is autowired into a field, constructor argument, or method parameter that expects the ApplicationContext type if the field, constructor, or method in question carries the @Autowired annotation. For more information, see Using @Autowired.
自动装配是获取对ApplicationContext的引用的另一种替代方法。传统的构造函数(traditional constructor)和byType自动装配模式(autowiring mode)(如Autowiring Collaborators中所述)可以分别为构造函数参数或setter方法参数提供类型ApplicationContext的依赖关系。要获得更大的灵活性,包括自动装配字段和多参数方法的能力,请使用基于注解的自动装配功能(annotation-based autowiring feature)。如果这样做,如果所讨论的字段、构造函数或方法带有 @Autowired注解,则ApplicationContext将被自动装配到字段、构造函数参数或方法参数中,且字段、构造函数参数或方法参数需要ApplicationContext类型。有关详细信息,请参阅“使用@Autowired”(Using @Autowired)。
When an ApplicationContext creates a class that implements the org.springframework.beans.factory.BeanNameAware interface, the class is provided with a reference to the name defined in its associated object definition. The following listing shows the definition of the BeanNameAware interface:
当ApplicationContext 创建一个实现org.springframework.beans.factory.BeanNameAware接口的类时,会向该类提供对其关联对象定义中定义的名称的引用。下面的列表展示了BeanNameWare接口的定义:
public interface BeanNameAware {
void setBeanName(String name) throws BeansException;
}
The callback is invoked after population of normal bean properties but before an initialization callback such as InitializingBean, afterPropertiesSet, or a custom init-method.
回调在填充普通bean属性之后,但在初始化回调(如InitializingBean、afterPropertiesSet或自定义init-method)之前被调用。