一千个人有一千种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的代表,就理解了两种写法,例子如下:

 

Spring+SprinMVC配置学习总结

如上图,在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博客上,现在签了过来。

相关文章:

  • 2021-05-24
  • 2021-08-21
  • 2021-12-18
  • 2021-07-06
  • 2021-06-26
  • 2021-09-24
  • 2021-09-11
猜你喜欢
  • 2022-01-03
  • 2021-08-09
  • 2021-06-17
  • 2021-12-21
  • 2021-11-19
  • 2021-07-16
  • 2021-12-10
相关资源
相似解决方案