java listener和Spring注入问题 工程需要每天在固定时间进行扫描入库,所以制定了listener做监听,当数据扫描成功后,入库的时候因为dao使用的Spring注入,这个dao始终无法得到,这个问题请问该如何解决。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 spring配置好了吗 配置文件贴出来看看 监听器代码如下: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还没有完成加载。 你这个监听没有起到任何作用,还是先让sping注入后自己写写实现一个定时器吧,想要定时执行,你一个servlet有什么用? 这个定时器是正常的,ServletContextListener本事就是启监听的作用,在监听器中的两个方法一个是启用方法,一个是消亡方法,当然web.xml文件我没有贴出来。我的问题主要集中在dao的注入。上面我也已经说明了情况。 你看看 你的dao 注入的 问题,DAO注入的 是它的实现类 你看看你的 好好看看 这个dao注入是正常的,如果不是用监听器调用,其他使用是OK的。再做一下声明,dao的注入是没有问题的,监听器也是可正常使用的。现在就是先后顺序的问题。 同样的问题,service中注入的dao为空,估计这个时候spring还没有注入,寻找解决方法 只有在你的listener中自己注入ApplicationContext ac = new ClassPathXmlApplicationContext("../springConfig/applicationContext-*.xml");从ApplicationContext中获取bean然后使用。 Failed to read input解决方案 请教NumberFormat中的百分数问题getPercentInstance struts2初始化页面时显示数据? Hibernate递归中事务问题 运行Login.java输入正确ID和密码,仍提示错误,不能登陆. java正则表达式怎么无法匹配$? 如何实现如下的时间计算??? 实体BEAN的关系问题! 『简单问题』请教EJB JNDI的问题 weblogic +linux 配置问题 myeclipse10.0 struts2 表单xml验证失败后其他界面手工也进入不了?
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还没有完成加载。
ApplicationContext ac = new ClassPathXmlApplicationContext("../springConfig/applicationContext-*.xml");从ApplicationContext中获取bean然后使用。