xml之xmlns 以及spring task

本文探讨了XML配置文件中命名空间的使用方法,并详细解释了如何通过Spring Task进行基于注解的任务调度配置,包括cron表达式的应用。

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

估计有人会和我产生同样的问题,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 注解配置 菜鸟奉上 欢迎交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值