주석 및 timertask 클래스에 문제가 있습니다. autowired 빈을 일정에 추가하려고하는데 오류가 발생합니다 : NullPointerException. 왜? 나는 봄에 대해 아주 새로운 사람이 나에게 힌트를 줄 수 있다면 행복 할 것이다. 더 자세한 정보를 제공해 줄 수 있는지 물어보십시오.autowired 및 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 예외에 따르면 TimeIntervalTriggerService 생성자에서 NullPointerException이 발생합니다. – Maksym
음, 그렇습니다. 어쩌면 내가 그 질문에 언급 했어야했는데. –
그리고이 코드에서 문제가있다. 이런 식으로 싱글 톤 스코프가있는 bean에 요청 스코프가있는 빈을 autowire 할 수 없다. 이런 식으로 싱글 톤 빈을 가질 것이다. – Maksym