3

this works if I call the notifyStaff method but I'm unable to instantiate userDAO if I use the Scheduled annotation and run the method as a scheduled task. Is there a work around for this?

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

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() {
        // 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();
}

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
    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]
Roman C
  • 49,761
  • 33
  • 66
  • 176
user771912
  • 401
  • 1
  • 4
  • 16

2 Answers2

1

You have to let Spring know of your @Autowired beans so it can register them.

add to your context file:

<context:component-scan base-package="path.to.the.FollowUp" />
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • that is already included in the app-servlet.xml file. – user771912 Nov 13 '14 at 18:21
  • this sounds like the webapp context. Don't forget that in a webapp there are 2 Spring contexts, the web one (the one you mentioning) and the root (the one you present above). Since you define the FollowUp @Service bean under the root context you have to have the component-scan in the same context config file. – dimitrisli Nov 13 '14 at 18:23
  • you're right! it is as you said. thought i was going insane – user771912 Nov 13 '14 at 18:27
0

These two lines you are probably missing in your xml file.

    <context:component-scan base-package="your base path*" />

    <context:annotation-config />
dReAmEr
  • 6,986
  • 7
  • 36
  • 63