I'm having some problems with annotations and the timertask class. I'm trying to add the autowired bean into the schedule, but I get the error: NullPointerException. Why? I'm quite new to Spring and would be happy if someone could give me a hint. 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
@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. The dependencies are set AFTER the execution of the constructor. In your constructor the dependencies are stillnull
.Instead of your constructor move the logic to a method annotated with
@PostConstruct
.However instead of doing this stuff yourself why not simply use Spring to schedule the bean for you...
Saves you some code. Or simply slap a
@Scheduled
annotation on theTimerProcessor.doIt
method and use<task:annotation-driven />
instead.See the scheduling section of the reference guide.