初期化がいつ終了したかを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 thatpostProcessAfterInitializationis 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
InitializingBeaninterface. It defines a single methodafterPropertiesSetwhich 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
@PostContstuctannotation on a method. This annotation tells theApplicationContextthat 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 に答える