2014-11-13 5 views
0

주석 및 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> 
+0

Spring 예외에 따르면 TimeIntervalTriggerService 생성자에서 NullPointerException이 발생합니다. – Maksym

+0

음, 그렇습니다. 어쩌면 내가 그 질문에 언급 했어야했는데. –

+0

그리고이 코드에서 문제가있다. 이런 식으로 싱글 톤 스코프가있는 bean에 요청 스코프가있는 빈을 autowire 할 수 없다. 이런 식으로 싱글 톤 빈을 가질 것이다. – Maksym

답변

1

사전 처리 @Autowired는 빈 인스턴스가 필요 주석에. 의존성은 생성자의 실행 후에 설정됩니다. 생성자에서 종속성은 여전히 ​​null입니다.

생성자 대신 로직을 @PostConstruct으로 주석 된 메소드로 이동하십시오.

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

그러나 대신에 단순히 당신을 위해 콩을 예약 스프링을 사용하지 왜이 물건 자신을 일을 ...

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

당신에게 몇 가지 코드를 저장합니다. 또는 TimerProcessor.doIt 메소드에서 @Scheduled 주석을 쳐서 <task:annotation-driven />을 대신 사용하십시오.

참조 가이드의 일정 섹션을 참조하십시오.

+0

정말 고마워요! –