Spring 常用基础点汇总(二)Spring中Properties文件读取

 

 Spring对Properties文件的管理

 

一、通过util方式

   util方式有两种方法,一种是通过配置bean方式,一种是通过标签方式。具体方式如下:

 1、通过配置bean的方法

   

  1.  <!-- 将多个配置文件位置放到列表中 -->  
  2.     <bean id="propertyResources" class="java.util.ArrayList">  
  3.         <constructor-arg>  
  4.             <list>  
  5.               <!-- 这里支持多种寻址方式:classpath和file -->  
  6.               <value>classpath:/opt/demo/config/db.properties</value>  
  7.               <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
  8.               <value>file:/opt/demo/config/demo-mq.properties</value>  
  9.               <value>file:/opt/demo/config/demo-remote.properties</value>  
  10.             </list>  
  11.         </constructor-arg>  
  12.     </bean> 
  13.  

2、通过标签的方式

   标签其实是对配置的一种简化的写法,看起来更简洁,但一次只能配置一个property文件,加载多个时需要配置多个util标签。另外在配置前物业添加上必须在XML中加入util名称空间(namespace)

 

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="

   http://www.springframework.org/schema/util 

   http://www.springframework.org/schema/util/spring-util-3.1.xsd 

"

具体的使用方式如下:

<util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" /> 

 

 3、对util配置的properties引用方式如下:

     (1).bean配置中的引用

          <property name="driverClassName" value="#{remoteSettings['db.driver']}" />

  <property name="url" value="#{remoteSettings['db.url']}" />

  <property name="username" value="#{remoteSettings['db.userName']}" />

  <property name="password" value="#{remoteSettings['db.password']}"/>

 

     (2)java 类中通过注解方式的引用

   

  1. public class Client() {  
  2.     @Value("#{remoteSettings['remote.ip']}")  
  3.     private String ip;  
  4.     @Value("#{remoteSettings['remote.port']}")  
  5.     private String port;  
  6.     @Value("#{remoteSettings['remote.serviceName']}")  
  7.     private String service;  
  8. }  

Bean中存在Properties类型的类变量需要以注入的方式初始化

  1. public class Client() {  
  2.     @Value("#{remoteSettings}")  
  3.     private Properties remoteSettings;  

 

 

二、通过Spring属性占位符PropertyPlaceholderConfigurer的使用

同样会有两种方式,一种是通过bean配置方式,一种是通过标签的方式

1、bean配置方式

  <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <!-- 

        使用location属性定义单个配置文件

        <property name="location">

            <value>classpath:/com/zsw/config/jdbc.properties</value>

        </property>

         -->

  

  <!-- 使用locations属性定义多个配置文件 -->

  <property name="locations">

     <list>

        <value>classpath:/com/zsw/config/jdbc.properties</value>

     </list>

  </property> 

</bean>

 

此种方式可以结合util配置方式一起使用,如下:

 

<!-- 用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="remoteSettings" />  

    </bean>

 

 

2 、通过标签的方式

     此标签是在spring3.0以后引用的

  1. <context:property-placeholder location="userinfo.properties"/> 

PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为

 

3、bean配置中对占位符的使用

  1. bean name="userInfo" class="test.UserInfo">  
  2.   <property name="username" value="${db.username}"/>  
  3.   <property name="password" value="${db.password}"/>  
  4. </bean>

 

/***--------------------------------------------------分割线 -------------------------------------**/

 

spring对各种第三方插件都做了很好的支持,同样也对Apache公司提交的配置管理插件提供了很好的支持,通过从数据库中读出配置信息的方式很好的解决了环境差异的问题,集成的方法如下:

引用所使用的jar包:

<!-- configuration to db support -->

<dependency>

<groupId>commons-configuration</groupId>

<artifactId>commons-configuration</artifactId>

<version>1.9</version>

</dependency>

<dependency>

<groupId>org.springmodules</groupId>

<artifactId>spring-modules-jakarta-commons</artifactId>

<version>0.8a</version>

<exclusions>

<exclusion>

<groupId>xerces</groupId>

<artifactId>xerces</artifactId>

</exclusion>

<exclusion>

<groupId>org.springframework</groupId>

<artifactId>spring-support</artifactId>

</exclusion>

<exclusion>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

</exclusion>

<exclusion>

<artifactId>commons-chain</artifactId>

<groupId>commons-chain</groupId>

</exclusion>

</exclusions>

</dependency>

 

 

具体的配置信息如下:

<?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"

 xsi:schemaLocation="http://www.springframework.org/schema/beans 

 http://www.springframework.org/schema/beans/spring-beans.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context.xsd">

 

<description>从数据库装入配置到 propertyConfigurer</description>

 

<!-- 方式一-->

<!-- <context:property-placeholder  properties-ref="applicationProperties" />  -->

<!-- 方式二 -->

<bean id="propertyConfigurer"  

   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

<property name="properties" ref="applicationProperties">  

     </property>  

</bean> 

 

<bean name="applicationProperties" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">

<constructor-arg ref="databaseConfiguration" />

</bean>

 

<bean name="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">

<constructor-arg index="0" ref="dataSource" />

<constructor-arg index="1" value="T_APP_PROPERTIES" />

<constructor-arg index="2" value="APP_ID" />

<constructor-arg index="3" value="KEY_" />

<constructor-arg index="4" value="VALUE_" />

<constructor-arg index="5" value="test" />

</bean>

 

</beans>

 

 

对Apache DatabaseConfiguration 的使用可以参考其它文档信息

其中配置的构造参数很容易通过读代码看出来,第0个是数据源、第1个是表名、第2、3、4分别对应表中的字段,第5个字段表示的是第2个字段的值,即 APP_ID="test";KEY_ 对应前面的"db.username",VALUE_为对应的值

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了一种便捷的方式来读取properties文件的配置信息。在Spring,我们可以使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer来加载properties文件。 PropertyPlaceholderConfigurer是在Spring 3.1之前版本使用的类,用于从classpath或指定位置加载properties文件。它可以将properties文件的键值对替换为Spring配置文件的占位符。通过将PropertyPlaceholderConfigurer配置为Bean的一部分,我们可以在XML配置文件使用占位符来引用properties文件的值。例如,我们可以在XML配置一个DataSource Bean,并通过占位符引用properties文件数据库连接信息。 PropertySourcesPlaceholderConfigurer是从Spring 3.1版本引入的新类,它可以更方便地处理properties文件。与PropertyPlaceholderConfigurer不同,PropertySourcesPlaceholderConfigurer允许我们使用@Value注解来直接注入properties文件的属性。我们可以在Java配置类或XML配置文件配置PropertySourcesPlaceholderConfigurer,并在需要的地方使用@Value注解来获取properties文件的属性值。 无论我们使用哪种方式来读取properties文件,我们都需要确保将其配置为Spring容器的一部分,并以适当的方式加载它。通常,我们会在Spring配置文件添加一个bean来加载properties文件,并将其配置为其他bean的属性或参数的值。 总结起来,Spring提供了两种主要的方法来读取properties文件:使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer。这些类可以让我们轻松地将properties文件的配置值引入到Spring容器,并在我们的应用程序使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值