1

Currently I have a legacy project that uses spring MVC. Now there is need to implement Spring boot actuator. So my questions are

  1. Can I implement Spring Actuator in my Spring MVC app without adding Spring boot
  2. Can I have both Spring boot and spring MVC in one single application. If Yes then how.

It would be great if someone can describe step by step implementation of it. I am using Eclipse, Gradle, Tomcat

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Divya Kamli
  • 31
  • 1
  • 3

1 Answers1

3

To answer your first question:

  1. yes You can use actuator in your spring mvc project. Add this to pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>Compatible version with mvc</version>
    </dependency>
    
  2. Add configuration below

    @Configuration
    @EnableWebMvc
    @Import({EndpointAutoConfiguration.class , PublicMetricsAutoConfiguration.class , HealthIndicatorAutoConfiguration.class
    })
    public class MyActuatorConfig {
    
        @Bean
        @Autowired
        public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
          return new EndpointHandlerMapping(endpoints);
        }
    
       @Bean
       @Autowired
       public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
          return new EndpointMvcAdapter(delegate);
      }
    }
    

2. Answer your second questions

Of course you can use Spring boot and spring MVC, just simple have below as parent to manage all dependencies version etc..

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

and spring-boot-starter-web as dependency.

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
kj007
  • 6,073
  • 4
  • 29
  • 47
  • For the first answer step 2 - Can you mention here the entry in spring servlet. i.e what values to be passed to make autowire work properly – Divya Kamli Oct 29 '18 at 13:58
  • after configuration /actuator end point should be available and same for metrics /metrics – kj007 Oct 29 '18 at 14:12
  • But how to configure? i.e. I do not understand the step 2. Can you describe it in detail Because since it has @Autowired annotations where should we inject those dependency and how? I am stuck at that point. Thanks in advance – Divya Kamli Oct 29 '18 at 15:55
  • After creating this configuration class, you will have access of actuator end point which you can call by their end points to get info about metrics, etc, if you want to customize info default end pint returns then please have a look actuator documentation like if EndpointHandlerMapping is available as bean. This is sufficient to configure actuator. – kj007 Oct 29 '18 at 16:10
  • Classes EndpointAutoConfiguration, PublicMetricsAutoConfiguration, HealthIndicatorAutoConfiguration are not getting resolved with only spring-boot-actuator dependecy. I think it requires spring boot for that – Divya Kamli Oct 29 '18 at 18:33
  • It worked for step 1 with this adjusments: I used version 1.2.3.RELEASE for spring-boot-actuator for a 4.3.12.RELEASE spring-webmvc, and I changed the class of ActuatorConfig for Import({EndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class }) and add this Bean Autowired public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate) { return new HealthMvcEndpoint(delegate); } – Thiago Cavalcanti Feb 15 '22 at 18:56