SpringBoot2.x loaded application.properties means other resource files

1, @ PropertySource annotation mode

Custom resource files custom.properties
Here Insert Picture Description

@Configuration
@PropertySource(value = "custom.properties")
public class EnvironmentTest implements CommandLineRunner{

    @Value("${test.param1:}")
    private String param1;

    @Override
    public void run(String... strings) throws Exception {
        System.out.println("read test.param1: "+param1);
    }
}

2, implement the interface EnvironmentPostProcessor

public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private final Properties properties = new Properties();
    /**
     * The Profiles.
     */
    private String[] profiles = {
            "custom.properties",
    };

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
        for (String profile : profiles) {
            Resource resource = new ClassPathResource(profile);
            configurableEnvironment.getPropertySources().addLast(loadProfiles(resource));
        }
    }

    private PropertySource<?> loadProfiles(Resource resource) {
        if (!resource.exists()) {
            throw new IllegalArgumentException("file" + resource + "not exist");
        }
        try {
            properties.load(resource.getInputStream());
            return new PropertiesPropertySource(resource.getFilename(), properties);
        } catch (IOException ex) {
            throw new IllegalStateException("load resource exception" + resource, ex);
        }
    }

}

spring.factories add the EnvironmentPostProcessor
Here Insert Picture Description

3, added directly to the environment in

@Configuration
public class CustomPropertiesLoadConfig {

    @Autowired
    private Environment environment;

    @PostConstruct
    public void load(){
        if(environment instanceof StandardEnvironment){
            MutablePropertySources propertySources = ((StandardEnvironment) environment).getPropertySources();
            Resource resource = new ClassPathResource("custom.properties");
            Properties properties = new Properties();
            try {
                properties.load(resource.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }

            propertySources.addFirst(new PropertiesPropertySource("customPorperties", properties));
        }
    }
}

Get tested

@Configuration
public class EnvironmentTest implements CommandLineRunner {

    @Value("${test.param1:}")
    private String param1;

    @Override
    public void run(String... strings) throws Exception {

        System.out.println("read test.param1: "+ param1);
    }
}

However, this approach should pay attention to load the configuration of the bean to give priority to get the configuration of the bean is loaded.

4、spring.profile.include

The configuration file was renamed application-custom.properties
direct spring.profile.include to increase in application.properites in.

spring.profiles.include=custom
Published 28 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_36142042/article/details/105038535