如果我调用notifyStaff方法,但是如果我使用userDAO
注释并将该方法作为计划任务运行,则无法实例化Scheduled
。有没有解决这个问题?
@Service
public class FollowUp {
@Autowired
private UserDAO userDAO;
@Scheduled(fixedDelay=5000)
public void notifyStaff() {
List<User> users = userDAO.findAll();
// do stuff with list of users
}
}
<?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>
public class UserDAOHibernateImpl extends
GenericDAOHibernateImpl<User> implements UserDAO {
public UserDAOHibernateImpl() {
super(User.class);
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
// This assumes that the table name is the same as the simple name of the class
return currentSession().createCriteria(getType())
.list();
}
}
public interface UserDAO extends GenericDomainDAO<User> {
List<T> findAll();
}
12:08:31.452 [myScheduler-1] ERROR org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
java.lang.NullPointerException: null
at com.example.department.appname.mvc.utility.FollowUp.notifyStaff(FollowUp.java:44) ~[FollowUp.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_11]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_11]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_11]
at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_11]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64) ~[spring-context-3.1.0.RC1.jar:3.1.0.RC1]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53) ~[spring-context-3.1.0.RC1.jar:3.1.0.RC1]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [na:1.7.0_11]
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351) [na:1.7.0_11]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178) [na:1.7.0_11]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) [na:1.7.0_11]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.7.0_11]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_11]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_11]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_11]
答案 0 :(得分:1)
您必须让Spring
知道您的@Autowired
bean,以便注册它们。
添加到您的上下文文件中:
<context:component-scan base-package="path.to.the.FollowUp" />
答案 1 :(得分:0)
xml文件中可能缺少这两行。
<context:component-scan base-package="your base path*" />
<context:annotation-config />