spring定时器及注入

一、定时器的配置(注意配图中beans里面,要有相关的引用)spring

xmlns:task="http://www.springframework.org/schema/task"component

http://www.springframework.org/schema/taskxml

http://www.springframework.org/schema/task/spring-task-3.2.xsd对象

<!-- task任务扫描注解 -->开发

<task:annotation-driven />io

<context:component-scan base-package="所须要扫描的包或类"></context:component-scan>配置

二、建立相对应的处理业务的Task方法。service

注:须要添加相关的注解。@Component、@Scheduled。定时器

三、定时器已经基本知足了基本业务的开发,可是,因为定时器的执行优先于注入,所以咱们不能经过@Resource注入service。引用

所以咱们须要建立一个类ApplicationContextUtil,用来获取service。

四、定时器中经过ApplicationContextUtil类,得到service。

注:相对应的,在service中须要配置service名称。

这样咱们就能够获取到service对象进行相对应的业务处理

相关文章
相关标签/搜索