简体   繁体   English

bean 生命周期中的回调

[英]Callbacks in bean lifecycle

I was just going through spring documentation for Lifecycle callbacks for a bean.我刚刚浏览了 spring 文档,了解 bean 的生命周期回调。 For initialization callback the documentation recommends using @PostConstruct annotation instead of using the InitializingBean interface implementation as it couples code with Spring. Could someone please explain what they mean when they say coupling code with spring. I will share the link to the section i was going through here .对于初始化回调,文档建议使用 @PostConstruct 注释而不是使用 InitializingBean 接口实现,因为它将代码与 Spring 耦合。当他们说耦合代码与 spring 时,有人能解释一下他们的意思吗?我将分享我要去的部分的链接通过这里

The @PostContruct annotation is not Spring-specific, but from Jakarta Annotations . @PostContruct注释不是 Spring 特定的,而是来自Jakarta Annotations This way, the core part of your code is not tied to Spring specifically, but could also work in a non-Spring environment, including any Jakarta EE implementation (see "Certified referencing runtimes" in the article), for example, GlassFish or WildFly.这样,您的代码的核心部分就不会具体绑定到 Spring,但也可以在非 Spring 环境中工作,包括任何Jakarta EE实现(请参阅文章中的“认证引用运行时”),例如 GlassFish 或 WildFly .

Here is the relevant Javadoc from the Jakarta documentation: PostConstruct .这是 Jakarta 文档中的相关 Javadoc: PostConstruct

InitializingBean is pure Spring and will only work in Spring. @PostConstruct is ordinary Java 1.8. InitializingBean是纯Spring,只会在@PostConstruct起作用。@PostConstruct是普通的Java 1.8。 Sadly, from 9 it's dependent on javax.annotation:javax.annotation-api:1.3.2可悲的是,从 9 开始它依赖于javax.annotation:javax.annotation-api:1.3.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM