如何让spring mvc web应用启动时就执行特定处理

Spring-MVC的应用中,要实现应用启动时就执行特定处理的功能,主要是通过实现下面这些接口(任选一,至少一个即可)

 

一、ApplicationContextAware接口

1 package org.springframework.context;
2  
3 import org.springframework.beans.BeansException;
4 import org.springframework.beans.factory.Aware;
5 import org.springframework.context.ApplicationContext;
6  
7 public interface ApplicationContextAware extends Aware {
8     void setApplicationContext(ApplicationContext var1) throws BeansException;
9 }

二、ServletContextAware 接口

1 package org.springframework.web.context;
2  
3 import javax.servlet.ServletContext;
4 import org.springframework.beans.factory.Aware;
5  
6 public interface ServletContextAware extends Aware {
7     void setServletContext(ServletContext var1);
8 }

 

三、InitializingBean 接口

1 package org.springframework.beans.factory;
2  
3 public interface InitializingBean {
4     void afterPropertiesSet() throws Exception;
5 }

 

四、ApplicationListener<ApplicationEvent> 接口

1 package org.springframework.context;
2  
3 import java.util.EventListener;
4 import org.springframework.context.ApplicationEvent;
5  
6 public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
7     void onApplicationEvent(E var1);
8 }

 

示例程序:

 1 package test.web.listener;
 2  
 3 import org.apache.logging.log4j.*;
 4 import org.springframework.beans.*;
 5 import org.springframework.beans.factory.InitializingBean;
 6 import org.springframework.context.*;
 7 import org.springframework.context.event.ContextRefreshedEvent;
 8 import org.springframework.stereotype.Component;
 9 import org.springframework.web.context.ServletContextAware;
10 import javax.servlet.ServletContext;
11  
12 @Component
13 public class StartupListener implements ApplicationContextAware, ServletContextAware,
14         InitializingBean, ApplicationListener<ContextRefreshedEvent> {
15  
16     protected Logger logger = LogManager.getLogger();
17  
18     @Override
19     public void setApplicationContext(ApplicationContext ctx) throws BeansException {
20         logger.info("1 => StartupListener.setApplicationContext");
21     }
22  
23     @Override
24     public void setServletContext(ServletContext context) {
25         logger.info("2 => StartupListener.setServletContext");
26     }
27  
28     @Override
29     public void afterPropertiesSet() throws Exception {
30         logger.info("3 => StartupListener.afterPropertiesSet");
31     }
32  
33     @Override
34     public void onApplicationEvent(ContextRefreshedEvent evt) {
35         logger.info("4.1 => MyApplicationListener.onApplicationEvent");
36         if (evt.getApplicationContext().getParent() == null) {
37             logger.info("4.2 => MyApplicationListener.onApplicationEvent");
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方法中处理。

 

转载于:https://www.cnblogs.com/wihainan/p/6239263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值