小编典典

在带有tomcat 8的spring mvc Web应用程序中尝试使用石英2.2调度程序调用服务类方法时获取空指针异常

tomcat

我有一个java spring mvc使用Spring 3.2和的Web应用程序Tomcat 8。我quartz 2.2用来安排任务。我的控制器类是:

@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类,如下所示:

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);
        }
    }

}

如果仅在控制器类中使用print语句,则它们将在控制台中打印,并且在指定的时间没有任何问题。但是,当我尝试从我的任何服务类中调用方法时,都会引发空指针异常。我从服务类中调用的方法实际上根本无法从控制器类中访问,我已经使用调试模式确认了这一点。这些方法在其他地方都可以正常工作。

我的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>

我不确定为什么无法访问服务类方法。是因为我想念一些anotations吗?


阅读 323

收藏
2020-06-16

共1个答案

小编典典

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

public void execute(JobExecutionContext arg0) throws JobExecutionException

这应该工作。

2020-06-16