1.6.1.1. Initialization Callbacks 初始化回调

 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.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配置,您可以使用@BeaninitMethod属性。请参阅接收生命周期回调(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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

月满闲庭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值