Spring Schedule配置及初始化

打开applicationContext,然后在现在的配置加一个标签,takst:annotaion-driven,这里的driven有好几个,

我们选择这一个

<task:annotation-driven/>

上面的xml的task已经加上了

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

那刚刚选的时候一定不要选错schema,虽然都是同一个annotaion-driven

<context:property-placeholder location="classpath:datasource.properties"/>

这个location我们怎么添加呢,首先我们先填上classpath,然后把这个配置闭合
<?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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <context:component-scan base-package="com.learn" annotation-config="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <aop:aspectj-autoproxy/>

    <!-- 二期新增spring schedule的时候新增的 -->
    <context:property-placeholder location="classpath:datasource.properties"/>
    <task:annotation-driven/>


    <import resource="applicationContext-spring-session.xml"/>
    <import resource="applicationContext-datasource.xml"/>

</beans>
首先我们创建一个类,这个类我们就叫CloseOrderTask,例如说下了一个订单,30分钟之类都没有付款的话,

那么我们就应该把这个订单关闭,然后把这个下单的产品的库存,再增加到产品表中,这样就保证产品表的库存

又回来了,那既然我们要他把放到Spring容器当中,我们要加一个注解,@Component,然后使用@Slfj注解,来打印

日志等,这样我们对应Schedule的一个配置,就写好了,然后我们在这里填充定时任务的方法,然后加上Schedule的

注解呢,就可以启动了,那既然我们要定时关单,注入Order相关的业务逻辑,那我们把OrderService注入进来,

@Component
@Slf4j
public class CloseOrderTask {

    @Autowired
    private IOrderService iOrderService;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值