在春季计划中注入春豆

时间:2017-03-14 12:18:42

标签: spring scheduled-tasks spring-scheduled

我想使用spring安排一个方法。在引用此example

之后

我将我的applicationcontext文件配置为

<?xml version="1.0" encoding="UTF-8"?>  

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"
    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.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
            ">  
  <context:component-scan base-package="com.ankit.schedular"/>

    <task:annotation-driven/>


    <bean id="vendor" class="com.ankit.schedular.SchedularService"/>      

</beans>  

但是,我正在追踪异常

2017-03-14 17:12:18 ERROR ContextLoader:318 - Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.ankit.schedular.SchedularService] for bean with name 'vendor' defined in class path resource [service.xml]; nested exception is java.lang.ClassNotFoundException: com.ankit.schedular.SchedularService

我的SchedularService类定义为

public class SchedularService {

    private Logger logger = Logger.getLogger(SchedularService.class);


    @Scheduled(fixedDelay=5000)
    public void cronJob(){
        logger.info("Entered");
        logger.info(Calendar.getInstance().getTimeInMillis());
        logger.info("Exit");
    }
}

因为我不需要制作任何控制器,所以我没有在我的web.xml上声明调度程序servlet

你能说出这里出了什么问题吗?

编辑:添加web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/service.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

0 个答案:

没有答案