PropertyPlaceholderConfigurer 用法

PropertyPlaceholderConfigurer 用法

一、PropertyPlaceholderConfigurer 的繼承體系

PropertyPlaceholderConfigurer位於org.springframework.beans.factory.config包下,它的繼承體系如下

在這裏插入圖片描述
PropertyPlaceholderConfigurer 直接繼承於PlaceholderConfigurerSupport,它的已知實現類只有一個

PreferencesPlaceholderConfigurer

二、PropertyPlaceholderConfigurer 的基本概念

​ 源自JavaDoc: PropertyPlaceholderConfigurer 是 PlaceholderConfigurerSupport 的一個子類,用來解析${…} 佔位符的,可以使用setLocationsetProperties設置系統屬性和環境變量。從Spring3.1 開始,PropertySourcesPlaceholderConfigurer應優先與此實現,通過使用Spring3.1 中的Environment和 PropertySource 機制, 使它的靈活性更強。

​ 但是PropertyPlaceholderConfigurer卻適用如下情況:當 spring-context 模塊不可用的時候,使用BeanFactory的API 而不是 ApplicationContext 的API。現有配置使用 setSystemPropertiesMode 和 setSystemPropertiesModeName屬性,建議用戶不要使用這些設置, 而是使用容器的__Environment__屬性;

在Spring3.1 之前,<context:property-placeholder/>命名空間保存了PropertyPlaceholderConfigurer的實例,如果使用spring-context-3.0 xsd的定義的話,仍然會這樣做。也就是說,即使使用Spring 3.1,您也可以通過命名空間保留PropertyPlaceholderConfigurer; 只是不更新schemaLocation 並繼續使用3.0 XSD。

三、PropertyPlaceholderConfigurer 的基本使用

  1. PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor接口的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置文 件)中的屬性值放在另一個單獨的標準java Properties文件中去。在XML文件中用${…}替換指定的properties文件中的值。這樣的話,只需要對properties文件進 行修改,而不用對xml配置文件進行修改。
  2. 在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部屬性文件

PropertyPlaceholderConfigurer 引入外部屬性文件

  • 定義一個properties 屬性文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sys
jdbc.username=root
jdbc.password=123456

這是一個最基本的配置數據庫連接的設置,前綴統一使用jdbc來命名

  • 定義xml用來獲取上面properties中的內容
	<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-2.5.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>database.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

	</beans>

通過給PropertyPlaceholderConfigurer 設置一個bean,指定的名稱爲location,指定value值就能夠引入外部配置文件,然後就能夠通過${jdbc.key} 來獲取properties 中的值

PropertyPlaceholderConfigurer 引入多個屬性文件

  • 再來定義一個encoding.properties
file.encoding=utf-8
file.name=encoding
  • PropertyPlaceholderConfigurer 引入多個屬性文件比較簡單,需要把__location -> locations__ ,然後直接指定一個list 就能夠引入
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>database.properties</value>
                <value>encoding.properties</value>
            </list>
        </property>
	</bean>
  1. 這樣,一個簡單的數據源就設置完畢了。可以看出:PropertyPlaceholderConfigurer起的作用就是將佔位符指向的數據庫配置信息放在bean中定義的工具。
  2. 查看源代碼,可以發現,locations屬性定義在PropertyPlaceholderConfigurer的祖父類 PropertiesLoaderSupport中,而location只有 setter方法。類似於這樣的配置,在spring的源程序中很常見的。PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的屬性,它還會在Java的System類屬性中查找。我們可以通過System.setProperty(key, value)或者java中通過-Dnamevalue來給Spring配置文件傳遞參數。

PropertyPlaceholderConfigurer 的替代方案

​ 正如PropertyPlaceholderConfigurer基本概念中提到的,Spring可以使用<context:property-placeholder/> 作爲PropertyPlaceholderConfigurer 的替代方案,代碼如下

	 <!-- 指定單個properties -->
    <!--<context:property-placeholder location="database.properties" />-->
    <!-- 指定多個properties-->
    <!--<context:property-placeholder location="classpath:*.properties"/>-->
    <!--<context:property-placeholder location="classpath:database.properties, classpath:encoding.properties"/>-->
    <!-- 指定配置文件加載順序-->
    <context:property-placeholder order="0" location="database.properties" />
    <context:property-placeholder order="1" location="encoding.properties" />

四、自定義PropertyPlaceholderConfigurer

  • 自定義一個SubPropertyPlaceholderConfigurer 繼承自PropertyPlaceholderConfigurer
		public class SubPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

        private static Map<String, String> ctxPropertiesMap;

        @Override
        protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
            // 調用父類PropertyPlaceholderConfigurer 的構造器
            super.processProperties(beanFactoryToProcess, props);
            // 遍歷配置文件的key,Properties 對象就是導入的配置文件
            Enumeration<?> enumeration = props.propertyNames();
            while (enumeration.hasMoreElements()) {
                System.out.println(enumeration.nextElement());
            }

            ctxPropertiesMap = new HashMap<String, String>();
            for (Object key : props.keySet()) {
                String keyStr = key.toString();
                String value = props.getProperty(keyStr);
                ctxPropertiesMap.put(keyStr, value);
            }
        }

        public static String getProperty(String name){
            return ctxPropertiesMap.get(name);
        }
    }
  • 需要引入這個自定義的SubPropertyPlaceholderConfigurer
	<bean id="propertyPlaceholderConfigurer" class="com.cxuan.spring.common.SubPropertyPlaceholderConfigurer">
        <property name="location">
            <value>database.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

如何啓動呢?其實引入的SubPropertyPlaceholderConfigurer 就能夠隨着Spring加載配置文件而被加載。

直接定義main方法,用ClassPathXmlApplicayionContext引入任意的配置文件即可。

文章參考:

Spring裏PropertyPlaceholderConfigurer類的使用](https://www.cnblogs.com/huqianliang/p/5673701.html)

PropertyPlaceholderConfigurer讀取配置文件

https://blog.csdn.net/y_index/article/details/79893765

https://blog.csdn.net/eson_15/article/details/51365707

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html#processProperties-org.springframework.beans.factory.config.ConfigurableListableBeanFactory-java.util.Properties-

https://blog.csdn.net/wrs120/article/details/84554366

歡迎關注:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章