除了 MapPropertySource 外, 还有其他, 比如: CompositePropertySource

请求:
http://localhost:9999/acInitializer/first/map1-k1
可以取到, 因为 MapPropertySource 加入了;
http://localhost:9999/acInitializer/first/map2-k1
http://localhost:9999/acInitializer/first/map3-k1
可以是因为 CompositePropertySource

CompositePropertySource示例:

package com.niewj.fileman.initialized;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author weijun.nie
 * @Date 2020/4/27 8:00
 * @Version 1.0
 */
@Order(1)
public class FirstACInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        // 1. 获取到 Environment
        ConfigurableEnvironment environment = applicationContext.getEnvironment();

        // PropertySource: MapPropertySource
        Map<String, Object> map1 = new HashMap<>();
        map1.put("map1-k1", "map1_value1");
        MapPropertySource map1PropertySource = new MapPropertySource("firstInitializer-map1", map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("map2-k1", "map2_value1");
        MapPropertySource map2PropertySource = new MapPropertySource("firstInitializer-map2", map2);

        Map<String, Object> map3 = new HashMap<>();
        map2.put("map3-k1", "map3_value1");
        MapPropertySource map3PropertySource = new MapPropertySource("firstInitializer-map2", map2);

        // PropertySource: CompositePropertySource
        Set<PropertySource<Map<String, Object>>> set = new HashSet<>();
        set.add(map1PropertySource);
        set.add(map2PropertySource);
        set.add(map3PropertySource);
        CompositePropertySource cps = new CompositePropertySource("firstInitializer-set");
        cps.getPropertySources().addAll(set);

        // 3. 调用 environment获取属性源(PropertySources),
        //  将map的PropertySource 加入 environment: 并没有加入 map2 和 map3
        //  将set的PropertySource 加入 environment: set内是 PropertySource对象
        environment.getPropertySources().addLast(map1PropertySource);
        environment.getPropertySources().addLast(cps);

        System.out.println("......FirstInitializer[ApplicationContextInitializer]......");
    }
}

其他:

//复合实现,一个set内容纳PropertySource
CompositePropertySource (org.springframework.core.env)    
//命令行属性资源
CommandLinePropertySource (org.springframework.core.env)    
//map型资源
MapPropertySource (org.springframework.core.env)    
//通过注解注入的属性资源
AnnotationsPropertySource (org.springframework.boot.test.autoconfigure.properties)

其中 MapPropertySource 是使用较广的,他的实现有:

//通过Properties资源构建属性资源
PropertiesPropertySource (org.springframework.core.env)

//直接通过资源路径构建属性资源
ResourcePropertySource (org.springframework.core.io.support)
//测试使用的属性资源
MockPropertySource (org.springframework.mock.env)
//对系统环境转换成属性资源
SystemEnvironmentPropertySource (org.springframework.core.env)
//命令行参数封装属性
SimpleCommandLinePropertySource (org.springframework.core.env)

丰木
322 声望19 粉丝

遇见超乎想象的自己!