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.3. Default Initialization and Destroy Methods默认初始化和销毁方法
When you write initialization and destroy method callbacks that do not use the Spring-specific InitializingBean
and DisposableBean
callback interfaces, you typically write methods with names such as init()
, initialize()
, dispose()
, and so on. Ideally, the names of such lifecycle callback methods are standardized across a project so that all developers use the same method names and ensure consistency.
当您编写不使用与Spring相关的InitializingBean
和DisposableBean
回调接口的初始化和销毁方法回调时,您通常使用 init()
, initialize()
, dispose()
等名称编写方法。理想情况下,这样的生命周期回调方法(lifecycle callback method)的名称在整个项目中是标准化的,以便所有开发人员使用相同的方法名称并确保一致性。
You can configure the Spring container to “look” for named initialization and destroy callback method names on every bean. This means that you, as an application developer, can write your application classes and use an initialization callback called init()
, without having to configure an init-method="init"
attribute with each bean definition. The Spring IoC container calls that method when the bean is created (and in accordance with the standard lifecycle callback contract described previously). This feature also enforces a consistent naming convention for initialization and destroy method callbacks.
您可以将Spring容器配置在每个bean上“查找”指定名称的初始化和销毁回调方法名。这意味着,作为应用程序开发人员,您可以编写应用程序类并使用名为init() 的初始化回调,而不必为每个bean定义配置init-method="init"
属性。Spring IoC容器在创建bean时调用该方法(并根据前面描述的标准生命周期回调约定(standard lifecycle callback contract))。此功能还强制执行初始化和销毁方法回调(initialization and destroy method callback)的一致命名约定。
Suppose that your initialization callback methods are named init()
and your destroy callback methods are named destroy()
. Your class then resembles the class in the following example:
假设初始化回调方法名为init() ,销毁回调方法名为destroy() 。然后,您的类与以下示例中的类相似:
Java
public class DefaultBlogService implements BlogService {
private BlogDao blogDao;
public void setBlogDao(BlogDao blogDao) {
this.blogDao = blogDao;
}
// this is (unsurprisingly) the initialization callback method
public void init() {
if (this.blogDao == null) {
throw new IllegalStateException("The [blogDao] property must be set.");
}
}
}
Kotlin
class DefaultBlogService : BlogService {
private var blogDao: BlogDao? = null
// this is (unsurprisingly) the initialization callback method
fun init() {
if (blogDao == null) {
throw IllegalStateException("The [blogDao] property must be set.")
}
}
}
You could then use that class in a bean resembling the following:
然后,您可以在类似以下内容的bean中使用该类:
<beans default-init-method="init">
<bean id="blogService" class="com.something.DefaultBlogService">
<property name="blogDao" ref="blogDao" />
</bean>
</beans>
The presence of the default-init-method
attribute on the top-level <beans/>
element attribute causes the Spring IoC container to recognize a method called init
on the bean class as the initialization method callback. When a bean is created and assembled, if the bean class has such a method, it is invoked at the appropriate time.
You can configure destroy method callbacks similarly (in XML, that is) by using the default-destroy-method
attribute on the top-level <beans/>
element.
顶级(top-level )<beans/>元素属性上的 default-init-method
属性会导致Spring IoC容器将bean类上名为init的方法识别为初始化方法回调。当创建和组装bean时,如果bean类有这样一个方法,则会在适当的时间调用它。
通过在顶级<beans/>元素上使用default-destroy-method
属性,可以类似地配置destroy方法回调(即XML)。
Where existing bean classes already have callback methods that are named at variance with the convention, you can override the default by specifying (in XML, that is) the method name by using the init-method
and destroy-method
attributes of the <bean/>
itself.
如果现有bean类已经有了名称与约定不同的回调方法,那么您可以通过使用<bean/>的 init-method
和
destroy-method
属性
指定(即XML)方法名来覆盖默认值。
The Spring container guarantees that a configured initialization callback is called immediately after a bean is supplied with all dependencies. Thus, the initialization callback is called on the raw bean reference, which means that AOP interceptors and so forth are not yet applied to the bean. A target bean is fully created first and then an AOP proxy (for example) with its interceptor chain is applied. If the target bean and the proxy are defined separately, your code can even interact with the raw target bean, bypassing the proxy. Hence, it would be inconsistent to apply the interceptors to the init
method, because doing so would couple the lifecycle of the target bean to its proxy or interceptors and leave strange semantics when your code interacts directly with the raw target bean.
Spring容器保证在为bean提供所有依赖项后立即调用配置的初始化回调。因此,对原始bean引用(raw bean reference)调用初始化回调(initialization callback),这意味着AOP拦截器(AOP interceptor)等尚未应用于bean。首先,完全创建一个目标bean,然后应用一个带拦截器链的AOP代理(AOP proxy)(例如)。如果目标bean和代理是单独定义的,那么代码甚至可以绕过代理与原始目标bean(raw target bean)交互。因此,将拦截器应用于init方法是不一致的,因为这样做会将目标bean的生命周期与其代理或拦截器(proxy or interceptor)耦合起来,并在代码直接与原始目标bean交互时留下奇怪的语义。