Spring boot health check on existing webapp

If smb is looking for a configuration of Spring Boot Actuator 2.x in an existing Spring MVC project, this is a configuration which worked for me

@Configuration
@Import({
        EndpointAutoConfiguration.class,
        HealthIndicatorAutoConfiguration.class,

        InfoEndpointAutoConfiguration.class,
        HealthEndpointAutoConfiguration.class,

        WebEndpointAutoConfiguration.class,
        ServletManagementContextAutoConfiguration.class,
        ManagementContextAutoConfiguration.class,
})
@EnableConfigurationProperties(CorsEndpointProperties.class)
class ActuatorConfiguration {

    @Bean //taken from WebMvcEndpointManagementContextConfiguration.class
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                                                                         ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,
                                                                         EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
                                                                         WebEndpointProperties webEndpointProperties) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        EndpointMapping endpointMapping = new EndpointMapping(webEndpointProperties.getBasePath());
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
                corsProperties.toCorsConfiguration(),
                new EndpointLinksResolver(allEndpoints, webEndpointProperties.getBasePath()));
    }

    @Bean
    DispatcherServletPath dispatcherServletPath() {
        return () -> "/";
    }

}

I did include

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator-autoconfigure</artifactId>
        <version>2.1.18.RELEASE</version>
    </dependency>

for compatibility with the baseline Spring version I've been using (5.1.19.RELEASE)


I figured it out myself. Instead of spring-boot-starter-actuator I am including spring-boot-actuator. And I don't need to initialize the application using @SpringBootApplication. Instead now I just import the auto-config classes that are required. So the config class now looks like this

@Configuration
@ComponentScan(basePackages = { "org.example" })
@Import({MyApplicationContext.class, EndpointWebMvcAutoConfiguration.class, 
  ManagementServerPropertiesAutoConfiguration.class, EndpointAutoConfiguration.class, 
  HealthIndicatorAutoConfiguration.class})
@PropertySource("classpath:app.properties")
@EnableWebMvc
public class MyWebApplicationContext {
...
}

EndpointWebMvcAutoConfiguration depends on ManagementServerProperties hence had to import it. This seems to be the bare minimum configuration for me. Let me know if there is any better alternative


First of all I'd like tell that sidgate's answer is correct. But maybe somebody need to enable all metrics or choose different set of metrics.

You still can use spring-boot-starter-actuator instead of spring-boot-actuator

For enabling all metrics

@org.springframework.context.annotation.Configuration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
@org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration
public class MyApplicationContext {
}

where @EnableAutoConfiguration

org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.InfoContributorAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.JolokiaAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricsDropwizardAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricsChannelAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricExportAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration.class

and @ManagementContextConfiguration

org.springframework.boot.actuate.autoconfigure.EndpointWebMvcManagementContextConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration

enables Spring Boot Configurations

For enabling certain management endpoints you could import endpoint's configurations directly as sidgate's already explained

@Configuration
@Import({
org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration.class,

...

})
public class MyApplicationContext {
}

P.S. Of course Spring Application Context and Properties initialization could be configured as always via annotations or xml configuration.