spring配置中,properties文件以及xml文件配置问题
spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂!
这里说下配置文件properties管理的问题。
一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件里面,项目功能复杂的时候,往往properties文件很多,这时,就比较容易让人困惑,有些properties的文件内容总是加载不起来,应用启动时,就不断爆出错误,说某某参数加载失败,这个是什么原因呢?
其实,这个是spring配置的时候,org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer这个bean只会被加载一次。在spring的xml配置文件中,多处出现这个bean的配置,spring加载的时候,只会加载第一个,后面的就加载不了。
如何解决呢?其实很简单,比如我的一个应用中,就有internal.properties和jdbc.properties两个文件。按下面这种方式就可以解决:
<bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:conf/internal.properties</value>
<value>classpath:conf/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configRealm"/>
</bean>
若有多个,可以在list中类似红色部分添加就可以了!
在这里,再多说一点,xml的配置问题,当然,不是功能问题,而是习惯问题!
大的基于spring的项目,往往有很多xml配置文件,比如DAO的,比如权限shiro的配置,比如redis的配置等等,在web.xml中的context-param字段,可以如下配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-dao.xml,classpath:conf/spring-servlet.xml</param-value>
</context-param>
但是,更好的办法,是将一些应用模块的xml配置通过import resource的方式放在一个xml文件中,例如applicationContext.xml。比如,我的应用applicationContext.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:conf/internal.properties</value>
<value>classpath:conf/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configRealm"/>
</bean> <import resource="applicationContext-springdata.xml"/>
<import resource="applicationContext-security.xml"/>
<import resource="applicationContext-ehcache.xml"/>
<import resource="applicationContext-task.xml"/>
<import resource="applicationContext-external.xml"/>
<import resource="applicationContext-message.xml"/> -->
</beans>
这个applicationContext.xml可以在web.xml中的context-param配置部分加载,如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
这样子,就会使得项目的配置文件结构非常的清晰!
spring配置中,properties文件以及xml文件配置问题的更多相关文章
- Spring中加载ApplicationContext.xml文件的方式
Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...
- 配置ssm 时, web.xml 文件无 # 自动代码提示
环境:STS 版本:spring-tool-suite-3.8.1.RELEASE-e4.6-win32-x86_64 配置ssm 时, web.xml 文件无 如下图蓝色圈范围内的提示 问题与 链接 ...
- java中采用dom4j解析xml文件
一.前言 在最近的开发中用到了dom4j来解析xml文件,以前听说过来解析xml文件的几种标准方式:但是从来的没有应用过来,所以可以在google中搜索dmo4j解析xml文件的方式,学习一下dom4 ...
- 在C#中创建和读取XML文件
1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...
- C#程序中:如何删除xml文件中的节点、元素。
C#中动态的清理xml文件中的垃圾信息是程序员必会的哦.这就像数据库一样,不会清理数据怎么可以呢?其实xml文件就可以用作一个小的数据库,存储一些简单的信息.所以,用C#程序实现xml文件的增.删.改 ...
- selector是在文件夹drawable中进行定义的xml文件。
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
- 在java代码中,用xslt处理xml文件
http://blog.csdn.net/zhou_lei/article/details/2661735 ********************************************** ...
- Spring框架找不到 applicationContext.xml文件,可能是由于applicationContext.xml文件的路径没有放在根目录下造成的
Spring框架找不到 applicationContext.xml文件,可能是由于applicationContext.xml文件的路径没有放在根目录下造成的
- VC 使用msxml6.dll动态链接库中的函数读写XML文件
VC 使用msxml6.dll动态链接库中的函数读写XML文件 目录 1 引言 2 .dll使用方法 3 常用函数总结 4 实例应用 5 运行效果预览 6 补充说明 7 不足之处 8 更新 引言: ...
- selector是在文件夹drawable中进行定义的xml文件转载 https://www.cnblogs.com/fx2008/p/3157040.html
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
随机推荐
- [转]慎用slice
在go中,slice是对固定长度数组的一段切片,其底层是用对数值空间的指针实现的. 在slice的赋值过程中,slice的容量会被初始化成“数组长度 - slice的起点位置”,举例说明: 假设有长度 ...
- 【转】Paxos算法深入分析
http://blog.csdn.net/anderscloud/article/details/7175209 在分布式系统设计领域,Paxos可谓是最重要一致性的算法.Google的大牛们称 ...
- caffe编译关于imread问题的解决
change Makefile: LIBRARIES += glog gflags protobuf leveldb snappy \ lmdb boost_system hdf5_hl hdf5 m ...
- JAVA存取PG大对象类型OID数据
转载地址:http://my.oschina.net/liuyuanyuangogo/blog/151537 pg用大对象存储二进制数据的老文档:http://jdbc.postgresql.org/ ...
- yii2构造方法
控制器文件下面建 一公共控制器:: CommonController.php 里面写一个空方法 : public function init(){} 然后在你要写构造函数的控制器类里写一个方法 ( ...
- 用Python对excel文件的简单操作
#-*-coding:utf8-*- import xlrd #代开excel文件读取数据 data = xlrd.open_workbook("C:\\Users\\hyl\\Deskto ...
- Set-常用API及详解
SetAPI: 类别 方法 增 add.addAll 删 remove.removeAll 长 size 遍 iterator 判 isEmpty hashCode 使用与集合大同小异,主要的区别就在 ...
- Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- sublime text3 本地化
博客園中搜到n篇 同題日誌 沒一篇靠譜 超級反感這種行為,浪費別人的時間無異於謀財害命 自動: Control+` (注意不是cmd+p) import urllib.request,os,hashl ...
- URAL 2034 Caravans(变态最短路)
Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...