Spring在web开发中的应用

(1)在 web 项目中要使用 spring 需要导入一个 jar 包: 
spring-web-4.2.4.jar包 
(2)在 web.xml 文件中配置 Listener

1 <listener>
2     <listener-class>
3     org.springframework.web.context.ContextLoaderListener
4     </listener-class>
5 </listener>

这个 ContextLoaderListener 它实现了 ServletContextListener.在这个 listener 中,当服务器启动时,将 ApplicationContext 对象,其实是它的一个实现类WebApplicationContext,对象存入到了 ServletContext 中。 
(3)我们还需要在 web.xml 文件中配置 applicationContext.xml 文件的位置,默认情况下会在 WEB-INF 目录 下查找 applicationContext.xml 
如果 applicationContext.xml 文件不在默认位置,我们可以在 web.xml 文件中配置。

1 <context-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:applicationContext.xml</param-value>
4 </context-param>

Classpath:applicationContext.xml 它代表的是在当前工程的类路径下(可以理解成是在 src)下来查找 applicationContext.xml 文件。 
contextConfigLocation 它是在 listener 中声明的一个常量,描述的就是 spring 配置文件的位置。 
经过上述配置后,在web开发中就可以使用spring框架了。

posted @ 2017-12-25 21:04  喜东东  阅读(160)  评论(0编辑  收藏  举报