Summary of common basic points in Spring (2) Reading the Properties file in Spring

 

 Spring's management of Properties files

 

1. Through the util method

   There are two ways to use the util method, one is through the configuration bean method, and the other is through the label method. The specific methods are as follows:

 1. By configuring the bean method

   

  1.  <!-- put multiple config file locations into a list -->  
  2.     <bean id="propertyResources" class="java.util.ArrayList">  
  3.         <constructor-arg>  
  4.             <list>  
  5.               <!-- Multiple addressing methods are supported here: classpath and file -->  
  6.               <value>classpath:/opt/demo/config/db.properties</value>  
  7.               <!-- It is recommended to use file introduction, so that configuration and code can be separated -->  
  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. By way of labels

   The label is actually a simplified way of writing the configuration, which looks more concise, but only one property file can be configured at a time, and multiple util labels need to be configured when loading multiple ones. In addition , the util namespace (namespace) must be added to the XML before the property is added.

 

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 

"

The specific usage is as follows:

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

 

 3. The way to refer to the properties of the util configuration is as follows:

     (1). References in the bean configuration

          <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) References by annotations in java classes

   

  1. public  class  Customer() {  
  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. }  

When a class variable of type Properties exists in the bean, it needs to be initialized by injection

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

 

 

Second, through the use of Spring property placeholder PropertyPlaceholderConfigurer

There are also two ways, one is through bean configuration, the other is through label

1. bean configuration method

  <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_为对应的值

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326774678&siteId=291194637