Spring+SprinMVC配置学习总结
2016-03-23 10:54
639 查看
一千个人有一千种spring的配置方式,真是这样。看了好多的配置,试验了很多。这里做一个总结。
1 原理上,spring和springmvc可以合并为一个配置文件然后在web.xml中加载,因为最终的形式都是bean。但最好分开配置配置spring,然后配置springmvc,但测试发现,两者调换顺序配置也没什么问题。呃呃。
2 Spring的配置文件命名一般为:applicationContext.xml,springmvc的配置文件为一般为:xxx-servlet.xml,因为springmvc在web.xml中以一个servlet配置。数据库的配置文件一般命名:jdbc.properties。
3 数据库的配置最好和spring的配置分开,单独列成jdbc.properties文件。在spring配置文件applicationContext.xml中加载时,用最简便的方式,下面的写法需要注意路径:jdbc.properties在src目录下可以:
<context:property-placeholderlocation="classpath*:jdbc.properties"/>
接上面,因为classpath就是代表 /WEB-INF /classes/这个路径,而上面的例子jdbc.properties发布时的路径在这个目录下。关于配置文件路径的问题,理解了classpath的代表,就理解了两种写法,例子如下:
如上图,在web.xml中配置时config下的applicationContext.xml时:
<param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
<param-value>classpath:config/applicationContext.xml</param-value>
上面这两种写法等价。而这时候,applicationContext.xml中加载jdbc.properties应该这么写:
<context:property-placeholderlocation="classpath:config/jdbc.properties"/>
classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找
4 Spring在web.xml中有两种配置方式:listener和servlet方式。查阅得知:spring3.0以后 不支持servlet方式。所以推荐用listener方式。
本文最先发表在csdn博客上,现在签了过来。
1 原理上,spring和springmvc可以合并为一个配置文件然后在web.xml中加载,因为最终的形式都是bean。但最好分开配置配置spring,然后配置springmvc,但测试发现,两者调换顺序配置也没什么问题。呃呃。
2 Spring的配置文件命名一般为:applicationContext.xml,springmvc的配置文件为一般为:xxx-servlet.xml,因为springmvc在web.xml中以一个servlet配置。数据库的配置文件一般命名:jdbc.properties。
3 数据库的配置最好和spring的配置分开,单独列成jdbc.properties文件。在spring配置文件applicationContext.xml中加载时,用最简便的方式,下面的写法需要注意路径:jdbc.properties在src目录下可以:
<context:property-placeholderlocation="classpath*:jdbc.properties"/>
接上面,因为classpath就是代表 /WEB-INF /classes/这个路径,而上面的例子jdbc.properties发布时的路径在这个目录下。关于配置文件路径的问题,理解了classpath的代表,就理解了两种写法,例子如下:
如上图,在web.xml中配置时config下的applicationContext.xml时:
<param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
<param-value>classpath:config/applicationContext.xml</param-value>
上面这两种写法等价。而这时候,applicationContext.xml中加载jdbc.properties应该这么写:
<context:property-placeholderlocation="classpath:config/jdbc.properties"/>
classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找
4 Spring在web.xml中有两种配置方式:listener和servlet方式。查阅得知:spring3.0以后 不支持servlet方式。所以推荐用listener方式。
本文最先发表在csdn博客上,现在签了过来。
相关文章推荐
- [2016/03/23] 2016年蓝桥杯JAVA A组 参赛总结和感想
- struts.xml中Action的method与路径的几种匹配模式。
- SpringMVC结合ajaxfileupload.js实现文件无刷新上传
- Spring Security(17)——基于方法的权限控制
- java中关于log日志
- Spring3 MVC请求参数获取的几种方法
- 【技术文档】Jeecg-P3开发环境搭建入门(java插件开发框架)
- Spring IoC Service Provicer 管理对象间的依赖关系的三种方式
- SpringMVC中使用Interceptor拦截器
- Spring Security(16)——基于表达式的权限控制
- SpringMVC上传文件以流方式判断类型附常用类型
- Eclipse : An error occurred while filtering resources(Maven错误提示)
- Spring的控制反转的三种方式
- Spring Security(15)——权限鉴定结构
- clone() and the Cloneable Interface in Java
- Java删除文件夹和文件
- Java单例模式解析
- java实现两个有序数组的合并
- JavaWeb项目遇到的一些小问题(一)
- java事件处理机制(自定义事件)