一、ApplicationContextAware接口
1 2 3 4 5 6 7 8 9 |
|
二、ServletContextAware 接口
1 2 3 4 5 6 7 8 |
|
三、InitializingBean 接口
1 2 3 4 5 |
|
四、ApplicationListener<ApplicationEvent> 接口
1 2 3 4 5 6 7 8 |
|
示例程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。