天天看點

Spring Framework 基于YAML資源裝載外部化配置

作者:NiceEleven

本篇我們來介紹一下Spring Framework 基于YAML資源裝載外部化配置。這裡我總結了以下内容:

* API 程式設計
	* org.springframework.beans.factory.config.YamlProcessor
		* org.springframework.beans.factory.config.YamlMapFactoryBean
		* org.springframework.beans.factory.config.YamlPropertiesFactoryBean           

接下來我們就通過實際案例一起來實踐一下,代碼如下:

user:
  id: 2
  name: eleven
  city: CHANGSHA

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="yamlMap" class="org.springframework.beans.factory.config.YamlMapFactoryBean" >
       <!-- 關聯 user.yaml 配置 -->
       <property name="resources" value="classpath:/META-INF/user.yaml" />
   </bean>
</beans>

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="yamlMap" class="org.springframework.beans.factory.config.YamlMapFactoryBean" >
       <!-- 關聯 user.yaml 配置 -->
       <property name="resources" value="classpath:/META-INF/user.yaml" />
   </bean>

</beans>

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.eleven.thinking.in.spring.configuration.metadata;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * YAML 格式的 {@link PropertySourceFactory} 實作
 *
 * @author <a href="mailto:[email protected]">eleven</a>
 * @since
 */
public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(resource.getResource());
        Properties yamlProperties = yamlPropertiesFactoryBean.getObject();
        return new PropertiesPropertySource(name, yamlProperties);
    }
}


/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.eleven.thinking.in.spring.configuration.metadata;

import org.eleven.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import java.util.Map;

/**
 * 基于 XML 資源的 YAML 外部化配置示例
 *
 * @author <a href="mailto:[email protected]">eleven</a>
 * @since
 */
public class XmlBasedYamlPropertySourceDemo {

    public static void main(String[] args) {
        // 建立 IoC 底層容器
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 建立 XML 資源的 BeanDefinitionReader
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        // 記載 XML 資源
        reader.loadBeanDefinitions("classpath:/META-INF/yaml-property-source-context.xml");
        // 擷取 Map YAML 對象
        Map<String, Object> yamlMap = beanFactory.getBean("yamlMap", Map.class);
        System.out.println(yamlMap);
    }
}           

繼續閱讀