Spring properties鏂囦欢涓枃涔辩爜鐨勫師鍥犲強瑙e喅鏂规

鍏堢湅鐪嬪師鎯呭喌浠ュ強鏁堟灉鍥�

image.png

璇村埌涔辩爜闂锛屽叾瀹炵洰鍓嶉亣鍒扮殑涔辩爜闂鍘熷洜鍙互褰掓牴涓�璇诲彇鍗忚涓嶅绛�锛屼緥濡備娇鐢℅BK鏂囦欢鏍煎紡鍘昏鍙朥TF-8鏂囦欢鏍煎紡鐨勫唴瀹硅偗瀹氫細鍑虹幇涔辩爜锛岄偅涔堟垜浠В鍐抽棶棰樼殑鏁翠綋鎬濊矾涔熷氨鏄�浣跨敤鍚堥�傜殑璇诲彇鏍煎紡鍘昏鍙栨枃浠跺唴瀹�

鏈瘒瀛︿範绗旇浠g爜鍦板潃鍦�simple-spring 鐨剆pring-learn妯″潡鐨刡pp鏂囦欢澶逛腑

鍦ㄦ鎰熻阿 antz_H 鐨勬寚姝o紝姝ょ瑪璁板彧鏄湰浜哄湪瀛︿範璋冭瘯Spring浠g爜鏃跺紕鐨勶紝鍚姩鐨勬柟娉曚篃鏄�ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[]{"spring/spring-bpp.xml"}) 涓嶉�傜敤浜嶴pringBoot甯歌鐨勬儏鍐典笅锛孲pringBoot宸茬粡鎻愪緵浜嗙幇鎴愮殑@PropertySource 娉ㄨВ 瑙e喅鏂规

Properties 鏂囦欢璇诲彇

鍦ㄤ箣鍓嶇殑瀛︿範绗旇涓�Spring Properties灞炴�ц幏鍙� 婧愮爜瀛︿範锛屼篃鏇剧粡浠嬬粛杩囧浣曡鍙�@Value("${XXX}")鍦ㄥ睘鎬ф枃浠朵腑XXX瀵瑰簲鐨勫�笺��

PropertySourcesPlaceholderConfigurer 绫绘槸瀹炵幇BeanFactoryPostProcessor鎺ュ彛鐨勶紝鍦ㄦ墽琛�postProcessBeanFactory鏂规硶鐨勬椂鍊欙紝浼氳繘琛屽睘鎬ф枃浠剁殑璇诲彇骞朵笖鎶婇敭鍊煎淇℃伅瀛樺偍鍒癙ropertySourcesPlaceholderConfigurer绫绘湰韬殑瀹瑰櫒涓�

鍦ㄦ娊璞$被PropertiesLoaderSupport鐨�protected void loadProperties(Properties props) throws IOException鏂规硶鍘诲畬鎴愬睘鎬ф枃浠跺唴瀹圭殑璇诲彇

鏈�鍚庢潵鍒颁簡PropertiesLoaderUtils宸ュ叿绫讳腑

    static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
            throws IOException {

        InputStream stream = null;
        Reader reader = null;
        try {
            String filename = resource.getResource().getFilename();
            // 鑾峰彇鏂囦欢鍚嶇О
            if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
                  // 1澶� 鏂囦欢鍚嶄笉涓簄ull锛岃�屼笖鏄痻ml缁撳熬
                stream = resource.getInputStream();
                persister.loadFromXml(props, stream);
            }
            else if (resource.requiresReader()) {
                  // 2澶� 濡傛灉resource涓殑encoding瀛楁鎴栬�卌harset瀛楁涓嶄负绌�
                    // 灏变細浣跨敤encoding鍐呯殑瀛楃缂栫爜鎴栬�卌harset璁剧疆鐨勫瓧绗︾紪鐮佹牸寮忚鍙栧睘鎬ф枃浠�
                reader = resource.getReader();
                persister.load(props, reader);
            }
            else {
                   // 3澶� 鍚﹀垯灏辨槸浣跨敤榛樿鐨勫瓧绗︾紪鐮佽幏鍙朓O娴�
                stream = resource.getInputStream();
                persister.load(props, stream);
            }
        }

鎵�浠ョ幇鍦ㄨ兘澶熻В鍐冲綋鍓嶉棶棰樺彧鏈�2澶勪簡锛岄渶瑕佽缃畆esource瀵硅薄鐨別ncoding銆乧harset瀛楁鍊硷紝鑰屾resource鐢熸垚鏄娇鐢ㄤ簡new EncodedResource(location, this.fileEncoding)瀹炵幇鐨�

鐜板湪瑙e喅鏂规宸茬粡闈炲父鏄庝簡浜嗭紝涔熷氨鏄缃甈ropertySourcesPlaceholderConfigurerbean鐨刦ileEncoding瀛楁鍊煎嵆鍙�

