У меня возникли проблемы с аннотациями и классом timertask. Я пытаюсь добавить в расписание автообученный bean-компонент, но я получаю ошибку: NullPointerException. Зачем? Я довольно новичок в Spring и был бы счастлив, если бы кто-нибудь мог дать мне подсказку. Пожалуйста, спросите, могу ли я предоставить вам дополнительную информацию.Проблемы с autwired и timertask
@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>
Согласно исключению Spring, у вас есть NullPointerException в конструкторе TimeIntervalTriggerService. – Maksym
Ну, да. Может быть, я должен был сказать это в вопросе. –
И у вас есть проблема в этом коде, вы не можете autowire bean с областью запроса в bean с singleton scope таким образом, таким образом вы будете иметь только одноэлементные бобы. – Maksym