bean初始化完成后如何调用方法? [英] How to call a method after bean initialization is complete?

查看:268
本文介绍了bean初始化完成后如何调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

这怎么做得好?

推荐答案

您可以使用类似以下内容的

You can use something like:

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

实例化bean时,它将调用"init"方法.

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

这篇关于bean初始化完成后如何调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