`
liujiekasini0312
  • 浏览: 131115 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring中配置和读取多个Properties文件

 
阅读更多

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

1.数据库配置文件demo-db.properties:

Properties代码收藏代码
  1. database.url=jdbc:mysql://localhost/smaple
  2. database.driver=com.mysql.jdbc.Driver
  3. database.user=root
  4. database.password=123

2.消息服务配置文件demo-mq.properties:

Properties代码收藏代码
  1. #congfigofActiveMQ
  2. mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
  3. mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
  4. mq.java.naming.security.principal=
  5. mq.java.naming.security.credentials=
  6. jms.MailNotifyQueue.consumer=5

3.远程调用的配置文件demo-remote.properties:

Properties代码收藏代码
  1. remote.ip=localhost
  2. remote.port=16800
  3. remote.serviceName=test

一、系统中需要加载多个Properties配置文件

应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。

配置方式:

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!--将多个配置文件读取到容器中,交给Spring管理-->
  7. <beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  8. <propertyname="locations">
  9. <list>
  10. <!--这里支持多种寻址方式:classpath和file-->
  11. <value>classpath:/opt/demo/config/demo-db.properties</value>
  12. <!--推荐使用file的方式引入,这样可以将配置和代码分离-->
  13. <value>file:/opt/demo/config/demo-mq.properties</value>
  14. <value>file:/opt/demo/config/demo-remote.properties</value>
  15. </list>
  16. </property>
  17. </bean>
  18. <!--使用MQ中的配置-->
  19. <beanid="MQJndiTemplate"class="org.springframework.jndi.JndiTemplate">
  20. <propertyname="environment">
  21. <props>
  22. <propkey="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
  23. <propkey="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
  24. <propkey="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
  25. <propkey="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
  26. <propkey="userName">${mq.java.naming.security.principal}</prop>
  27. <propkey="password">${mq.java.naming.security.credentials}</prop>
  28. </props>
  29. </property>
  30. </bean>
  31. </beans>

我们也可以将配置中的List抽取出来:

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!--将多个配置文件位置放到列表中-->
  7. <beanid="propertyResources"class="java.util.ArrayList">
  8. <constructor-arg>
  9. <list>
  10. <!--这里支持多种寻址方式:classpath和file-->
  11. <value>classpath:/opt/demo/config/demo-db.properties</value>
  12. <!--推荐使用file的方式引入,这样可以将配置和代码分离-->
  13. <value>file:/opt/demo/config/demo-mq.properties</value>
  14. <value>file:/opt/demo/config/demo-remote.properties</value>
  15. </list>
  16. </constructor-arg>
  17. </bean>
  18. <!--将配置文件读取到容器中,交给Spring管理-->
  19. <beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  20. <propertyname="locations"ref="propertyResources"/>
  21. </bean>
  22. <!--使用MQ中的配置-->
  23. <beanid="MQJndiTemplate"class="org.springframework.jndi.JndiTemplate">
  24. <propertyname="environment">
  25. <props>
  26. <propkey="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
  27. <propkey="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
  28. <propkey="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
  29. <propkey="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
  30. <propkey="userName">${mq.java.naming.security.principal}</prop>
  31. <propkey="password">${mq.java.naming.security.credentials}</prop>
  32. </props>
  33. </property>
  34. </bean>
  35. </beans>

二、整合多工程下的多个分散的Properties

应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。

配置如下:

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7. <!--将DB属性配置文件位置放到列表中-->
  8. <beanid="dbResources"class="java.util.ArrayList">
  9. <constructor-arg>
  10. <list>
  11. <value>file:/opt/demo/config/demo-db.properties</value>
  12. </list>
  13. </constructor-arg>
  14. </bean>
  15. <!--将MQ属性配置文件位置放到列表中-->
  16. <beanid="mqResources"class="java.util.ArrayList">
  17. <constructor-arg>
  18. <list>
  19. <value>file:/opt/demo/config/demo-mq.properties</value>
  20. </list>
  21. </constructor-arg>
  22. </bean>
  23. <!--用Spring加载和管理DB属性配置文件-->
  24. <beanid="dbPropertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  25. <propertyname="order"value="1"/>
  26. <propertyname="ignoreUnresolvablePlaceholders"value="true"/>
  27. <propertyname="locations"ref="dbResources"/>
  28. </bean>
  29. <!--用Spring加载和管理MQ属性配置文件-->
  30. <beanid="mqPropertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  31. <propertyname="order"value="2"/>
  32. <propertyname="ignoreUnresolvablePlaceholders"value="true"/>
  33. <propertyname="locations"ref="mqResources"/>
  34. </bean>
  35. <!--使用DB中的配置属性-->
  36. <beanid="rmsDataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"
  37. p:driverClassName="${demo.db.driver}"p:url="${demo.db.url}"p:username="${demo.db.username}"
  38. p:password="${demo.db.password}"pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"
  39. p:poolPreparedStatements="true"p:defaultAutoCommit="false">
  40. </bean>
  41. <!--使用MQ中的配置-->
  42. <beanid="MQJndiTemplate"class="org.springframework.jndi.JndiTemplate">
  43. <propertyname="environment">
  44. <props>
  45. <propkey="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
  46. <propkey="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
  47. <propkey="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
  48. <propkey="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
  49. <propkey="userName">${mq.java.naming.security.principal}</prop>
  50. <propkey="password">${mq.java.naming.security.credentials}</prop>
  51. </props>
  52. </property>
  53. </bean>
  54. </beans>

注意:其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。这里一定需要按照这种方式设置这两个参数。

三、Bean中直接注入Properties配置文件中的值

应用场景:Bean中需要直接注入Properties配置文件中的值。例如下面的代码中需要获取上述demo-remote.properties中的值:

Java代码收藏代码
  1. publicclassClient(){
  2. privateStringip;
  3. privateStringport;
  4. privateStringservice;
  5. }

配置如下:

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="<ahref="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"
  3. xmlns:xsi="<ahref="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
  4. xmlns:util="<ahref="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</a>"
  5. xsi:schemaLocation="
  6. <ahref="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a><ahref="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a>
  7. <ahref="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</a><ahref="http://www.springframework.org/schema/util/spring-util-3.0.xsd">http://www.springframework.org/schema/util/spring-util-3.0.xsd</a>">
  8. <!--这种加载方式可以在代码中通过@Value注解进行注入,
  9. 可以将配置整体赋给Properties类型的类变量,也可以取出其中的一项赋值给String类型的类变量-->
  10. <!--<util:properties/>标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签-->
  11. <util:propertiesid="remoteSettings"location="file:/opt/demo/config/demo-remote.properties"/>
  12. <!--<util:properties/>标签的实现类是PropertiesFactoryBean,
  13. 直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的-->
  14. <beanid="settings"
  15. class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  16. <propertyname="locations">
  17. <list>
  18. <value>file:/opt/rms/config/rms-mq.properties</value>
  19. <value>file:/opt/rms/config/rms-env.properties</value>
  20. </list>
  21. </property>
  22. </bean>
  23. </beans>

Client类中使用Annotation如下:

Java代码收藏代码
  1. importorg.springframework.beans.factory.annotation.Value;
  2. publicclassClient(){
  3. @Value("#{remoteSettings['remote.ip']}")
  4. privateStringip;
  5. @Value("#{remoteSettings['remote.port']}")
  6. privateStringport;
  7. @Value("#{remoteSettings['remote.serviceName']}")
  8. privateStringservice;
  9. }

四、Bean中存在Properties类型的类变量

应用场景:当Bean中存在Properties类型的类变量需要以注入的方式初始化

1. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下

Java代码收藏代码
  1. importorg.springframework.beans.factory.annotation.Value;
  2. importorg.springframework.beans.factory.annotation.Autowired;
  3. publicclassClient(){
  4. @Value("#{remoteSettings}")
  5. privatePropertiesremoteSettings;
  6. }

2. 配置方式:也可以使用xml中声明Bean并且注入

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!--可以使用如下的方式声明Properties类型的FactoryBean来加载配置文件,这种方式就只能当做Properties属性注入,而不能获其中具体的值-->
  7. <beanid="remoteConfigs"class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  8. <propertyname="locations">
  9. <list>
  10. <value>file:/opt/demo/config/demo-remote.properties</value>
  11. </list>
  12. </property>
  13. </bean>
  14. <!--远端调用客户端类-->
  15. <beanid="client"class="com.demo.remote.Client">
  16. <propertyname="properties"ref="remoteConfigs"/>
  17. </bean>
  18. </beans>

代码如下:

Java代码收藏代码
  1. importorg.springframework.beans.factory.annotation.Autowired;
  2. publicclassClient(){
  3. //@Autowired也可以使用
  4. privatePropertiesremoteSettings;
  5. //gettersetter
  6. }

上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。

分享到:
评论

相关推荐

    Spring中配置和读取多个Properties文件的方式方法

    本篇文章主要介绍了Spring中配置和读取多个Properties文件的方式方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

    Spring加载配置和读取多个Properties文件的讲解

    今天小编就为大家分享一篇关于Spring加载配置和读取多个Properties文件的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    17 Spring IoC容器如何读取多个属性文件或者配置文件?慕课专栏(1)1

    背景在 Spring 项目中,你可能需要从 properties 文件中读入配置注入到 bean 中,例如数据库连接信息,redis server的地址端口信息

    osgi多个bundle读取同一配置文件

    osgi多个bundle之间读取同一配置文件,需要单独添加一个用来读取配置文件的bundle

    Spring-Reference_zh_CN(Spring中文参考手册)

    处理多个持久化单元 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的JPA实现DAO 12.6.4. 异常转化 12.6.5. 事务管理 12.6.6. JpaDialect III. Web 13. Web框架 13.1. 介绍 13.1.1. 与其他web框架的集成 ...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    Spring MVC 入门实例

    你可以在 list 标签中配置多个 value 标签. database.xml: 1 2 &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt; 3 4 5 6 &lt;!-- Remove ...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    SpringBoot的配置文件

    SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用 application.properties或者application.yml(application.yaml)进行配置 SpringBoot默认会从Resources目录...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    spring chm文档

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    ssh(structs,spring,hibernate)框架中的上传下载

    如果有多个映射文件需要声明,使用类路径映射方式显然比直接单独指定映射文件名的方式要简便。  第27~30行定义了Spring代理Hibernate数据操作的HibernateTemplate模板,而第32~34行将该模板注入到tfileDAO中。  ...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    properties:Go的Java属性扫描器

    它支持从多个文件或URL进行读取,并将${key}类的表达式的Spring样式递归属性扩展为它们的相应值。 值表达式可以引用${key}其他键,也可以引用${USER}环境变量。 文件名还可以包含/home/${USER}/myapp.properties...

    自己写的struts2+hibernate+spring实例

    注:本代码为个人所写.全当练习使用.愿所有人能够更好地学习软件.愿意与所有软件爱好者成为好友. 邮箱:menxin32@163.com&lt;br&gt;当前版本...数据库连接配置文件是/s2sh/src/目录下面的jdbc.properties文件.直接配置即可.

    基于SpringBoot的移动ssh项目(源码)

    此时我们可以看一下settings中的内容,会发现将新创建的teacher自动添加到了其中config中是自定义读取application-schoolinfo.properties文件中的配置。因为需要配置数据源,但是最终我们是需要student和teacher都...

    spring-cloud-books

    每个项目的info信息都是直接从maven的pom文件中读取,具体可参考各个项目的resources目录下的application和application-test属性文件 ``` #查看info信息配置 info.app.name=@project.name@ info.app.description=@...

    (2.0版本)自己写的struts2+hibernate+spring实例

    数据库连接配置文件是/s2sh/src/目录下面的jdbc.properties文件.直接配置即可. &lt;br&gt;以下是一些相关说明. &lt;br&gt; 数据库脚本 create table HUBO_USER ( ID VARCHAR2(32) not null, NAME VARCHAR...

    spring-boot-filter:一个简单的Spring Boot过滤器数据示例

    弹簧靴过滤器 这是在Spring Boot中将过滤器与外部库一起使用的示例 通过mvn clean spring-boot:run运行此仓库 ... 按多个属性过滤 http://myhost/api/customers?firstName=Homer&lastName=Simpson 这个仓库来自

    基于Dubbo实现的SOA分布式(没有实现分布式事务)-SpringBoot整合各种组件的JavaWeb脚手架+源代码+文档

    6. 引用application.properties中的属性的方式:@ConfigurationProperties(prefix = "spring.mail") + @Component + setter + getter 7. 引用其他自定义配置文件中的属性的方式: - @Component - ## 项目备注 1...

Global site tag (gtag.js) - Google Analytics