spring mvc web应用启动时执行特定处理
来源:互联网 发布:张馨予淘宝店名 编辑:程序博客网 时间:2024/06/06 18:48
FROM: http://www.cnblogs.com/yjmyzz/p/4747251.html
Spring-MVC的应用中,要实现 web应用启动时执行特定处理,主要是通过实现下面这些接口(任选一,至少一个即可)
一、ApplicationContextAware接口
1
2
3
4
5
6
7
8
9
package
org.springframework.context;
import
org.springframework.beans.BeansException;
import
org.springframework.beans.factory.Aware;
import
org.springframework.context.ApplicationContext;
public
interface
ApplicationContextAware
extends
Aware {
void
setApplicationContext(ApplicationContext var1)
throws
BeansException;
}
二、ServletContextAware 接口
1
2
3
4
5
6
7
8
package
org.springframework.web.context;
import
javax.servlet.ServletContext;
import
org.springframework.beans.factory.Aware;
public
interface
ServletContextAware
extends
Aware {
void
setServletContext(ServletContext var1);
}
三、InitializingBean 接口
1
2
3
4
5
package
org.springframework.beans.factory;
public
interface
InitializingBean {
void
afterPropertiesSet()
throws
Exception;
}
四、ApplicationListener<ApplicationEvent> 接口
1
2
3
4
5
6
7
8
package
org.springframework.context;
import
java.util.EventListener;
import
org.springframework.context.ApplicationEvent;
public
interface
ApplicationListener<E
extends
ApplicationEvent>
extends
EventListener {
void
onApplicationEvent(E var1);
}
示例程序:
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
package
test.web.listener;
import
org.apache.logging.log4j.*;
import
org.springframework.beans.*;
import
org.springframework.beans.factory.InitializingBean;
import
org.springframework.context.*;
import
org.springframework.context.event.ContextRefreshedEvent;
import
org.springframework.stereotype.Component;
import
org.springframework.web.context.ServletContextAware;
import
javax.servlet.ServletContext;
@Component
public
class
StartupListener
implements
ApplicationContextAware, ServletContextAware,
InitializingBean, ApplicationListener<ContextRefreshedEvent> {
protected
Logger logger = LogManager.getLogger();
@Override
public
void
setApplicationContext(ApplicationContext ctx)
throws
BeansException {
logger.info(
"1 => StartupListener.setApplicationContext"
);
}
@Override
public
void
setServletContext(ServletContext context) {
logger.info(
"2 => StartupListener.setServletContext"
);
}
@Override
public
void
afterPropertiesSet()
throws
Exception {
logger.info(
"3 => StartupListener.afterPropertiesSet"
);
}
@Override
public
void
onApplicationEvent(ContextRefreshedEvent evt) {
logger.info(
"4.1 => MyApplicationListener.onApplicationEvent"
);
if
(evt.getApplicationContext().getParent() ==
null
) {
logger.info(
"4.2 => MyApplicationListener.onApplicationEvent"
);
}
}
}
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。
0 0
- spring mvc web应用启动时执行特定处理
- spring mvc web应用启动时就执行特定处理(线程启动)
- 如何让spring mvc web应用启动时就执行特定处理
- Spring MVC让Web容器启动时自动执行代码
- spring mvc web应用异常处理
- Spring Boot学习--项目启动时执行特定方法
- spring启动完成后执行特定代码
- spring启动自动后执行特定方法
- Tomcat web应用启动时执行代码
- Spring MVC Web应用上下文
- [spring mvc]tomcat生成处理线程执行spring mvc时的思考
- spring集成mvc时web工程配置文件的启动顺序
- Spring MVC +Web 的执行过程
- spring项目停止时执行特定代码
- spring在web容器启动时执行初始化方法
- spring在web容器启动时执行初始化方法
- spring在web容器启动时执行初始化方法
- spring在web容器启动时执行初始化方法
- 创建一个简单的debugfs文件系统节点
- spring核心框架体系结构
- Git如何帮你查原因
- swagger2
- tomcat 设置并发连接数
- spring mvc web应用启动时执行特定处理
- Listview按键触发选择item改变文字
- Maven报错Please ensure you are using JDK 1.4 or above and not a JRE解决方法
- MFC显示图片
- 《软技能-代码之外的生存指南》-关于职业思考
- UploadDialog使用完整代码
- 利用Oracle DBMS_SCHEDULER调用shell脚本文件
- mysql的内连接、左外连接、右外连接、完全连接
- 网络编程(47)—— windows平台下的四种线程同步技术(一)