Spring定时器技术终结者——采用XML配置的方式实现Spring定时器

在Spring中有两种方式可以实现定时器的功能,分别是Scheduled注释方式和XML配置方式,本博客将介绍如何在Spring中使用采用XML配置的方式实现定时器的功能,代码及相应的解释如下:

代码1—Spring配置文件(applicationContext.xml文件):

<?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.xsd
          http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task.xsd">

	<bean id="jobDispatcher" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject"><!-- 指定调度对象 -->
			<bean class="com.ghj.packageoftimer.SpringTimerTest" />
		</property>
		<property name="targetMethod" value="test" /><!-- 指定定时器执行调度对象中的那个方法 -->
		<property name="concurrent" value="false" /><!-- 配置job是否可以并行运行 -->
	</bean>

	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobDispatcher" />
		<property name="cronExpression" value="0 0/1 * * * ?" /><!-- cronExpression用于指明该方法被调用的时机 -->
	</bean>

	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list><!-- 通过在list标签中的ref标签可以放置一个或多个触发器 -->
				<ref bean="cronTrigger" />
			</list>
		</property>
	</bean>
</beans>

代码2——Spring定时器测试类(SpringTimerTest.java文件):

package com.ghj.packageoftimer;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Spring定时器测试类
 *
 * @author 高焕杰
 */
public class SpringTimerTest{

	/**
	 * Spring定时器测试方法
	 *
	 * @author 高焕杰
	 */
    public void test(){
    	System.err.println(new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒").format(new Date()));
    }
}

代码3——加载Spring配置文件并启动Spring定时器的类(StartSpringTimer.java文件):

package com.ghj.packageoftest;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 加载Spring配置文件,启动Spring定时器
 *
 * @author 高焕杰
 */
public class StartSpringTimer {

	public static void main(String[] args){
		new ClassPathXmlApplicationContext("conf/spring/applicationContext.xml");
		System.out.println("加载Spring配置文件完毕,Spring定时器成功启动!!!");
	}
}
时间: 2024-08-12 11:29:21

Spring定时器技术终结者——采用XML配置的方式实现Spring定时器的相关文章

Spring定时器技术终结者——采用Scheduled注释的方式实现Spring定时器

在Spring中有两种方式可以实现定时器的功能,分别是Scheduled注释方式和XML配置方式,本博客将介绍如何在Spring中使用Scheduled注释方式的方式实现定时器的功能,代码及相应的解释如下: 代码1-Spring配置文件(applicationContext.xml文件): <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframe

Springmvc案例1----基于spring2.5的采用xml配置

首先是项目和所需要的包截图: 修改xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLoca

【Spring】AOP之基于XML配置总结与案例

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.AOP的一些概念 AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入封装.继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合.当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力.也就是说,OOP允许你定义从上到下的关系,但

Spring学习篇02-Bean的xml配置

1.bean标签 在Spring中,要在xml中配置一个bean,必须使用bean标签这个标签包含了要两个重要的属性: id:对bean进行标记在容器中是唯一的 class:bean的类类型 scope:表示该bean的实例个数,默认为单例. 2.property标签 property是bean的子标签,通过它可以向bean实例的属性进行注入,重要属性如下: name:对应类的属性 value:要注入类属性的原始值 ref:要注入类属性的引用bean的id property里面可以包含bean的

spring Ioc容器之使用XML配置Bean

1.项目截图 2.创建xml文件 3.打印机接口 package com.example.demo.computerTest; public interface Printer { void init(); void print(String txt); } 4.彩色打印机 package com.example.demo.computerTest; public class ColorPrinter implements Printer { @Override public void init

AspectJ通过xml配置的方式来实现

AspectJ来通过xml配置实现可以通过参考查看下面的代码: <?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:aop="http:/

Spring中用java config简化xml配置

个人感觉还是这种方式配置方式最灵活了. package com.baobaotao.conf; public class LogDao {     public void print(){         System.out.println("helloworld");     } } package com.baobaotao.conf; public class UserDao {     public void print(){         System.out.print

【Spring四】AOP之XML配置

AOP:Aspect Oriented  Programming 面向切面编程 面向切面编程的核心是动态代理设计模式.请先參见动态代理设计模式笔记. 以Hibernate保存一个对象到数据库为例,因为保存数据时须要开启事务,利用面向切面编程思想,将事务的处理分离出来.当作一个切面来处理. jdk的动态代理的缺点: 1.在拦截器中,切入点的推断是很复杂的 2.尽管实现了切面与目标类的松耦合,可是在拦截器中还得实现结合过程 一.springAOP的原理: 目标类:在目标类的方法调用的前后,我们须要增

spring boot热部署pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersi