bean初始化完成后如何调用方法?

发布于 2024-07-26 10:08:13 字数 254 浏览 6 评论 0原文

我有一个用例,我需要在 ApplicationContext 加载时仅调用一次 bean 中的(非静态)方法。 如果我使用 MethodInvokingFactoryBean 可以吗? 或者我们有更好的解决方案?

附带说明一下,我使用 ConfigContextLoaderListener 在 Web 应用程序中加载应用程序上下文。 并且希望,如果 bean 'A' 被实例化,只需调用 methodA() 一次。

这件事怎样才能做得好呢?

I have a use case where I need to call a (non-static) method in the bean only-once at the ApplicationContext load up. Is it ok, if I use MethodInvokingFactoryBean for this? Or we have a some better solution?

As a side note, I use ConfigContextLoaderListener to load the Application Context in web application. And want, that if bean 'A' is instantiated just call methodA() once.

How can this be done nicely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

撩人痒 2024-08-02 10:08:13

为了扩展其他答案中的 @PostConstruct 建议,在我看来,这确实是最好的解决方案。

  • 它使您的代码与 Spring API 解耦(@PostConstruct 位于 javax.* 中)。
  • 它显式地将您的 init 方法注释为需要调用来初始化 bean 的
  • 方法不需要记住将 init-method 属性添加到你的 spring bean 定义中,spring 会自动调用该方法(假设你在上下文中的其他地方注册了annotation-config 选项)。

To expand on the @PostConstruct suggestion in other answers, this really is the best solution, in my opinion.

  • It keeps your code decoupled from the Spring API (@PostConstruct is in javax.*)
  • It explicitly annotates your init method as something that needs to be called to initialize the bean
  • You don't need to remember to add the init-method attribute to your spring bean definition, spring will automatically call the method (assuming you register the annotation-config option somewhere else in the context, anyway).
小姐丶请自重 2024-08-02 10:08:13

您可以使用类似的方法:

<beans>
    <bean id="myBean" class="..." init-method="init"/>
</beans>

这将在实例化 bean 时调用“init”方法。

You can use something like:

<beans>
    <bean id="myBean" class="..." init-method="init"/>
</beans>

This will call the "init" method when the bean is instantiated.

冷清清 2024-08-02 10:08:13

需要考虑三种不同的方法,如 参考

使用 init-method 属性

优点:

  • 不需要 bean 实现接口。

缺点:

  • 源代码中没有立即表明构建后需要此方法以确保 bean 正确配置。

实现InitializingBean

优点:

  • 无需指定init-method,或打开组件扫描/注释处理。
  • 适用于由库提供的 bean,我们不希望使用该库的应用程序关心 bean 生命周期。

缺点:

  • 比 init-method 方法更具侵入性。

使用 JSR-250 @PostConstruct 生命周期注释

优点:

  • 在使用组件扫描自动检测 bean 时很有用。
  • 更清楚地表明要使用特定方法进行初始化。 意图更接近代码。

缺点:

  • 初始化不再在配置中集中指定。
  • 您必须记住打开注释处理(有时可能会忘记)

There are three different approaches to consider, as described in the reference

Use init-method attribute

Pros:

  • Does not require bean to implement an interface.

Cons:

  • No immediate indication in source code that this method is required after construction to ensure the bean is correctly configured.

Implement InitializingBean

Pros:

  • No need to specify init-method, or turn on component scanning / annotation processing.
  • Appropriate for beans supplied with a library, where we don't want the application using this library to concern itself with bean lifecycle.

Cons:

  • More invasive than the init-method approach.

Use JSR-250 @PostConstruct lifecyle annotation

Pros:

  • Useful when using component scanning to autodetect beans.
  • Makes it clearer that a specific method is to be used for initialisation. Intent is closer to the code.

Cons:

  • Initialisation no longer centrally specified in configuration.
  • You must remember to turn on annotation processing (which can sometimes be forgotten)
仄言 2024-08-02 10:08:13

您是否尝试过实施 InitializingBean? 听起来正是您所追求的。

缺点是您的 bean 变得支持 Spring,但在大多数应用程序中这还不错。

Have you tried implementing InitializingBean? It sounds like exactly what you're after.

The downside is that your bean becomes Spring-aware, but in most applications that's not so bad.

零時差 2024-08-02 10:08:13

You could deploy a custom BeanPostProcessor in your application context to do it. Or if you don't mind implementing a Spring interface in your bean, you could use the InitializingBean interface or the "init-method" directive (same link).

地狱即天堂 2024-08-02 10:08:13

为了进一步消除对这两种方法的任何混淆,即使用

  1. @PostConstruct
  2. init-method="init"

从个人经验来看,我意识到使用 (1) 仅适用于servlet 容器,而 (2) 可在任何环境中工作,甚至在桌面应用程序中。 因此,如果您要在独立应用程序中使用 Spring,则必须使用 (2) 来执行“初始化后调用此方法”。

To further clear any confusion about the two approach i.e use of

  1. @PostConstruct and
  2. init-method="init"

From personal experience, I realized that using (1) only works in a servlet container, while (2) works in any environment, even in desktop applications. So, if you would be using Spring in a standalone application, you would have to use (2) to carry out that "call this method after initialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文