1. 程式人生 > >基於Spring task註解方式配置任務

基於Spring task註解方式配置任務

           此文標題有錯,感謝各位網友指出

        新建一個Java工程,匯入要用到的包,Spring3.2、Quartz2.2.1、aopalliance-1.0.jar、commons-logging-1.2.jar。整個工程目錄如下:


1、配置需要排程的類,並添加註解
package com.mucfc;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**  
*事件類,基於Spring註解方式
*作者 林炳文(
[email protected]
部落格:http://blog.csdn.net/evankaka) *時間 2015.4.29 */ @Component public class MyJob { public MyJob(){ System.out.println("MyJob建立成功"); } @Scheduled(cron = "0/1 * * * * ? ")//每隔1秒隔行一次 public void run(){ System.out.println("Hello MyJob "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())); } }
2、首先要配置我們的beans.xml,在xmlns 多加下面的內容
xmlns:task="http://www.springframework.org/schema/task"
3、然後xsi:schemaLocation多加下面的內容
    http://www.springframework.org/schema/task  
    http://www.springframework.org/schema/task/spring-task-3.0.xsd

4、最後是我們的task任務掃描註解

    <!--開啟這個配置,spring才能識別@Scheduled註解-->
     <task:annotation-driven/> 
5、自動配置掃描位置:
     <!-- 自動掃描註解的bean -->
    <context:component-scan base-package="com.mucfc"/>
6、整個文件如下
<?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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" 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/context   
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task  
    http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <!--開啟這個配置,spring才能識別@Scheduled註解-->
     <task:annotation-driven/>
     <!-- 自動掃描註解的bean -->
    <context:component-scan base-package="com.mucfc"/>

</beans>
7、使用
package com.mucfc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
    }

}

輸出結果:


一旦這個xml被載入進來來,就會自動建立bean的例項,並且開始定時任務了
需要注意的幾點:
1、spring的@Scheduled註解  需要寫在實現上
2、 定時器的任務方法不能有返回值(如果有返回值,spring初始化的時候會告訴你有個錯誤、需要設定一個proxytargetclass的某個值為true、具體就去百度google吧)
3、實現類上要有元件的註解@Component

相關推薦

基於Spring task註解方式配置任務

           此文標題有錯,感謝各位網友指出        新建一個Java工程,匯入要用到的包,Spring3.2、Quartz2.2.1、aopalliance-1.0.jar、common

spring定時器Task註解方式配置

第一步:spring配置檔案加入頭部加入 xmlns:task="http://www.springframework.org/schema/task" http://www.springframe

spring +ehcache 註解方式配置快取(spring與googlecode 的ehcache 兩種)

前言:有人知道 -spring  +ehcache 註解方式和mybatis +ehcache的區別嗎? (①spring的ehcache與②googlecode 的ehcache)//這是兩種配置,都能實現ehcache快取。綠色表示1需要,藍色表示2需要 黑色表示都需要 googl

spring boot 註解方式配置多資料來源與使用

1、首先看一下application-dev.yml 配置 spring:     datasource:         type: com.alibaba.druid.pool.Dru

Spring-Task 註解方式實現

第一步:編寫pojo 第二步:新增task相關的配置: 說明:理論上只需要加上<task:annotation-driven />這句配置就可以了,這些引數都不是必須的。 Ok配置完畢,當然spring task還有很多引數,我就不一一解釋了,具體參考

Spring註解方式配置事物

Spring以註解方式配置事物: @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)//類上配置的註解會對所有方法生效 publ

基於Spring註解方式配置Quartz

之前我們都是通過基於XML的方式實現Spring  Quartz 雖然配置起來特別的方便,但是Spring還支援基本註解的方式來配置,這樣做不僅更加簡單,而且程式碼量也更加少了很多。 1、配置需要排程的類,並添加註解 import java.text.SimpleDate

基於spring註解方式配置和使用spring AOP

