工程需要每天在固定时间进行扫描入库,所以制定了listener做监听,当数据扫描成功后,入库的时候因为dao使用的Spring注入,这个dao始终无法得到,这个问题请问该如何解决。

解决方案 »

  1.   

    spring配置好了吗 配置文件贴出来看看
      

  2.   

    监听器代码如下:
    public class SysListener extends HttpServlet implements ServletContextListener {
     public void contextInitialized(ServletContextEvent sce) {
          OrgDataService dataService = new OrgDataService();
          dataService.initCollectionData();
    }
     public void contextDestroyed(ServletContextEvent sce) {}
    }
    调用类如下:
    public class OrgDataService {
           Logger logger = Logger.getLogger(OrgDataService.class);

    SimpleDateFormat dateFormat = new SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss");

    CollectionDao collectionDao;

    public void setCollectionDao(CollectionDao collectionDao) {
    this.collectionDao = collectionDao;
    }
            public void initCollectionData() {
                    String infoSql = "************************";
                    collectionDao.excuteSql(infoSql);
            }
    }
    spring加载文件如下:
    <bean id="dataService" class="com.warefont.itsmportal.dashbord.web.collection.service.OrgDataService">
    <property name="collectionDao" ref="collectionDao"/>
    </bean>
    加载时报空指针,主要是因为这个监听器是在工程启动的时候开始监听的,当监听器的初始化方法调用OrgDataService 对象使用到dao时,这个时候的spring还没有完成加载。
      

  3.   

    你这个监听没有起到任何作用,还是先让sping注入后自己写写实现一个定时器吧,想要定时执行,你一个servlet有什么用?
      

  4.   

    这个定时器是正常的,ServletContextListener本事就是启监听的作用,在监听器中的两个方法一个是启用方法,一个是消亡方法,当然web.xml文件我没有贴出来。我的问题主要集中在dao的注入。上面我也已经说明了情况。
      

  5.   

    你看看 你的dao 注入的 问题,DAO注入的 是它的实现类 你看看你的 好好看看
      

  6.   

    这个dao注入是正常的,如果不是用监听器调用,其他使用是OK的。再做一下声明,dao的注入是没有问题的,监听器也是可正常使用的。现在就是先后顺序的问题。
      

  7.   

    同样的问题,service中注入的dao为空,估计这个时候spring还没有注入,寻找解决方法
      

  8.   

    只有在你的listener中自己注入
    ApplicationContext  ac = new ClassPathXmlApplicationContext("../springConfig/applicationContext-*.xml");从ApplicationContext中获取bean然后使用。