5

初期化がいつ終了したかをSpringアプリで知る方法はありますか? アプリがデプロイされたら、いくつかのコードを実行する必要があり、ServletContextListener やSpring built-in events などを検索しています。

4

2 に答える 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.

  1. You can utilize a BeanPostProcessor. It has two methods that are treated as callbacks, and I believe that postProcessAfterInitialization is the one that you would be interested in. The thing with BeanPostProcessor's is that they are run for each bean in the ApplicationContext, 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 a BeanPostProcessor, you simply define it as a part of your ApplicationContext.
  2. Implement the InitializingBean interface. It defines a single method afterPropertiesSet which is invoked by the ApplicationContext. This has an advantage over number 1, as it can be applied on a bean by bean basis (doesn't apply to all beans in ApplicationContext).
  3. Utilize the @PostContstuct annotation on a method. This annotation tells the ApplicationContext 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 に答える