zoukankan      html  css  js  c++  java
  • scheduled定时任务+实例请求数据库

    1.scheduled定时任务类:ScheduledDemo.java

    package com.nantian.scheduled;

    import java.util.Date;

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

    /**
    * scheduled定时任务
    *@author xjj13
    *@component (把普通pojo实例化到spring容器中,相当于配置文件中的
    *<bean id="" class=""/>)
    *泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
    */
    @Component
    public class ScheduledDemo {
    /**
    * 定时任务方法
    * @Scheduled:设置定时任务
    * 方法上添加该注解,表示方法会以定时任务的方式出现,一旦时间到达后,该方法就会被触发
    * cron:(设置触发时间)cron表达式,定时任务触发是时间的一个字符表达式
    */
    @Scheduled(cron="0/2 * * * * ?")//每两秒触发一次方法
    public void scheduledMethod() {
    System.out.println("定时器被触发"+new Date());
    }
    }
    2.在启动类中开启定时任务的启用

    springBoot中默认是对scheduled是不开启的

    @SpringBootApplication
    @EnableScheduling//开启scheduled定时任务
    public class Application {
    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }

    }

    ================================================================================

    实例请求数据库

     1.Application.java

    package com.nantian;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;

    @SpringBootApplication
    @EnableScheduling
    public class Application {
    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }

    }

    2.

    package com.nantian.scheduled;

    import java.util.Date;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import com.nantian.dao.UserDao;
    import com.nantian.pojo.User;

    /**
    * scheduled定时任务
    *@author xjj13
    *@component (把普通pojo实例化到spring容器中,相当于配置文件中的
    *<bean id="" class=""/>)
    *泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
    */
    @Component
    public class ScheduledDemo {
    @Autowired
    private UserDao userDao;
    /**
    * 定时任务方法
    * @Scheduled:设置定时任务
    * 方法上添加该注解,表示方法会以定时任务的方式出现,一旦时间到达后,该方法就会被触发
    * cron:(设置触发时间)cron表达式,定时任务触发是时间的一个字符表达式
    */
    @Scheduled(cron="0/1 * * * * ?")//每两秒触发一次方法
    public void scheduledMethod() {
    List<User> users=(List<User>) userDao.findAllUsers();
    System.out.println(users);
    System.out.println("定时器被触发"+new Date());
    }

    }

    3.执行结果图

  • 相关阅读:
    《Cracking the Coding Interview》——第6章:智力题——题目2
    《Cracking the Coding Interview》——第6章:智力题——题目1
    《Cracking the Coding Interview》——第5章:位操作——题目8
    《Cracking the Coding Interview》——第5章:位操作——题目7
    《Cracking the Coding Interview》——第5章:位操作——题目6
    Spyder 调出绘图界面
    作为非计算机专业的学生,觉得 C 语言远比其他语言易于上手,正常吗?
    vs2015 + Python3.5 环境搭建
    更新32位Spyder从3.0.0-> 3.2.3
    luogu P1047 校门外的树 x
  • 原文地址:https://www.cnblogs.com/curedfisher/p/11933067.html
Copyright © 2011-2022 走看看