ssm定时器(全注解/配置)

本文档详细介绍了SSM框架下如何配置定时任务,包括配置文件中的命名空间添加、扫描包设定,以及全注解方式下的@EnableScheduling和@Scheduled的使用方法,确保定时任务在指定的扫描包下生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置方式

  1. 配置文件中添加命名空间   xmlns:task="http://www.springframework.org/schema/task"
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
  2. 添加扫描包;和注解
    
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:task="http://www.springframework.org/schema/task"
          xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
             http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
         <!-- 新闻的service -->
         <!-- <bean id="newsService" class="com.ssm.service.impl.NewsServiceImpl" /> -->
        <!-- 配置扫描-->
         <context:component-scan base-package="com.ssm.service.*"></context:component-scan>
        <context:annotation-config />
         <task:annotation-driven/>//使用的注解方式来实现Spring的定时任务
         
         <!-- 开启这个配置,spring才能识别@Scheduled注解     -->
         <<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
         <task:scheduler id="qbScheduler" pool-size="10"/>  
     </beans>

    3.定时任务  注意在扫描包下

  3. package com.ssm.service.task;
      
      import java.util.Date;
      
      import org.springframework.scheduling.annotation.Scheduled;
     import org.springframework.stereotype.Component;
      import org.springframework.stereotype.Service;
      
      
     @Component("spider")
     public class Spider {
     
         @Scheduled(fixedRate=10*1000)
         public void getNews(){
             System.out.println(new Date()+"-------getNews");
         }
     }

全注解方式

  • 使用两个注解@EnableScheduling@Scheduled
  • 定时任务也要在扫描包下
    @Component
    @EnableScheduling
    public class Spider {
     
        @Scheduled(fixedRate=10*1000)
        public void getNews(){
           System.out.println(new Date()+"-------getNews");
         }
     }

     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值