I am trying to use a actuator on a different port like: management.server.port=8081 I also use a custom register viewResolver:

@Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.viewResolver(viewResolver()); }.

I get an error like this:

Cannot reinitialize with different application context: current one is [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3727f0ee, started on Fri Oct 22 16:30:10 EEST 2021, parent: org.springframework .context.annotation.AnnotationConfigApplicationContext @ 4525d1d3], passed-in one is [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5b96272e, started or started on E Fri Oct 22 16:30:34 .boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext @ 3727f0ee].

I am guessing that this error is being thrown because of the two applicationContext. ManagementContextAutoConfiguration.DifferentManagementContextConfiguration.onApplicationEvent() updates the context and the registration of the viewResolver fires again. Is there any way to get around this?

Comment From: wilkinsona

When you configure Actuator to run on a separate port there are two application contexts. The parent context contains the main application as it usually does, but the Actuator and all its infrastructure moves into a separate context that uses the main context as its parent. When Spring MVC is initialized in this child context, it looks for WebMvcConfigurer beans. In addition to any configurer beans in the Actuator's context, any beans in ancestor contexts are also found. This is standard Spring MVC behaviour and is out of Spring Boot's control.

The result of the parent-child context and Spring MVC's behaviour is that your configurer is called twice. You appear to be returning the same ViewResolver instance each time which is then resulting in the failure you have shared above. You should either return a different ViewResolver instance each time your configurer is called, or you could chose to only register the resolver on the first invocation.

Comment From: wilkinsona

I thought this rang a bell. It's a duplicate of https://github.com/spring-projects/spring-boot/issues/4929.