spring 引入多个properties
PropertyPlaceholderConfigurer
Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spring配置期间替换使用属性值。
【一】配置惟一PropertyPlaceholderConfigurer。
1、配置单个properties文件,使用location属性
"PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> "fileEncoding" value="UTF-8"> "location"> classpath:/system/properties/database.properties
2、配置单个properties文件,使用locations属性
"PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> "fileEncoding" value="UTF-8"> "location">
classpath:/system/properties/database.properties /WEB-INF/config/system/serviceConfig.properties
【二】配置多个PropertyPlaceholderConfigurer。
propertyPlaceholderConfigurer01
"propertyPlaceholderConfigurer01" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> "order" value="1" /> "ignoreUnresolvablePlaceholders" value="true" /> "fileEncoding" value="UTF-8"> "location">
classpath:/system/properties/database01.properties /WEB-INF/config/system/serviceConfig01.properties
propertyPlaceholderConfigurer02
"propertyPlaceholderConfigurer02" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> "order" value="2" /> "ignoreUnresolvablePlaceholders" value="true" /> "fileEncoding" value="UTF-8"> "location">
classpath:/system/properties/database02.properties /WEB-INF/config/system/serviceConfig02.properties
参数说明:
1、order :属性代表其加载顺序;
2、ignoreUnresolvablePlaceholders :是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true;
扩展补充:
PropertyOverrideConfigurer与PropertyPlaceholderConfigurer 不同的是:
PropertyOverrideConfigurer 利用属性文件的相关信息,覆盖XML 配置文件中定义。即PropertyOverrideConfigurer 允许XML 配置文件中有默认的配置信息。
如果PropertyOverrideConfigurer 的属性文件有对应配置信息,则XML 文件中的配
置信息被覆盖:否则,直接使用XML 文件中的配置信息。使用PropertyOverrideConfigurer
属性文件的格式如下:
beanName(beanId).property=value
beanName 是属性占位符企图覆盖的bean 名, property 是企图覆盖的属性名
————————————————
原文链接:https://blog.csdn.net/mahoking/article/details/104553737
备注:
多个properties引入的时候相同的beanid只能是唯一的,可以修改为不同的