简体   繁体   English

自动接线和TimerTask问题

[英]Problems with autowired and timertask

I'm having some problems with annotations and the timertask class. 我在注释和timertask类上遇到了一些问题。 I'm trying to add the autowired bean into the schedule, but I get the error: NullPointerException. 我正在尝试将自动装配的Bean添加到计划中,但出现错误:NullPointerException。 Why? 为什么? I'm quite new to Spring and would be happy if someone could give me a hint. 我对Spring还是很陌生,如果有人可以给我提示,我会很高兴。 Please ask if I can provide you with more info. 请询问我是否可以为您提供更多信息。

@Service
@Scope("singleton")
public class TimeIntervalTriggerService {
    private static final Logger LOG = LoggerFactory.getLogger(TimeIntervalTriggerService.class);
    private static final int updateFrequency = 1000 * 60 * 60 * 3;

    @Autowired
    UpdateTableTask updateTableTask;


    public TimeIntervalTriggerService() {
        super();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(updateTableTask,5000,updateFrequency);
    }

Here is the TimerTask class 这是TimerTask类

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UpdateTableTask  extends TimerTask{

    private static final Logger LOG = LoggerFactory.getLogger(UpdateTableTask.class);

    @Autowired
    TimerProcessor timerProcessor;

    @Override
    public void run() {
        LOG.info("Is working??");
        timerProcessor.doIt();
        LOG.info("It is working!!");

    }
}

The error: 错误:

ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'timeIntervalTriggerService' defined in file [/usr/local/apache-tomcat-7.0.54/webapps/fremad/WEB-INF/classes/fremad/service/TimeIntervalTriggerService.class]: 
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [fremad.service.TimeIntervalTriggerService]: 
Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
...
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [fremad.service.TimeIntervalTriggerService]: Constructor threw exception; nested exception is java.lang.NullPointerException

Context: 语境:

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


    <!-- Activates scanning of @Autowired -->
    <context:annotation-config/>

    <!-- Activates scanning of @Repository and @Service -->
    <context:component-scan base-package="fremad"/>

</beans>

The proces the @Autowired annotations a bean instance is required. 处理@Autowired注释需要一个bean实例。 The dependencies are set AFTER the execution of the constructor. 依赖项是在构造函数执行后设置的。 In your constructor the dependencies are still null . 在构造函数中,依存关系仍然为null

Instead of your constructor move the logic to a method annotated with @PostConstruct . 代替您的构造函数,将逻辑移到以@PostConstruct注释的方法。

@PostConstruct
public void init() {
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(updateTableTask,5000,updateFrequency);
}

However instead of doing this stuff yourself why not simply use Spring to schedule the bean for you... 但是,除了自己做这些事情之外,为什么不直接使用Spring为您安排bean ...

<task:scheduled-tasks>
    <task:scheduled ref="timerProcessor" method="doIt" fixed-delay="5000" initial-delay="1000"/>     
</task:scheduled-tasks>

Saves you some code. 为您节省一些代码。 Or simply slap a @Scheduled annotation on the TimerProcessor.doIt method and use <task:annotation-driven /> instead. 或者,只需在TimerProcessor.doIt方法上拍一个@Scheduled批注,然后使用<task:annotation-driven />

See the scheduling section of the reference guide. 请参阅参考指南的计划部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM