java - Unable to Autowire a DAO when using Spring Task Scheduler -
this question has reply here:
understanding spring @autowired usage 4 answersthis works if phone call notifystaff method i'm unable instantiate userdao if utilize scheduled
annotation , run method scheduled task. there work around this?
@service public class followup { @autowired private userdao userdao; @scheduled(fixeddelay=5000) public void notifystaff() { list<user> users = userdao.findall(); // stuff list of users } }
edits: applicationcontext-business.xml <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- dao layer generic config--> <bean id="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.genericdaohibernateimpl" abstract="true"> <property name="sessionfactory" ref="sessionfactory"/> </bean> <!-- dao layer instances --> <bean id="divisiondao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.divisiondaohibernateimpl" /> <bean id="modalitydao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.modalitydaohibernateimpl" /> <bean id="scoredao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.scoredaohibernateimpl" /> <bean id="userdao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.userdaohibernateimpl" /> <bean id="reviewdao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.reviewdaohibernateimpl" /> <bean id="qacasedao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.qacasedaohibernateimpl" /> <bean id="examdao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.examdaohibernateimpl" /> <bean id="patientdao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.patientdaohibernateimpl" /> <bean id="roledao" parent="abstractdaotarget" class="com.example.department.appname.persistence.hibernate.roledaohibernateimpl" /> </beans>
userdaohibernateimpl public class userdaohibernateimpl extends genericdaohibernateimpl<user> implements userdao { public userdaohibernateimpl() { super(user.class); } @suppresswarnings("unchecked") public list<t> findall() { // assumes table name same simple name of class homecoming currentsession().createcriteria(gettype()) .list(); } }
userdao public interface userdao extends genericdomaindao<user> { list<t> findall(); }
error message 12:08:31.452 [myscheduler-1] error org.springframework.scheduling.support.taskutils$loggingerrorhandler - unexpected error occurred in scheduled task. java.lang.nullpointerexception: null @ com.example.department.appname.mvc.utility.followup.notifystaff(followup.java:44) ~[followup.class:na] @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.7.0_11] @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) ~[na:1.7.0_11] @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:1.7.0_11] @ java.lang.reflect.method.invoke(method.java:601) ~[na:1.7.0_11] @ org.springframework.scheduling.support.scheduledmethodrunnable.run(scheduledmethodrunnable.java:64) ~[spring-context-3.1.0.rc1.jar:3.1.0.rc1] @ org.springframework.scheduling.support.delegatingerrorhandlingrunnable.run(delegatingerrorhandlingrunnable.java:53) ~[spring-context-3.1.0.rc1.jar:3.1.0.rc1] @ java.util.concurrent.executors$runnableadapter.call(executors.java:471) [na:1.7.0_11] @ java.util.concurrent.futuretask$sync.innerrunandreset(futuretask.java:351) [na:1.7.0_11] @ java.util.concurrent.futuretask.runandreset(futuretask.java:178) [na:1.7.0_11] @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$301(scheduledthreadpoolexecutor.java:178) [na:1.7.0_11] @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(scheduledthreadpoolexecutor.java:293) [na:1.7.0_11] @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1110) [na:1.7.0_11] @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:603) [na:1.7.0_11] @ java.lang.thread.run(thread.java:722) [na:1.7.0_11]
you have allow spring
know of @autowired
beans can register them.
add context file:
<context:component-scan base-package="path.to.the.followup" />
java spring scheduled-tasks
No comments:
Post a Comment