版权声明:本文为博主原创文章,未经博主允许不得转载。
转载请说明出处:http://blog.csdn.NET/chinadim/article/details/40621671
最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,
网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式
1.通过context:property-placeholde实现配置文件加载
1.1、在spring.xml中加入context相关引用
1.2、引入jdbc配置文件
1.3、jdbc.properties的配置如下
1.4、在spring-mybatis.xml中引用jdbc中的配置
1.5、在Java类中引用jdbc.properties中的配置
1.6、在controller中调用
1.7、测试
在ie中输入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2
返回如下结果:
注:通过context:property-placeholde加载多个配置文件
只需在第1.2步中将多个配置文件以逗号分隔即可
2、通过util:properties实现配置文件加载
2.1、在spring.xml中加入util相关引用
2.2、 引入config配置文件
2.3、config.properties的配置如下
2.4、在java类中引用config中的配置
2.5、在controller中调用
2.6、测试
在ie中输入http://localhost:8080/testWeb/service2/test
返回如下结果:
3.直接在Java类中通过注解实现配置文件加载
3.1、在java类中引入配置文件
3.2、在controller中调用 }3.3、测试
在ie中输入http://localhost:8080/testWeb/service2/test
返回如下结果:
最后附上spring.xml的完整配置: