• Spring定时器实现(一)


    Spring定时器简单应用实现,如下:

    首先、Spring配置文件:

    <?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:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.3.xsd ">
    
        <description>spring-configuration</description>
    
        <bean id="timerTask" class="com.charles.spring.service.impl.TimerTaskImpl"></bean>
    
        <task:scheduled-tasks>
            <task:scheduled ref="timerTask" method="doTimerTask" cron="0/5 * * * * ?" />
        </task:scheduled-tasks>
    
    </beans>

    其次、相关定时器接口(忽略不计,只是定义一个方法)、类:

    package com.charles.spring.service.impl;
    
    import com.charles.spring.service.TimerTask;
    
    public class TimerTaskImpl implements TimerTask {
    
        @Override
        public void doTimerTask() throws Exception {
            System.out.println("Hello Timer");
        }
    
    }

    最后测试,测试项目是Java项目,所以只需要加载Spring就可以了,如下:

    package com.charles.spring.handler;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Timer {
    
        public static void main(String[] args) {
            
            @SuppressWarnings({ "unused", "resource" })
            ApplicationContext context = new ClassPathXmlApplicationContext("config/spring-config.xml");
            try {
                Thread.sleep(10*60*1000);
            } catch (Exception e) {
                
            }
    
        }
    
    }

    结束。结果:

  • 相关阅读:
    python中的 if __name__ == “__main__”: 有什么用
    LeetCode Two Sum 解题思路(python)
    numpy cheat sheet
    matlab中换行
    从github下载一个单一文件
    tensorflow轮子下载地址 wheels(whl)
    tensorflow报错 Key Conv/biases not found in checkpoint
    tensorflow报错 tensorflow Resource exhausted: OOM when allocating tensor with shape
    西门子 1500 1200 PLC,位访问, 字节访问
    查看pip install安装的python包的位置
  • 原文地址:https://www.cnblogs.com/itachy/p/7211474.html
Copyright © 2020-2023  润新知