在 tomcat 8 的 spring mvc web 应用程序中尝试使用 quartz 2.2 调度程序调用服务 class 方法时出现空指针异常

Getting null pointer exception while trying to call a service class method using a quartz 2.2 scheduler in a spring mvc web application with tomcat 8

我有一个 java spring mvc 网络应用程序,它使用 Spring 3.2Tomcat 8。 我使用 quartz 2.2 来安排任务。 我的控制器 class 是:

@Controller
public class StatusController  implements Job
{
    @Autowired
    WebContentDefinitionService webContentDefinitionService;


    public void execute(JobExecutionContext arg0) throws JobExecutionException 
    {
         System.out.println("Starting Job");
        try 
        {
            webContentDefinitionService.deletePurgedContents();
            webContentDefinitionService.moveContentsToPurged();

        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
         System.out.println("Executed Job");

    }

}

我有一个 QuartzInitializer class 如下:

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;

@WebListener
public class QuartzListener extends QuartzInitializerListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        super.contextInitialized(sce);
        ServletContext ctx = sce.getServletContext();
        StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
        try {
            Scheduler scheduler = factory.getScheduler();
            JobDetail jobDetail = JobBuilder.newJob(StatusController.class).build();
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
                    CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")).startNow().build();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
        } catch (Exception e) {
            ctx.log("There was an error scheduling the job.", e);
        }
    }

}

如果我只在我的控制器中使用打印语句 class,它们会在指定的时间毫无问题地打印在控制台中。但是当我尝试从我的任何服务 classes 调用方法时,会抛出一个空指针异常。我从服务 classes 调用的方法实际上根本没有从控制器 class 访问,我已经使用调试模式确认了这一点。这些方法在其他地方也很管用。

我的 porm.xml 文件是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>BasicQuartz</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>BasicQuartz</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我不确定为什么我无法访问服务 class 方法。是不是因为我少了一些anotations??

尝试添加 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 在你的

public void execute(JobExecutionContext arg0) throws JobExecutionException

这应该有效。