fileEncoding 瀛楁鍊煎~鍏�

xml閰嶇疆鐨勬柟娉曡繕鏄緢绠�鍗曠殑,鐩存帴璁剧疆bean鐨勫熀鏈弬鏁帮紝鐒跺悗鍦╞ean瀹炰緥瀹屼箣鍚庡弬鏁板~鍏呭嵆鍙�

image

鐜板湪璁ㄨ鐨勬槸鍙︿竴绉嶆儏鍐碉紝娌℃湁瀵瑰鐩存帴鐨刦ile-encoding瀛楁璁剧疆鑳藉姏

涓婇潰宸茬粡璇翠簡锛�璋冪敤postProcessBeanFactory鏂规硶锛屼細杩涜灞炴�ф枃浠剁殑璇诲彇锛堜篃灏辨剰鍛崇潃PropertySourcesPlaceholderConfigurer鏄竴涓疄鐜颁簡BeanFactoryPostProcessor鍜孭riorityOrdered鎺ュ彛鐨勭壒娈夿PP绫伙級锛岄偅涔堝氨蹇呴』鍦ㄨ繖涓柟娉曡皟鐢ㄥ墠瀹屾垚鍙傛暟鐨勬敞鍏�

閲嶇偣鍏虫敞BPP澶勭悊鐨勬牳蹇冪被PostProcessorRegistrationDelegate

鍙互鎻愬嚭涓ょ瑙e喅鏂规

  • 瀹炵幇BeanDefinitionRegistryPostProcessor 鎺ュ彛
  • 瀹炵幇BeanFactoryPostProcessor銆丳riorityOrdered 鎺ュ彛锛堟帓搴忚秴杩嘝ropertySourcesPlaceholderConfigurer锛�

鑷充簬涓哄暐鏄繖涓ょ鏂规锛屽彲浠ョ湅鐪嬩箣鍓嶅啓鐨�Spring 閽╁瓙涔婤eanFactoryPostProcessor鍜孊eanPostProcessor鐨勬簮鐮佸涔� 銆�鍐嶈皥Spring BeanPostProcessor

渚嬪濡備笅鑷畾涔夌殑BPP锛堝叿鏂规硶鍙湅github鍐呯殑浠g爜锛�

@Component
public class PriorityOrderedBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
        PriorityOrdered {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
            throws BeansException {
        Map map = beanFactory.getBeansOfType(PropertySourcesPlaceholderConfigurer.class);
        // 鍙兘瀛樺湪澶氫釜PropertySourcesPlaceholderConfigurerbean鐨勬儏鍐�
        Iterator> it = map.entrySet().iterator();

        while (it.hasNext()) {
            Map.Entry entry = it.next();
            PropertySourcesPlaceholderConfigurer pp = entry.getValue();
            pp.setFileEncoding("UTF-8");
            // 缁堜簬鑾峰彇鍒颁簡璁剧疆鏂囦欢缂栫爜绫诲瀷鐨勫湴鏂逛簡
            // 杩欎篃鏄渶鍏抽敭鐨勫湴鏂�
        }
    }

    @Override
    public int getOrder() {
        // 鎺掑簭瑙勫垯锛屽叿浣撳彲浠ョ湅鐪婳rderComparator鐨勬帓搴忚鍒�
        // PropertySourcesPlaceholderConfigurer鐨勬帓搴弌rder鏄渶浣庣殑锛屾墍浠ヨ缃竴涓�0瀹屽叏鍙互婊¤冻瑕佹眰
        return 0;
    }
}

鎬荤粨

鏈涔犵瑪璁颁篃鍙互绠楁槸鍙堜竴绡嘊PP鐨勫疄璺碉紝涓�鏂归潰鍔犳繁瀵笲PP鐨勪簡瑙o紝鍙︿竴鏂归潰缁撳悎褰撳墠閬囧埌鐨勫叿浣撻棶棰樺叿浣撹В鍐筹紝鍏跺疄闅忕潃瀵箂pring瀛︿範鐨勬繁鍏ヤ細鍙戠幇瓒婃潵瓒婂閫氳繃BPP鎵╁睍鐨勫姛鑳斤紝渚嬪鐩戞帶spring-actuator涔熸槸鍒╃敤BPP娣诲姞棰濆鐨勭鐐瑰畬鎴愮浉搴旂殑鍔熻兘

濡傛灉瀵笲PP鏈夋洿澶氬叴瓒g殑锛孭ostProcessorRegistrationDelegate绫婚渶瑕佹潵鍥炵悽纾紝璋冭瘯

你可能感兴趣的:(Spring properties鏂囦欢涓枃涔辩爜鐨勫師鍥犲強瑙e喅鏂规)