博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-listener&spring-task注解版本
阅读量:4635 次
发布时间:2019-06-09

本文共 2324 字,大约阅读时间需要 7 分钟。

1.spring-listener:

a)

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

public class ServletImpl implements ServletContextListener {

@Override

public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub

System.out.println("web启动的时候调用此类********************************************************");

}

@Override

public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("webdestroy的时候调用此类********************************************************");

}

}

b)web.xml配置

<!-- 自定义监听器 -->

<listener>
<listener-class>com.servlet.impl.ServletImpl</listener-class>
</listener>

经过上边简单的两部操作,即可实现web容器启动时候执行某个动作。

2.spring-task

a)task.class,红色标记都是必须的。

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@EnableScheduling

@Component
public class FooTask {

@Scheduled(cron="0 0/1 8-23 * * ?")

public void run(){
System.out.println("start run...........................................................");
}
}

b)spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- auto scan task -->
<task:annotation-driven/>
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.task"/>

 参考:http://blog.csdn.net/wumingqian_137229/article/details/52846261 感谢分享!

 

转载于:https://www.cnblogs.com/wlhebut/p/6226211.html

你可能感兴趣的文章
xcode target
查看>>
js循环动态绑定带参数函数遇到的问题及解决方案[转]
查看>>
javamail gmail
查看>>
Linux C连接Mysql
查看>>
MyEclipse提示键配置、提示快捷键、提示背景色、关键字颜色、代码显示
查看>>
ATL的GUI程序设计(3)
查看>>
25个iptables常用示例
查看>>
react-navigation
查看>>
【DSP开发】C6000非多核非KeyStone系列DSP中断系统
查看>>
Ubuntu下如何解压缩zip,tar,tar.gz,tar.bz2文件
查看>>
Jenkins实现SVN+Maven+Java项目的持续集成
查看>>
Java:全局变量(成员变量)与局部变量
查看>>
101
查看>>
2014-01-04 SQL练习
查看>>
Android 悬浮窗口
查看>>
封装了一套WeCenter的IOS SDK
查看>>
Linux 用户行为日志记录
查看>>
SpringBoot学习之启动方式
查看>>
Linux Centos 7 安装配置nginx
查看>>
Java学习笔记---字符类型
查看>>