How to change application profile properties before Spring container refresh

reason

At work, we often encounter situations where we need to write a public jar package to encapsulate other open source frameworks and provide some best practice configurations. However, some open source frameworks require configuration files. If a business project references a public jar package, you need to go to It's annoying to have a lot of things. The most convenient way to solve this problem is whether to add or modify the property source in the form of java code in the jar package, but it must be before the spring container is refreshed, because the referenced open source framework will be initialized when the container is refreshed.

Because I have studied springboot superficially before, I learned that springboot loads all SpringApplicationRunListeners from the spring.factories file during the startup process, and calls back all the monitoring methods registered by SpringApplicationRunListener during the container startup process. By default, only one RunListener here is EventPublishingRunListener, which indirectly publishes many ApplicationEvents to ApplicationListener (the ApplicationListener here is also found by scanning the spring.factories file, so in addition to registering SpringApplicationRunListener in spring.factories, you can also register ApplicationListener ), ApplicationListener, is an extension point provided by springboot, which can monitor various implementations directly by registering bean implementations, but it should be noted that it is not possible to monitor the events before the container is refreshed by ordinary registered beans (annotation method), the reason is very Simple, the container has not refreshed how to find the ApplicationListener. So it must be loaded in advance using spring.factories.

practice

The approach is simple. There are two ways, the first to register RunListener, the second to register ApplicationListener.

The first

first step

First create a new SpringApplicationRunListener, in order to avoid conflicts, set this order to 1, after the EventPublishingRunListener.

public class MyListener implements SpringApplicationRunListener, Ordered {

    public MyListener(SpringApplication application, String[] args) {


    }

    @Override
    public void starting() {

    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        MutablePropertySources m = environment.getPropertySources();
        Properties p = new Properties();
        p.put("test", "123");
		  //addFirst优先级是最高的,如果已经存在值,将会覆盖
         m.addFirst(new PropertiesPropertySource("mypop",p));
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {

    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {

    }

    @Override
    public void started(ConfigurableApplicationContext context) {

    }

    @Override
    public void running(ConfigurableApplicationContext context) {

    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {

    }

    @Override
    public int getOrder() {
        return 1;
    }
}

second step

Create a new spring.factories file in the META-INF folder, add a line to register the above SpringApplicationRunListener: org.springframework.boot.SpringApplicationRunListener=com.cman777.springc.sample.MyListener.

the second

first step


public class ConfigListener4RunSprboot implements ApplicationListener<ApplicationPreparedEvent> {
    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event) {
        ConfigurableEnvironment env = event.getApplicationContext().getEnvironment();
        MutablePropertySources m = env.getPropertySources();
        Properties p = new Properties();
        p.put("test", "123");
        //addFirst优先级是最高的,如果已经存在值,将会覆盖
        m.addFirst(new PropertiesPropertySource("mypop",p));
    }
}

second step

Create a new spring.factories file in the META-INF folder, add a line to register the above ApplicationListener: org.springframework.context.ApplicationListener=com.cman777.springc.sample.ConfigListener4RunSprboot.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324132945&siteId=291194637