1.6.1.3. Default Initialization and Destroy Methods(默认初始化和销毁方法)

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

1.5. Bean Scopes(Bean作用域)

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-methoddestroy-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和代理是单独定义的,那么代码甚至可以绕过代理与原始目标beanraw target bean)交互。因此,将拦截器应用于init方法是不一致的,因为这样做会将目标bean的生命周期与其代理或拦截器(proxy or interceptor)耦合起来,并在代码直接与原始目标bean交互时留下奇怪的语义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值