Springboot2 initializer: mappropertysource and others

Time:2020-12-2

exceptMapPropertySourceBesides, there are others, such as:CompositePropertySource

Request:
http://localhost:9999/acInitializer/first/map1-k1
You can get it becauseMapPropertySourceAdded;
http://localhost:9999/acInitializer/first/map2-k1
http://localhost:9999/acInitializer/first/map3-k1
It can be becauseCompositePropertySource

CompositePropertySourceExample:

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. Get the 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);

        //To get the source property 3,
        //Add the propertysource of map to environment: MAP2 and map3 are not added
        //Add propertysource of set to environment: set is propertysource object
        environment.getPropertySources().addLast(map1PropertySource);
        environment.getPropertySources().addLast(cps);

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

other:

//Composite implementation, a set content contains propertysource
CompositePropertySource (org.springframework.core.env)    
//Command line properties resource
CommandLinePropertySource (org.springframework.core.env)    
//Map Resources
MapPropertySource (org.springframework.core.env)    
//Attribute resources injected through annotations
AnnotationsPropertySource (org.springframework.boot.test.autoconfigure.properties)

Mappropertysource is widely used. Its implementation includes:

//Building property resources through properties resources
PropertiesPropertySource (org.springframework.core.env)

//Building attribute resources directly through resource path
ResourcePropertySource (org.springframework.core.io.support)
//Property resources used by the test
MockPropertySource (org.springframework.mock.env)
//Convert system environment to attribute resource
SystemEnvironmentPropertySource (org.springframework.core.env)
//Command line parameters encapsulate properties
SimpleCommandLinePropertySource (org.springframework.core.env)