xml之xmlns 以及spring task

估计有人会和我产生同样的问题,xml配置文件开始部分第一个bean 里面的内容不是很懂,但是基本的使用应该是没有什么问题的,感兴趣的同学会去google,百度下,记录下来 我只是其中一个。旨在自己谨记,观者分享。

始由

http://www.w3school.com.cn/xml/xml_namespaces.asp xml 命名空间的讲解这里已经表明,不做过多赘述。
看下我们经常会看到的配置

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/task  
           http://www.springframework.org/schema/task/spring-task-3.1.xsd  "
    default-autowire="byName" default-lazy-init="false">

上面是我们项目的xml 配置命名空间

其中bean,context,aop,tx,task 都是spring最熟悉不过的的。
如果你要想使用xml配置某个标签你就要去添加相对应的 xmlns ,xml解析就会根据相应的规则去做判断
demo

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=”http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd ”

这个是task xmlns 其中xmlns:task “task” 是定义的标签名称 ,你可以直接访问 这个
url “http://www.springframework.org/schema/task” 你会发现这个对应的“房子”你就到了spring task 的“家”了,而相应的规则 就是里面具体的 定义规则。

<task:annotation-driven/>  

Element : annotation-driven
Enables the detection of @Async and @Scheduled annotations on any Spring-managed object. If
present, a proxy will be generated for executing the annotated methods asynchronously. See Javadoc
for the org.springframework.scheduling.annotation.EnableAsync and
org.springframework.scheduling.annotation.EnableScheduling annotations for information on code-
based alternatives to this XML element.
启用相应的注解

代码里面你可以这样 写下

`
import java.io.IOException;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.json.JSON;
import com.tf56.ps.dao.MyTaskService;
import com.tf56.ps.entity.AdsManage;

@Component
public class MyTaskServiceImpl implements MyTaskService {

//涉及到cron 表达式 http://cron.qqe2.com/
//5s 
@Scheduled(cron="0/5 * *  * * ? ")
@Override
public void SysWork() {
    System.err.println("--------------------- SysWork mytask begin------------------");
}

@Scheduled(cron="0/7 * *  * * ? ")
@Override
public void DoWork() {
    System.err.println("--------------------- DoWork mytask begin------------------");  
}

}
`
以上你可能已经实现了基于注解的spring task 配置 。
反例 非基于注解实现task 配置 较多可能涉及task 具体代码解读稍后奉上。
cron 表达式 http://cron.qqe2.com/ 没做过深入解读
本篇基于 xml命名空间探索引申到task 注解配置 菜鸟奉上 欢迎交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringTaskSpring框架中提供的一种用于在指定时间执行任务的调度框架。它可以定时执行任务、循环执行任务、以及在指定时间执行一次性任务等。 SpringTask的使用步骤如下: 1. 在Spring配置文件中添加Task命名空间和TaskExecutor线程池配置,如下所示: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <task:executor id="taskExecutor" pool-size="5"/> <task:scheduler id="taskScheduler" pool-size="10"/> <!-- bean definitions go here --> </beans> ``` 其中,`task:executor`定义了一个线程池,用于执行任务;`task:scheduler`定义了一个调度器,用于调度定时任务。 2. 在Spring配置文件中定义一个定时任务,如下所示: ```xml <bean id="myTask" class="com.example.MyTask"/> <task:scheduled-tasks scheduler="taskScheduler"> <task:scheduled ref="myTask" method="run" cron="0 0/5 * * * ?"/> </task:scheduled-tasks> ``` 其中,`myTask`是一个简单的任务类,`cron`属性指定了任务的执行时间。 3. 启动Spring容器,Spring会自动启动定时任务,按照指定的时间执行任务。 除了定时任务外,SpringTask还支持按照一定的间隔时间执行任务、以及在指定时间执行一次性任务等。具体使用方式可以参考Spring官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值