自动装配和时间任务的问题

时间:2014-11-13 12:43:28

标签: java spring annotations timertask

我在注释和timertask类方面遇到了一些问题。我正在尝试将自动装配的bean添加到计划中,但是我收到错误:NullPointerException。为什么?我对Spring很陌生,如果有人能给我一个提示,我会很高兴。请问我是否可以为您提供更多信息。

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

这是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!!");

    }
}

错误:

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

上下文:

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

1 个答案:

答案 0 :(得分:1)

需要bean实例的@Autowired注释的过程。在执行构造函数之后设置依赖项。在构造函数中,依赖项仍为null

而不是构造函数将逻辑移动到使用@PostConstruct注释的方法。

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

然而,为什么不简单地使用Spring来为你安排bean,而不是自己做这些东西......

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

保存一些代码。或者只是在@Scheduled方法上拍一个TimerProcessor.doIt注释,然后使用<task:annotation-driven />

请参阅参考指南的计划部分。