配置xml实现定时任务

1.创建一个xml(名字.xml),注意代码中的名字A,名字B,名字C的位置,且和其他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://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	//id名字要唯一,不要和其他的定时任务bean重了,class参数写到要运行方法所在的类的名字
    <bean id="名字A" class="cn.wiztek.chsms.nhousechange.servlet.NhouseChangeAppServlet">
    </bean>
    
	<!-- 配置作业类 -->
	<bean id="名字B" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
		<property name="targetObject">  <!-- 指定任务类 -->
			<ref bean="名字A"/>
		</property>  
		<property name="targetMethod" value="要运行的方法名" />  <!-- 指定运行的方法 -->
		<property name="concurrent" value="false" />  <!-- 作业不并发调度 -->  
	</bean>
	
	<!-- 配置作业调度的触发方式(触发器) -->
	<bean id="名字C" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="名字B" />
		<property name="cronExpression" value="0 0 9 * * ?" />	<!-- 每天9点执行一次 -->
	</bean>
	
	<!-- 配置调度工厂  -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="名字C" />
			</list>
		</property>
	</bean>
	
</beans>

2.在applicationContext.xml中添加

//引号内容为刚才建的xml的位置,从和applicationContext.xml同级目录位置开始写(新建xml和applicationContext.xml在同一个文件夹中),不在同文件夹情况本人没试过
<import resource="..位置../名字.xml" />

3.重启程序测试即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值