spring AOP,面向切面程式設計,通常應用於系統的日誌,事物資訊輸出部分,如下圖中所示,當A呼叫B的f2方法時,如果我們想要讓系統在f2呼叫前輸出一些呼叫前的資訊,在f2呼叫結束後輸出一些呼叫結束後的資訊,一般的想法就是在呼叫f2前去呼叫C的f3,然後在呼

spring 註解方式配置定時任務

一、註解的方式 1:spring 配置檔案中增加這句 <task:annotation-driven/> 2:確保掃描程式能夠掃描後 下面第3步驟的java類 <context:component-scan base-pack

註解方式配置spring+springMVC

bsp ces roo 註意 public jsp 視圖 lte turn 1、新建類initConfig,繼承AbstractAnnotationConfigDispatcherServletInitializer,並重寫getRootConfigClasses()、ge

基於redis的spring task集群配置

ron runt parseint 工作空間 runtime point ava 自定義 ips   項目從單節點環境變為集群環境,這個時候要確保項目中的定時任務同一時刻只能在集群中的其中一個服務器中運行,但又不能寫死在哪一臺服務器上運行,怎樣才能實現這樣的需求? 思路

Spring註解方式配置Redis

@Configuration public class RedisConfiguraion { @Bean public JedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfigura

Spring Aop例項@Around、@Before、@After、@AfterReturning 、@AfterThrowing註解方式配置

用過spring框架進行開發的人,多多少少會使用過它的AOP功能,都知道有@Before、@Around和@After等advice。最近,為了實現專案中的輸出日誌和許可權控制這兩個需求,我也使用到了AOP功能。我使用到了@Before、@Around這兩個advice。但在,使用過程中,卻

spring轉springboot後,使用註解方式配置spring相關配置

spring專案轉springboot專案後,一般只需要配置yml相關的屬性就可以;當是有時候需要一些比較複雜的配置就需要使用bean註解方式來配置; 1.. 將spring相關的xml 中的bean物件進行注入: 直接上程式碼:yml配置就不給出了 spring.xml: &l

spring aop的基於xml以及註解配置

AOP概述 springAOP:面向切面程式設計。是面向物件程式設計的一種有效補充。面向物件程式設計的三大要素是繼承、多型和封裝。即在類設計時,要求讓不同的類設計不同的方法。但是這種模式下,也會產生一些弊端。比如增加了程式碼的重複性。比如我們想在類中增加日誌的記錄,如果讓每個類都做日

Spring學習——註解方式基本配置Spring實體(含與xml配置對比)

1、開啟使用註解配置 <!-- base-package="指定包路徑" --> <context:component-scan base-package="com.hh"></context:component-scan> 2、

Spring框架的事務管理之基於AspectJ的註解方式(重點掌握,最簡單的方式

1. 步驟一:恢復轉賬的開發環境(具體開發環境實現見:https://www.cnblogs.com/wyhluckdog/p/10137283.html)2. 步驟二:applicationContext的基本配置為: <?xml version="1.0" encoding="UTF-8"?

Spring 註解方式配置aop:

註解配置AOP(使用 AspectJ 類庫實現的),大致分為三步: 1. 使用註解@Aspect來定義一個切面,在切面中定義切入點(@Pointcut),通知型別(@Before, @AfterReturning,@After,@AfterThrowing,@Around)

05 Spring Aop例項(AOP 如此簡單)@Aspect、@Around 註解方式配置

轉載請註明來源 賴賴的部落格 導語 沒有什麼是不可以改變的,換個角度看世界,截然不同! IoC相關的基本內容告一段落,本次介紹Spring的第二個特性,AOP,面向切面程式設計,術語聽起來比較不容易理解,沒關係,一切盡在例項中,讓我們看一個簡單

Spring入門學習(基於XML檔案的方式配置事務) 第二十一節

Spring入門學習(基於XML檔案的方式配置事務) XML方式配置事務 XML方式配置事務 複製之前的包,去掉相關的註解,新建Spring配置檔案如下:<?xml version="1.0" encoding="UTF-8"?&