由于监听器在执行的时候,框架的配置文件都没有被扫描,所以所有的对象都没有被创建出来,因此,一定会报空指针异常,解决方法很简单就是我们手动跟他说帮我们把所有的对象都创建好就可
加上创建容器的语句,然后从容器中拿出对象就可:
ApplicationContext ac = new ClassPathXmlApplicationContext(“spring/applicationContext.xml”);
DitcServiceImpl ditcService = (DitcServiceImpl)ac.getBean(“ditcServiceImpl”);
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("上下文作用域对象来了");
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
DitcServiceImpl ditcService = (DitcServiceImpl)ac.getBean("ditcServiceImpl");
System.out.println(ditcService+"=================");
Map map = ditcService.createDitcMap();
servletContextEvent.getServletContext().setAttribute("dic",map);
System.out.println("上下文作用域对象完成任务啦");
}