Auto-Configuration and Actuator

Learn about auto-configuration in Spring Boot, along with the Spring Boot Actuator.

Auto-configuration

Spring Boot considers the runtime of your application and automatically configures your application based on many factors, such as libraries on the classpath.

It follows the motto: “If everyone has to do it, then why does everyone have to do it?”

For example, to create an MVC web app, you need to add a configuration class, multiple dependencies, and configure a tomcat container. With Spring Boot, all you need to add is a dependency and a controller class, and it will automatically add tomcat.

Actuator

The Spring Boot Actuator allows you to see the insides of your application, including environment properties, mappings, metrics, and so much more. It exposes information using a set of rest endpoints and JMX MBeans. It even allows you to change settings at runtime (you can turn that off). This can be extremely useful.

To get started with Actuator, first add it to your dependencies:

compile('org.springframework.boot:spring-boot-starter-actuator')

All actuator endpoints are mapped to the /actuator/path. This allows you to secure these endpoints with one filter. By default in Spring 2, health and info are available and open to everyone. To enable other endpoints, you need to specify the additional configurations.

The status of the application can be obtained at actuator/health. This returns a simple JSON with a status value.

Information is at actuator/info.

For security, you may not want all of the actuator endpoints exposed to everyone in the world. Other actuator endpoints can be exposed using the following configuration (in your application.yml):

management:
   endpoints:
     web:
       exposure:
          include: '*'

There are too many endpoints available to list. Some of them include (with the /actuator prefix):

  • /beans/: Provides a list of all defined Spring beans.
  • /conditions/: Provides all of the logic Spring used to do auto-configuration. This can be useful for figuring out what’s going on “behind the curtains.”
  • /env/: All Spring properties
  • /loggers/: Gives a list of all logger configurations.
  • /httptrace/: Lists basic info about HTTP request traces.
  • /mappings/: Gives information about how your URLs are mapped to your controllers.
  • /metrics/:Gives detailed metrics about your application.

Many endpoints accept restful style JSON posts to actually change values at runtime. For example, you can post to /actuator/loggers/org.springframework.web/ with JSON {"configedLevel": "DEBUG"}, and it will immediately update that logging level to DEBUG.

Rest controller

Annotate a class with @RestController to create a restful controller. For example:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() { return "Hello world!"; }
}

By default, a jar-based Spring Boot “web” application uses an embedded Tomcat container.

“Using the embedded Tomcat container means that Tomcat is just another ‘bean,’ making configuration a lot easier. Tomcat can be configured using application.properties and other application configuration files.”

Devtools

Including Spring Boot’s dev tools can greatly improve your development life. If included, it enables automatic restart of your application when and if the code changes. Add the following to your dependencies:

compile('org.springframework.boot:spring-boot-devtools')

Don’t worry about removing the dependency for production. It is smart enough not to include itself in a final product, such as a jar or war. It’s only enabled when running in a development environment.

Conclusion

We’ve seen that Spring Boot enables developer productivity by removing the need for tons of boilerplate associated with building a Spring application. It uses conventions and “starter” dependencies in addition to plain-old Spring features like annotation scanning to greatly simplify developing applications. With dev tools, it even gives you dynamic reloading during development.

At the same time, it is very flexible and gives the developer a lot of agency in deciding what to include. Overtime, you can modify the configuration as needed.

Get hands-on with 1200+ tech skills courses.