一个系统中通常会存在如下一些以Properties形式存在的配置文件

1.数据库配置文件demo-db.properties:
database.url=jdbc:mysql://localhost/smaple database.driver=com.mysql.jdbc.Driver database.user=root database.password=123
2.消息服务配置文件demo-mq.properties:
#congfig of ActiveMQ mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000 mq.java.naming.security.principal= mq.java.naming.security.credentials= jms.MailNotifyQueue.consumer=5
3.远程调用的配置文件demo-remote.properties:
remote.ip=localhost remote.port=16800 remote.serviceName=test
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 将多个配置文件读取到容器中,交给Spring管理 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 这里支持多种寻址方式:classpath和file -->
<value>classpath:/opt/demo/config/demo-db.properties</value>
<!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
<value>file:/opt/demo/config/demo-mq.properties</value>
<value>file:/opt/demo/config/demo-remote.properties</value>
</list>
</property>
</bean>
<!-- 使用MQ中的配置 -->
<bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
<prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
<prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
<prop key="userName">${mq.java.naming.security.principal}</prop>
<prop key="password">${mq.java.naming.security.credentials}</prop>
</props>
</property>
</bean>
</beans>
我们也可以将配置中的List抽取出来:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 将多个配置文件位置放到列表中 -->
<bean id="propertyResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<!-- 这里支持多种寻址方式:classpath和file -->
<value>classpath:/opt/demo/config/demo-db.properties</value>
<!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
<value>file:/opt/demo/config/demo-mq.properties</value>
<value>file:/opt/demo/config/demo-remote.properties</value>
</list>
</constructor-arg>
</bean>
<!-- 将配置文件读取到容器中,交给Spring管理 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="propertyResources" />
</bean>
<!-- 使用MQ中的配置 -->
<bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
<prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
<prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
<prop key="userName">${mq.java.naming.security.principal}</prop>
<prop key="password">${mq.java.naming.security.credentials}</prop>
</props>
</property>
</bean>
</beans>
二、整合多工程下的多个分散的Properties
应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。
配置如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 将DB属性配置文件位置放到列表中 -->
<bean id="dbResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>file:/opt/demo/config/demo-db.properties</value>
</list>
</constructor-arg>
</bean>
<!-- 将MQ属性配置文件位置放到列表中 -->
<bean id="mqResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>file:/opt/demo/config/demo-mq.properties</value>
</list>
</constructor-arg>
</bean>
<!-- 用Spring加载和管理DB属性配置文件 -->
<bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations" ref="dbResources" />
</bean>
<!-- 用Spring加载和管理MQ属性配置文件 -->
<bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations" ref="mqResources" />
</bean>
<!-- 使用DB中的配置属性 -->
<bean id="rmsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${demo.db.driver}" p:url="${demo.db.url}" p:username="${demo.db.username}"
p:password="${demo.db.password}" pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"
p:poolPreparedStatements="true" p:defaultAutoCommit="false">
</bean>
<!-- 使用MQ中的配置 -->
<bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
<prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
<prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
<prop key="userName">${mq.java.naming.security.principal}</prop>
<prop key="password">${mq.java.naming.security.credentials}</prop>
</props>
</property>
</bean>
</beans>
注意:其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。这里一定需要按照这种方式设置这两个参数。
三、Bean中直接注入Properties配置文件中的值
应用场景:Bean中需要直接注入Properties配置文件中的值 。例如下面的代码中需要获取上述demo-remote.properties中的值:
public class Client() {
private String ip;
private String port;
private String service;
}
配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="<a href="http://www.springframework.org/schema/beans" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" rel="external nofollow" >http://www.w3.org/2001/XMLSchema-instance</a>" xmlns:util="<a href="http://www.springframework.org/schema/util" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a>" xsi:schemaLocation=" <a href="http://www.springframework.org/schema/beans" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" rel="external nofollow" >http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a> <a href="http://www.springframework.org/schema/util" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a> <a href="http://www.springframework.org/schema/util/spring-util-3.0.xsd" rel="external nofollow" >http://www.springframework.org/schema/util/spring-util-3.0.xsd</a>"> <!-- 这种加载方式可以在代码中通过@Value注解进行注入, 可以将配置整体赋给Properties类型的类变量,也可以取出其中的一项赋值给String类型的类变量 --> <!-- <util:properties/> 标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签 --> <util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" /> <!-- <util:properties/> 标签的实现类是PropertiesFactoryBean, 直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的 --> <bean id="settings" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>file:/opt/rms/config/rms-mq.properties</value> <value>file:/opt/rms/config/rms-env.properties</value> </list> </property> </bean> </beans>
Client类中使用Annotation如下:
import org.springframework.beans.factory.annotation.Value;
public class Client() {
@Value("#{remoteSettings['remote.ip']}")
private String ip;
@Value("#{remoteSettings['remote.port']}")
private String port;
@Value("#{remoteSettings['remote.serviceName']}")
private String service;
}
四、Bean中存在Properties类型的类变量
应用场景:当Bean中存在Properties类型的类变量需要以注入的方式初始化
1. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
public class Client() {
@Value("#{remoteSettings}")
private Properties remoteSettings;
}
2. 配置方式:也可以使用xml中声明Bean并且注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 可以使用如下的方式声明Properties类型的FactoryBean来加载配置文件,这种方式就只能当做Properties属性注入,而不能获其中具体的值 -->
<bean id="remoteConfigs" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>file:/opt/demo/config/demo-remote.properties</value>
</list>
</property>
</bean>
<!-- 远端调用客户端类 -->
<bean id="client" class="com.demo.remote.Client">
<property name="properties" ref="remoteConfigs" />
</bean>
</beans>
代码如下:
import org.springframework.beans.factory.annotation.Autowired;
public class Client() {
//@Autowired也可以使用
private Properties remoteSettings;
//getter setter
}
五、使用通配符加载多个properties文件
<context:property-placeholder location="file:///${CONFIG_PATH}/*.properties" />
或者
<context:property-placeholder location="classpath*:/*.properties"/>
上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# properties配置文件
# spring配置properties
# 在SpringBoot下读取自定义properties配置文件的方法
# SpringBoot获取yml和properties配置文件的内容
# 详解Spring Boot加载properties和yml配置文件
# 详解SpringMVC加载配置Properties文件的几种方式
# 详解spring boot 使用application.properties 进行外部配置
# Spring Boot中配置文件application.properties使用
# Spring Boot2.0 @ConfigurationProperties使用详解
# spring boot使用i18n时properties文件中文乱码问题的解决方法
# Spring加载properties文件的方法
# 详解Spring加载Properties配置文件的四种方式
# Spring中属性文件properties的读取与使用详解
# 谈谈Spring 注入properties文件总结
# spring boot application properties配置实例代码详解
# Spring Boot的properties配置文件读取
# spring boot中的properties参数配置详解
# Spring用代码来读取properties文件实例解析
# Spring加载properties文件的两种方式实例详解
# spring无法读取properties文件数据问题详解
# 配置文件
# 多个
# 加载
# 可以使用
# 推荐使用
# 列表中
# 可以用
# 这两个
# 设置为
# 可以达到
# 群中
# 启动时
# 大家多多
# 通常会
# 远端
# 则需
# 类中
# 组中
# 客户端
# ip
相关文章:
如何选择高效便捷的WAP商城建站系统?
如何彻底删除建站之星生成的Banner?
,sp开头的版面叫什么?
nginx修改上传文件大小限制的方法
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
如何选择可靠的免备案建站服务器?
c++ stringstream用法详解_c++字符串与数字转换利器
如何做网站制作流程,*游戏网站怎么搭建?
怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?
如何快速生成专业多端适配建站电话?
如何选购建站域名与空间?自助平台全解析
香港服务器WordPress建站指南:SEO优化与高效部署策略
高防服务器租用首荐平台,企业级优惠套餐快速部署
无锡营销型网站制作公司,无锡网选车牌流程?
北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?
建站之星图片链接生成指南:自助建站与智能设计教程
青岛网站建设如何选择本地服务器?
如何在阿里云域名上完成建站全流程?
如何注册花生壳免费域名并搭建个人网站?
如何使用Golang安装API文档生成工具_快速生成接口文档
平台云上自助建站如何快速打造专业网站?
美食网站链接制作教程视频,哪个教做美食的网站比较专业点?
如何在服务器上配置二级域名建站?
北京专业网站制作设计师招聘,北京白云观官方网站?
如何获取PHP WAP自助建站系统源码?
网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?
制作企业网站建设方案,怎样建设一个公司网站?
如何快速上传建站程序避免常见错误?
C++时间戳转换成日期时间的步骤和示例代码
大同网页,大同瑞慈医院官网?
如何快速搭建高效服务器建站系统?
建站主机选购指南:核心配置优化与品牌推荐方案
哈尔滨网站建设策划,哈尔滨电工证查询网站?
建站之星上传入口如何快速找到?
电商平台网站制作流程,电商网站如何制作?
如何在Windows服务器上快速搭建网站?
微信小程序 五星评分(包括半颗星评分)实例代码
如何在景安服务器上快速搭建个人网站?
定制建站流程步骤详解:一站式方案设计与开发指南
如何高效配置IIS服务器搭建网站?
制作公司内部网站有哪些,内网如何建网站?
如何选择适合PHP云建站的开源框架?
昆明高端网站制作公司,昆明公租房申请网上登录入口?
如何续费美橙建站之星域名及服务?
香港服务器部署网站为何提示未备案?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
如何快速打造个性化非模板自助建站?
安云自助建站系统如何快速提升SEO排名?
如何快速搭建高效WAP手机网站?
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
*请认真填写需求信息,我们会在24小时内与您取得联系。