spring与web整合(交鸡肋,因为有前台框架封装了servlet)

1、 新建web项目 spring3_day1_web 导入jar和配置文件

2、 编写HelloServlet 、HelloService (交给Spring管理 )

3、 发现问题,每次访问Servlet都会创建新的Spring容器对象

一个项目中 spring容器只需要一个就可以了 !如何只有一个!

web项目中:确保对象唯一, 放入ServletContext 范围 !

项目启动时,放入spring容器对象 ------  ServletContextListener 对象

 

4、Spring框架内容为我们提供了这样的Listener ,位于spring-web开发包 !

       导入 spring-web-3.2.0.RELEASE.jar

配置Listener 在web.xml 

错误:Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

默认会去WEB-INF找配置文件

这里classpath: 必须小写,相当于WEB-INF/classes 

5、Servlet 从ServletContext 范围获取对象

posted @ 2015-11-01 19:59  自相矛盾  阅读(463)  评论(0编辑  收藏  举报