初期化がいつ終了したかをSpringアプリで知る方法はありますか? アプリがデプロイされたら、いくつかのコードを実行する必要があり、ServletContextListener やSpring built-in events などを検索しています。
質問する
5348 次
2 に答える
7
Based on your response to my comment I will respond with the multiple things you can do to process an initialized Spring bean.
- You can utilize a
BeanPostProcessor
. It has two methods that are treated as callbacks, and I believe thatpostProcessAfterInitialization
is the one that you would be interested in. The thing withBeanPostProcessor
's is that they are run for each bean in theApplicationContext
, so you will want to be sure to look for only the bean(s) that you are interested in applying this processing to. To use aBeanPostProcessor
, you simply define it as a part of yourApplicationContext
. - Implement the
InitializingBean
interface. It defines a single methodafterPropertiesSet
which is invoked by theApplicationContext
. This has an advantage over number 1, as it can be applied on a bean by bean basis (doesn't apply to all beans inApplicationContext
). - Utilize the
@PostContstuct
annotation on a method. This annotation tells theApplicationContext
that this method should be run after the bean has been initialized. This acts similarly to number 2, in that it is performed on a bean by bean basis.
Further information on the callback lifecycle of the ApplicationContext
can be read about at this location.
于 2013-07-09T21:16:24.010 に答える
2
使用できます
@PostConstruct
注釈- または
ApplicationListener
によってトリガーされるContextStartedEvent
です (ただし、一般的な Web アプリケーションの場合は、2 つのコンテキストと 2 つContextStartedEvent
の があることに注意してください。
于 2013-07-09T21:11:29.423 に答える