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

熟悉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()方法之前执行。执行生命周期如下:

 

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

其中,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接口等,我将会在未来的文章中阐述其它们。

 

    

SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)的更多相关文章

  1. JS流程控制语句 做判断(if语句)if语句是基于条件成立才执行相应代码时使用的语句。语法:if(条件) { 条件成立时执行代码}

    做判断(if语句) if语句是基于条件成立才执行相应代码时使用的语句. 语法: if(条件) { 条件成立时执行代码} 注意:if小写,大写字母(IF)会出错! 假设你应聘web前端技术开发岗位,如果 ...

  2. 判断语句(if...else)if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码

    判断语句(if...else) if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码. 语法: if(条件) { 条件成立时执行的代码 } else { 条件不成立时 ...

  3. Spring-boot 启动完成时执行指定任务

    在服务启动完成时,如果需要执行一些特定的预加载任务,则可以通过实现 CommandLineRunner 接口来实现. 实现 @Component public class Started implem ...

  4. 齐博cms最新SQL注入网站漏洞 可远程执行代码提权

    齐博cms整站系统,是目前建站系统用的较多的一款CMS系统,开源,免费,第三方扩展化,界面可视化的操作,使用简单,便于新手使用和第二次开发,受到许多站长们的喜欢.开发架构使用的是php语言以及mysq ...

  5. SpringMVC中向服务器传递时间参数时出现的问题

    1. 问题描述: 今天在SpringMVC应用中上传参数的时候遇到如下问题: The request sent by the client was syntactically incorrect 这说 ...

  6. 在SpringMVC中使用@RequestBody注解处理json时,报出HTTP Status 415的解决方案

    Spring的@RequestBody非常牛x,可以将提交的json直接转换成POJO对象. 正好今天有这样的需求,使用一下,结果一直报415,十分头疼. HTTP 415 错误 – 不支持的媒体类型 ...

  7. SpringMVC中在Controller类的每个方法执行前调用某个方法的实现

    在使用SpringMVC做项目的时候,如果想在@Controller类中每个@RequestMapping方法执行前都调用某个方法,要怎么实现呢?答案是使用Spring的@ModelAttribute ...

  8. is7.0中发布mvc网站,一直无法正常执行路由的解决办法

    在config中加一句话: <system.webServer> <validation validateIntegratedModeConfiguration="fals ...

  9. IDEA问题之“微服务启动项目时,不会加载Spring Boot到Services中”

    1.启动项目时,不会加载Spring Boot到Services中 现象解析: 启动项目时 会在debug的位置加载项目 注:这里没有配图,因为问题已解决,未记录图,需往后遇到记录 解决方案: 需要在 ...

随机推荐

  1. python应用案例

    安装库 : PIL(Image.ImageDraw.ImageFont.zlib).jpeg 常见问题 (1) Could not find a version that satisfies the ...

  2. ubuntu下gedit闪退,遇到问题:ERROR:../../gi/pygi-argument.c:1583:_pygi_argument_to_object: code should not be reached 已放弃 (核心已转储)

    解决方法:编辑->首选项关闭->插件->取消"多文件编辑"

  3. php用redis保存session

    1.修改php.ini中session配置: ini_set('session.save_handler', 'redis');ini_set('session.save_path', 'tcp:// ...

  4. DEDEcms 在php5.4以上 后台登录空白解决办法

    本次环境php5.5 首先确定 dede data文件夹已经有写入权限 775 777都可以 然后再远程桌面或者FTP修改 include/userlogin.class.php 文件 注释掉下面六句 ...

  5. 百度编辑器ueditor插入表格没有边框颜色的解决方法

    附:从word excel 中 复制的表格提交后无边框,参考这个同学的,写的很详细:   http://blog.csdn.net/lovelyelfpop/article/details/51678 ...

  6. win10连vpn

    1.首先卸载网络适配器下所有的WAN Miniport 2.打开命令提示符,输入:netsh interface ipv4 uninstall 卸载TCP/IPv4协议. 3.重启电脑后再次打开“命令 ...

  7. 关于JSF中immediate属性的总结(一)

    Purpose The immediate attribute can be used to achieve the following effects: Allow a commandLink or ...

  8. asp.net mvc使用log4gNetz

    1. 下载安装log4gNet 2. 将 \bin\net\4.0\release\log4net.dll 复制到你的项目中 . 3. 将log4net.dll 添加引用到你的项目中. 4. 添加如下 ...

  9. ObjC运行时部分概念解析(一)

    转型iOS已经许久了,Runtime(运行时)还没有好好了解过.之前没有阅读过源码,紧紧凭借自己的臆测.现在阅读下源码,做一些笔记.方便再次翻阅 SEL SEL是一个关键字,如果没有涉及runtime ...

  10. 安装Axure7.0,完整教程,有验证码和汉化包

    以下内容由Axure中文网 » Axure7.0中文汉化语言包下载 axure汉化包 改编,特此声明 1.下载安装包 官方下载页面: http://www.axure.com/download 官网直 ...