spring的scheduled-tasks定时任务,applicationcontextaware获取spring上下文

本文介绍了如何在Spring中使用`scheduled-tasks`进行定时任务配置,并通过`ApplicationContextAware`接口获取Spring上下文以实现对Service的依赖注入。示例中展示了在`application.xml`中配置`<task:annotation-driven/>`、`<task:scheduled-tasks>`,以及创建`ApplicationContextUtis`工具类来静态获取ApplicationContext。同时,展示了`MonitorTask`类如何调用Service的方法,实现了定时任务的功能。

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

<-- application.xml中添加下面的代码-->
<!-- 定时任务 -->
	<task:annotation-driven/>
	<!-- applicationcontextaware,获取service -->
	<bean id="applicationContextUtis" class="com.infotrust.common.utils.commutils.ApplicationContextUtis" />
	<!-- 定时任务所在的类 -->
	 <bean id="monitorTask" class="com.infotrust.creditreport.reportmonitor.task.MonitorTask" /> 
	<!-- 监控定时任务配置时间 -->
	<task:scheduled-tasks>   
        <task:scheduled ref="monitorTask" method="monitorinfo" cron="0 58 14 * * ?"/> 
    </task:scheduled-tasks>
package com.infotrust.common.utils.commutils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 定时任务类用到:获取ApplicationContext获取spring上下文,获取service
 * @date 2018年3月30日上午11:17:07
 */
public class ApplicationContextUtis implements ApplicationContextAware{
	private static ApplicationContext applicationContext;

	public static ApplicationContext getApplicationContext()
			throws BeansException {
		return applicationContext;
	}
	
	public void setApplicationContext(ApplicationContext apc)
			throws BeansException {
		applicationContext = apc;
		
	}

	/**
	 * 获取service的方法
	 * @param beanName service的名称
	 * @return
	 */
	public static Object getBean(String beanName){
		return applicationContext.getBean(beanName);
	}
}
<--springmvc.xml中添加需要用service的扫描的包-->
<context:component-scan base-package="com.infotrust.creditreport.reportmonitor.task">
</context:component-scan>
package com.infotrust.creditreport.reportmonitor.task;

import java.util.List;

import org.springframework.stereotype.Component;

import com.infotrust.common.utils.commutils.ApplicationContextUtis;
import com.infotrust.creditreport.reportmonitor.po.ReportMonitor;
import com.infotrust.creditreport.reportmonitor.service.IReportmonitorService;

/**
 * 定时任务的执行的类和方法
 * @date 2018年3月30日上午10:24:26
 */
@Component
public class MonitorTask{
	
	public void monitorinfo(){
//使用ApplicationContextUtis工具类获取service
IReportmonitorService reportmonitorService=(IReportmonitorService)ApplicationContextUtis.getBean("reportmonitorService");
		List<ReportMonitor> relist= reportmonitorService.getReportmonitorList(null, null, null, null, null);
		for (ReportMonitor reportMonitor : relist) {
			System.out.println(reportMonitor.getEnterprisename());
		}
	}

}
@Service("reportmonitorService")//service别名
//实现类方法
public class ReportmonitorServiceImpl implements IReportmonitorService {}
//注解获取service,需要application.xml配置service的bean
 @Autowired
 private IReportmonitorService reportmonitorService;
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值