gpt4 book ai didi

java - 使用 Spring Task Scheduler 时无法 Autowiring DAO

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:31 24 4
gpt4 key购买 nike

<分区>

如果我调用 notifyStaff 方法,这会起作用,但如果我使用 Scheduled 注释并将该方法作为计划任务运行,我将无法实例化 userDAO。有解决办法吗?

@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
}
}

编辑:

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() {
// This assumes that the table name is the same as the simple name of the class
return currentSession().createCriteria(getType())
.list();
}
}

UserDAO

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]

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com