springscheduled-tasksapplicationcontext

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


I'm trying to add a new task scheduler job into my Spring application. When I configure it, it gives compile time errors.

Here is my config file:

<?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" />

<context:annotation-config />

<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 compile time error it shows is:

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


Solution

  • It's because you don't have the task namespace declared in the head of your spring context file. I'm not sure what the current version is, but you need to do something like this:

    The bean tag requires the task namespace declaration and schema location:

    <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">
    
    "