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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1088550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:54:07  来源:igfitidea点击:

How to call a method after bean initialization is complete?

springinitializationstartupapplicationcontext

提问by peakit

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?

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

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.

作为旁注,我使用 ConfigContextLoaderListener 在 Web 应用程序中加载应用程序上下文。并且想要,如果 bean 'A' 被实例化,只需调用 methodA() 一次。

How can this be done nicely?

这怎么能做得好?

采纳答案by Mercer Traieste

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.

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

回答by skaffman

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

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

  • 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).
  • 它使您的代码与 Spring API 分离(@PostConstruct 在 javax.* 中)
  • 它将您的 init 方法显式注释为需要调用以初始化 bean 的方法
  • 您不需要记住将 init-method 属性添加到您的 spring bean 定义中,spring 将自动调用该方法(假设您在上下文中的其他位置注册 annotation-config 选项,无论如何)。

回答by toolkit

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

参考资料中所述,需要考虑三种不同的方法

Use init-method attribute

使用 init-method 属性

Pros:

优点:

  • Does not require bean to implement an interface.
  • 不需要 bean 来实现接口。

Cons:

缺点:

  • No immediate indication this method is required after construction to ensure the bean is correctly configured.
  • 构建后不需要立即指示此方法以确保正确配置 bean。


Implement InitializingBean

实现 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.
  • 无需指定init-method,也无需开启组件扫描/注解处理。
  • 适用于随库提供的 bean,我们不希望使用此库的应用程序关注 bean 生命周期。

Cons:

缺点:

  • More invasive than the init-method approach.
  • 比 init-method 方法更具侵入性。


Use JSR-250 @PostConstructlifecyle annotation

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

Pros:

优点:

  • Useful when using component scanning to autodetect beans.
  • Makes it clear that a specific method is to be used for initialisation. Intent is closer to the code.
  • 在使用组件扫描自动检测 bean 时很有用。
  • 明确表示要使用特定的方法进行初始化。意图更接近代码。

Cons:

缺点:

  • Initialisation no longer centrally specified in configuration.
  • You must remember to turn on annotation processing (which can sometimes be forgotten)
  • 初始化不再在配置中集中指定。
  • 一定要记得开启注解处理(有时候会忘记)

回答by Jon Skeet

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

你试过实施InitializingBean吗?这听起来正是你所追求的。

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

缺点是您的 bean 变得可感知 Spring,但在大多数应用程序中并没有那么糟糕。

回答by Rob H

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

您可以在应用程序上下文中部署自定义BeanPostProcessor来执行此操作。或者,如果您不介意在 bean 中实现 Spring 接口,则可以使用InitializingBean接口或“init-method”指令(相同链接)。

回答by Ayorinde

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

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

  1. @PostConstructand
  2. init-method="init"
  1. @PostConstruct
  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.

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