打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何让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 @Component13 public class StartupListener implements ApplicationContextAware, ServletContextAware,14         InitializingBean, ApplicationListener<ContextRefreshedEvent> {15  16     protected Logger logger = LogManager.getLogger();17  18     @Override19     public void setApplicationContext(ApplicationContext ctx) throws BeansException {20         logger.info("1 => StartupListener.setApplicationContext");21     }22  23     @Override24     public void setServletContext(ServletContext context) {25         logger.info("2 => StartupListener.setServletContext");26     }27  28     @Override29     public void afterPropertiesSet() throws Exception {30         logger.info("3 => StartupListener.afterPropertiesSet");31     }32  33     @Override34     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方法中处理。

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring AOP实现日志输出
Spring的事务管理入门:编程式事务管理(TransactionTemplate)
spring boot 实际应用(一) 内置tomcat 实现JMX配置
在Spring中发布SOAP HTTP Webservice - Java杂家 - Blo...
Spring Boot 优雅重启知多少
服务器spring boot版本,平滑升级
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服