Spring环境搭建之:导入jar包、配置文件名称及放置位置

来源:互联网 发布:完整id查询软件 编辑:程序博客网 时间:2024/06/07 06:43

以Spring4为例,这里就不提供Spring的下载地址了,官网上很容易下的到。

1、导入相关jar包

建好web项目以后,导入Spring自己的jar包
spring-beans-4.0.0.M2.jar
spring-context-4.0.0.M2.jar
spring-core-4.0.0.M2.jar
spring-expression-4.0.0.M2.jar
spring-jdbc-4.0.0.M2.jar
spring-tx-4.0.0.M2.jar
spring-web-4.0.0.M2.jar
spring-webmvc-4.0.0.M2.jar
导入Spring所依赖的jar包
commons-logging-1.1.1.jar
导入文件上传下载组件的jar包
commons-fileupload-1.3.jar
commons-io-2.4.jar
导入数据源jar包,这里用的是apache的dbcp
commons-dbcp-1.4.jar
commons-pool-1.6.jar
Spring MVC中使用JstlView时需要添加的包
jstl-1.1.2.jar
standard-1.1.2.jar
数据库驱动包,这里用的是mysql
mysql-connector-java-5.1.7-bin.jar
……
其他项目所需的jar包,导入jar包可以先自己尝试,在项目的相关配置完成以后,尝试部署启动web服务器,根据服务器启动过程中的提示添加缺少的jar包就行,直到服务器正常启动。

2、配置文件的名称及放置位置

2.1默认名称及位置
Spring框架默认会加载WEB-INF/下的applicationContext.xml文件
2.2自定义名称及位置
当然我们可以更改配置文件的名称,也可以更改文件的放置位置
不过这样就需要在web.xml文件中配置相关信息,来告诉Spring框架去哪里加载配置文件。
Spring环境搭建之:导入jar包.配置文件名称及放置位置

在web.xml中配置ContextLoaderListenerContextLoaderServlet指定加载路径方式。
它们两个有着同样的功能,都实现在了org.springframework.web.context.ContextLoader类,
都要定义contextConfigLocation参数。区别在于listener不能在Servlet 2.2兼容的容器中使用。
自从Servelt 2.4规范,listener被要求在web应用启动后初始化。web.xml初始化的时候,
listerner会检查contextConfigLocation参数。如果不存在的话,它将默认使用
/WEB-INF/applicationContext.xml
如果它存在,它就会用预先定义的分隔符(逗号,分号和空格)分开分割字符串(<param-value></param-value),
并将这些值作为应用上下文将要搜索的位置。

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/conf/applicationContext.xml</param-value></context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

org.springframework.web.context.ContextLoaderListener在初始化时会通过org.springframework.web.context.support.XmlWebApplicationContext初始化ApplicationContext,初始化时,先检查从ContextLoader中继承来的属性CONFIG_LOCATION_PRRAM有没有对应的初始化参数

//ContextLoader.java中的属性public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

通过servletContext.getInitPrame(CONFIG_LOCATION_PRRAM);如果没有就加载

//XmlWebApplicationContext.java中的属性public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
0 0