首页 技术 正文
技术 2022年11月8日
0 收藏 708 点赞 1,651 浏览 3894 个字

    在一个网站启动、结束时,我们经常有些操作是需要执行的。

熟悉Asp.net的朋友,使用Global.asax很容易就搞定,在其中有Application_Start和Application_End等方法可以供我们来轻松实现。

但是,在Java的SpringMVC框架中,需要如何实现这个功能呢?在互联网上,有不少类似文章,介绍功能的实现,我看过很多篇文档,基本都在一些关键点有所缺失,很多新手朋友照做往往达不到效果,下面我来阐述一下我正在使用的方法。

 

原理:使用注解@PostConstruct和@PreDestroy来实现功能。

    从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion);@PostConstruct和@PreDestroy。这两个注解被用来修饰一个非静态的void()方法 。写法有如下两种方式:

@PostConstruct

public
void applicationStart(){

System.out.println(“application start”);

}

 

public @PostConstruct void applicationStart(){

System.out.println(“application start”);

}

被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。执行生命周期如下:

SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)

 

下边直接来看程序中怎么写吧,下图是我用来测试的项目结构,标红的3个是这个功能需要涉及的文件。

SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)

其中,web.xml用来配置Spring的servlet,内容如下:

<?xml
version=“1.0”
encoding=“UTF-8”?>

<web-app
version=“2.4”
xmlns=“http://java.sun.com/xml/ns/j2ee”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>

 


<display-name>InskyScheduleCenter</display-name>

 


<!– Spring应用上下文,
理解层次化的ApplicationContext –>


<context-param>


<param-name>contextConfigLocation</param-name>


<param-value>/WEB-INF/config/spring/applicationContext*.xml</param-value>


</context-param>

 


<!– DispatcherServlet, Spring MVC的核心 –>


<servlet>


<servlet-name>InskyScheduleCenter</servlet-name>


<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


<!– DispatcherServlet对应的上下文配置,
默认为/WEB-INF/$servlet-name$-servlet.xml –>


<init-param>


<param-name>contextConfigLocation</param-name>


<param-value>/WEB-INF/config/spring/ScheduleCenter-servlet.xml</param-value>


</init-param>


<load-on-startup>1</load-on-startup>


</servlet>


<servlet-mapping>


<servlet-name>InskyScheduleCenter</servlet-name>


<!– mvc-dispatcher拦截所有的请求 –>


<url-pattern>/</url-pattern>


</servlet-mapping>

 

</web-app>

这个文件没啥说的,只是标明contextConfigLocation的位置。

再看ScheduleCenter-servlet.xml文件。

<?xml
version=“1.0”
encoding=“UTF-8”?>

<beans
xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xsi:schemaLocation=

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd”>

 


<!– 本配置文件为InskyScheduleCenter项目提供其相关的Spring MVC配置 –>

 

 


<!– 启用Spring基于annotation的DI, 使用户可以在Spring MVC中使用Spring的强大功能。
激活 @Required

@Autowired,JSR 250’s @PostConstruct, @PreDestroy and @Resource 等标注 –>


<context:annotation-config
/>

 

 


<!– 扫描自动加载的包名 –>


<context:component-scan
base-package=“com.insky.InskyScheduleCenter.web”>


</context:component-scan>

 

</beans>

其中,<context:annotation-config
/>一定要加上,这样才可以激活对@PostConstruct和@PreDestroy等注解。自动扫描的包名也要写对,确保我们的功能类global.java在配置的包名下。

最后看global.java文件。

package com.insky.InskyScheduleCenter.web.util;

 

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

 

import org.springframework.stereotype.Service;

 

/**

*

* web应用的全局事件

* @author Simon

*

*/

@Service

public
class global {

 

/**

* 在web启动时执行

*/

@PostConstruct

public
void applicationStart(){

System.out.println(“application start”);

}

 

/**

* 在web结束时执行

*/

@PreDestroy

public
void applicationEnd(){

System.out.println(“InskyScheduleCenter application end”);

 

}

}

我们看到,这个类位于配置的被扫描包名com.insky.InskyScheduleCenter.web之下。

其中,在applicationStart和applicationEnd方法之上,存在注解@PostConstruct和@PreDestroy,当网站启动时,自动扫描到这两个注解时,在相应的生命周期,就会执行被注解的方法。

注意global.java的class顶部被标红的注解@Service,在很多文章中,其贴出的代码上没有这个注解,很多照做的新手朋友最终没有加上,运行的时候就没有效果了,最终会多花很多时间去找问题。

因为配置文件中的<context:component-scan base-package=”com.insky.InskyScheduleCenter.web”></context:component-scan>只有扫描到有@Component @Controller@Service等这些注解的类,才会把这些类注册为bean,只有被注册为bean,才会加载到web容器的生命周期。

 

当然,实现这个功能还有很多其它的方式,如实现ApplicationListener接口等,我将会在未来的文章中阐述其它们。

 

    

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853