main class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GiantApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(GiantApplication.class);
        application.addListeners(new Log4J2SpringEnvListener());
        application.run(args);
    }
}

The configuration file is bootstrap.yml,

The debugging results of idea found that when use nacos or spring cloud config

, the SpringApplication.run method was executed twice as shown in the figure:

the first:

SpringApplication.java

image-20201113115903351

Second : Log4J2SpringEnvListener is gone.

BootstrapApplicationListener.java

image-20201113135049598

SpringApplication.java

image-20201113115955251

What i need to Do something so that custom listener is called before LoggingApplicationListener. But custom defined Listener needs to be able to use the environment.

I used Google Translate and it may be difficult to understand,

Thank you for reading all this question!

Comment From: np7sky

it work for me; in src\main\resources\META-INF\spring.factories

org.springframework.context.ApplicationListener=\
com.xxx.xxx.common.spring.log4j2.Log4J2SpringEnvListener

But I don’t want to configure more locally, Is there any other way?

Comment From: spencergibb

This is an artifact of the legacy bootstrap system. https://github.com/spring-cloud/spring-cloud-commons/issues/737 There is no easy fix. Using the new spring.config.import in 2020.0 (aka ilford) is the way to avoid this problem.

Comment From: spring-cloud-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: np7sky

Hope we all have a bright future! Come on!

Comment From: spencergibb

As I said before, this is an artifact of bootstrap. There's no way to fix it. The new spring.config.import avoids this.

Comment From: np7sky

If it is not easy to fix, it can be added to the future plan. As for whether to close the problem, it depends on the long-term consideration of the spring cloud team. I have no other opinions