Spring構成のP r o p e r t y PlaceholdeConfigurer,P r o p e r t y OverrideConfigurer


PropertyPlaceholderConfigurerは、Springが外部プロパティファイルからプロパティをロードし、Springプロファイルのプレースホルダ変数(${varible})をこれらのプロパティ値で置き換えます.
BeanFactoryPostProcessorインタフェースはBeanファクトリに対する後処理操作であり、SpringのP r e t y PlaceholderConfigurerクラスはBeanFactoryProcessorインタフェースを実現するのに非常に有用なクラスである.
SpringのApplicationContextコンテナは、簡単な構成で簡単に使用できるPropertyPlaceholderConfigurerを便利に使用できます.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="jdbc.properties" />
</bean> 

PropertyPlaceholderのlocationsプロパティを使用するには、複数のプロファイルを使用する必要があります.
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
         <list>
             <value>hello.properties</value> 
             <value>welcome.properties</value> 
             <value>other.properties</value>
         </list>
    </property>     
</bean>

propertiesファイルのフォーマットは次のとおりです.
#oracle :\u6570\u636e\u6e90
datasource.type=oracle
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#datasource.url=jdbc:oracle:thin:@192.168.1.2:1521:orcl
datasource.username=cpcim2
datasource.password=cpcim2

------------------------分割線-------------------------------
PropertyOverrideConfigurerは、PropertyPlaceholder Configurerに似ていますが、前者はbeanプロパティに対してデフォルト値があるか、まったく値がありません.上書き機能を持つPropertiesファイルにbeanプロパティの内容がない場合は、デフォルトのコンテキスト定義が使用されます.
注意:beanファクトリの定義は上書きされていることを認識しないため、XML定義ファイルを参照するだけでは、上書き構成が使用されているかどうかをすぐに明らかにすることはできません.複数のPorpertyOverrideConfigurer対が1つのbean属性で異なる値を定義している場合、最後の1つが勝つ(上書きのメカニズムに依存).
Propertiesファイルの1行の構成は、次の形式である必要があります.
beanName.property=value、beanNameは上書きが必要なbeanの名前、propertyは上書きが必要な属性名
エンティティ:
public  class Chinese {
    private String name;
    private String age;
    public String getAge(){
        return age;
    }
    public void setAge(String age){
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}

プロファイル:年齢age注入値30
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
     <property name="locations" value="classpath:Bean/propertytwo/person.properties">
     </property>
  </bean>
  <bean id="chinese" class="Bean.propertytwo.Chinese">
      <property name="age" value="30"></property>
        <property name="name" value="test">
      </property>
  </bean>
</beans>

プロパティファイル、ageを26に設定します.
chinese.age=26
テストコード:
public static void main(String[] args) throws Exception {
        
        String path=new Test().getClass().getResource("/").getPath();
        String realpath=path.substring(1, path.length());
        ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/propertytwo.xml");

        Chinese p=(Chinese)context.getBean("chinese");
        System.out.println(p.getName()+p.getAge());
}

実行結果:
test26
ageはpropertiesの数値で上書きされており、プロファイルの数値は使用されていません.