无法在应用程序上下文配置文件中添加任务计划程序标签

Not able to add task scheduler tags in application context configuation file

我已经创建了一个简单的 spring 应用程序,我正在尝试在其中添加一个新的任务计划程序作业。当我配置它时,它会给出编译时错误。我在谷歌上搜索了很多,但不确定为什么会这样。这是我的配置文件。任何想法

<?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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">


<context:component-scan base-package="com.ibm.spring" />

<bean id="location" class="com.ibm.spring.Location" autowire="byName"  scope="singleton">
    <property name="addresses">
        <list>
            <ref bean="address2"></ref>
        </list>
    </property>
</bean>

<bean id="address1" class="com.ibm.spring.Address">
    <property name="id" value="1"></property>
    <property name="street" value="shahjahan"></property>
</bean>

<bean id="address2" class="com.ibm.spring.Address">
    <property name="id" value="2"></property>
    <property name="street" value="Akbar"></property>
</bean> 
<bean id="restaurant" class="com.ibm.spring.Restaurant" scope="prototype">
</bean>

<task:scheduled-tasks scheduler="printingScheduler">
   <task:scheduled ref="printer" method="print" fixed-delay="3000" />
 </task:scheduled-tasks>

 <task:scheduler id="printingScheduler" />

</beans>

编译时错误显示为

The prefix "task" for element "task:scheduled-tasks" is not 

绑定。

这是因为您没有在 spring 上下文文件的头部声明任务命名空间。我不确定当前版本是什么,但您需要执行以下操作:

bean 标记需要任务命名空间声明和架构位置:

<bean... 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.0.xsd">

"

为什么不使用基于@annotation 的配置。只需要在配置文件中包含这个@EnableScheduling,你就可以开始安排任务了。

您可以访问:- https://www.baeldung.com/spring-scheduled-tasks

https://spring.io/guides/gs/scheduling-tasks/