XML configuration:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5 xmlns:task= "Http://www.springframework.org/schema/task" 6xsi:schemalocation="7http//Www.springframework.org/schema/beans8http//www.springframework.org/schema/beans/spring-beans-3.2.xsd9http//Www.springframework.org/schema/contextTenhttp//www.springframework.org/schema/context/spring-context-3.2.xsd One / http www.springframework.org/schema/task A http://www.springframework.org/schema/task/spring-task-3.2.xsd "> -<!--Scan Package base directory-- -<context:component-scan base- Package= "Com.wisezone"/> the<!--identification @scheduled annotations-- - <task:annotation-driven/> - -</beans>
Java code:
1 PackageCom.wisezone.service;2 3 ImportJava.text.SimpleDateFormat;4 Importjava.util.Date;5 6 Importorg.springframework.scheduling.annotation.Scheduled;7 ImportOrg.springframework.stereotype.Service;8 9 @ServiceTen Public classJobservice { One A@Scheduled (cron= "0/2 * * * * *?")) - Public voidJob () { -SYSTEM.OUT.PRINTLN ("Timed output task:" +NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (NewDate ())); the } -}
Test class:
1 Packagecom.wisezone.test;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 Public classTest7 {8 Public Static voidMain (string[] args)9 {TenApplicationContext app =NewClasspathxmlapplicationcontext ("Beans.xml"); One A } -}
Results:
Spring implements timed tasks with annotations