Spring在javaweb工程中的简单配置与应用

来源:互联网 发布:水刀编程软件 编辑:程序博客网 时间:2024/04/30 22:02

一、导入spring与web相关的jar包。这里注意由于我之前起的web项目,然后是通过java的application做的哦实验,因此所有的jar包都是导入在了src的library中。在运行web的时候会出现各种class找不到,因此还需要将jar包导入到/WebContent/WEB-INF/lib中。

二、在web.xml中添加如下关于spring的代码:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext-*.xml</param-value>   </context-param>   <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener> 
这里classpath:applicationContext-*.xml表示spring的配置文件从src文件下搜索以applicationContext-*开头的文件。

三、servlet中如果需要spring提供的bean的话,如下使用:

        ServletContext servletContext = this.getServletContext();               WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);       UserDao userDao = (UserDao)ctx.getBean("mysqlDaoImpl");

0 0
原创粉丝点击