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.1.1. Initialization Callbacks 初始化回调
The org.springframework.beans.factory.InitializingBean
interface lets a bean perform initialization work after the container has set all necessary properties on the bean. The InitializingBean
interface specifies a single method:
org.springframework.beans.factory.InitializingBean
接口允许bean在容器对bean设置了所有必要的属性之后执行初始化工作。InitializingBean
接口指定了一个方法:
void afterPropertiesSet() throws Exception;
We recommend that you do not use the InitializingBean
interface, because it unnecessarily couples the code to Spring. Alternatively, we suggest using the @PostConstruct annotation or specifying a POJO initialization method. In the case of XML-based configuration metadata, you can use the init-method
attribute to specify the name of the method that has a void no-argument signature. With Java configuration, you can use the initMethod
attribute of @Bean
. See Receiving Lifecycle Callbacks. Consider the following example:
我们建议您不要使用InitializingBean
接口,因为它不必要地将代码耦合到了Spring。而且,我们建议使用 @PostConstruct注解或指定POJO初始化方法。对于基于XML的配置元数据,可以使用 init-method
属性指定具有void no-argument(无参数)签名的方法的名称。通过Java配置,您可以使用@Bean的initMethod属性。请参阅接收生命周期回调(Receiving Lifecycle Callbacks)。考虑下面的例子:
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
Java
public class ExampleBean {
public void init() {
// do some initialization work
}
}
Kotlin
class ExampleBean {
fun init() {
// do some initialization work
}
}
The preceding example has almost exactly the same effect as the following example (which consists of two listings):
前述示例与下面的示例(由两个列表组成)具有几乎完全相同的效果:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
Java
public class AnotherExampleBean implements InitializingBean {
@Override
public void afterPropertiesSet() {
// do some initialization work
}
}
Kotlin
class AnotherExampleBean : InitializingBean {
override fun afterPropertiesSet() {
// do some initialization work
}
}
However, the first of the two preceding examples does not couple the code to Spring.
然而,前面两个示例中的第一个并没有将代码耦合到Spring。