• spring定时器的配置


    首先,新建一个java项目,下面导入需要的jar包:

    这里有你需要的jar包哦。

    jar包下载

    在src文件夹下,新建一个applicationContext.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 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:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
      
          <!-- Spring 定时器演示 -->
    
    <!--     定时器开关   -->
        <task:annotation-driven /> 
      
        <bean id="LogTask" class="com.test.Test"></bean>  
      
        <task:scheduled-tasks>  
    <!--         这里表示的是2s执行一次    -->
            <task:scheduled ref="LogTask" method="execute" cron="*/2 * * * * ?" />  
        </task:scheduled-tasks>  
          
        
    </beans>
         

    在web.xml中配置:

    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

    创建定时启动的java类:

    test.java

    package com.test;
    
    public class Test {
        public void execute() {
            System.out.println("定时器启动了。。。");
        }
    }

    部署在Tomcat中启动Tomcat即可。

    具体定时器执行的时间可以再行研究。

    此demo亲测好使。。。。。

  • 相关阅读:
    react中的生命周期钩子
    vue小知识
    vue发布中的前后端分离和前后端不分离
    vue中的vuex
    vue项目的发布
    stylus解决移动端1像素
    一行代码实现数组去重(ES6)
    详解js中Number()、parseInt()和parseFloat()的区别_javascript技巧
    关于element-ui 的日期时间选择器的超出时间无法选择的设置
    git使用
  • 原文地址:https://www.cnblogs.com/zjiacun/p/6782495.html
Copyright © 2020-2023  润新知