Essentials and Key Components of Spring Boot

Hello friends welcome for this tutorial. In line of Spring Boot tutorials we have already discussed "Introduction to Spring Boot - A Spring Boot Complete Guide" here about Spring Boot basics. Now we will discuss about "What are the essentials components of Spring Boot".

Essentials and Internals of Spring Boot:

Spring Boot gives us a great deal of magic to Spring application development. But there are four core essentials or key components that it performs:
  • Spring Boot Starters- We just tell Spring Boot what kind of functionality we need; now it is responsibility of Spring Boot so that it will ensure that the libraries needed are added to the build.
  • Spring Boot AutoConfigurator- Spring Boot can automatically provide configuration for application functionality common to many Spring applications.
  • Spring Boot CLI- This optional feature of Spring Boot lets you write complete applications with just application code, but no need for a traditional project build.
  • Spring Boot Actuator- Gives us insight of application into what’s going on inside of a running Spring Boot application.
Each of these key components serves to simplify Spring application development in its own way. There are two ways for creating Spring Boot application or help to create spring boot application.
  • Spring Initializr- To quick start new Spring Boot projects, we can use “Spring Initializr” web interface. Spring Initializr URL:
  • Spring Boot IDEs- We has many Spring Boot IDEs like Eclipse IDE, IntelliJ IDEA, Spring STS Suite etc.

1. Spring Boot Starters
When we start to working with a spring project then it can be challenging to add dependencies to a project’s build. What libraries do you need? What are its group and artifact? Which version do you need? Will that version play well with other dependencies in the same project?

Spring Boot Starters is one of the major key features or components of Spring Boot. Spring Boot offers help with project dependency management by way of starter dependencies. The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies into single dependencies.

For example, suppose that we’re going to build a REST API with Spring MVC that
works with JSON resource representations. To accomplish all of this, you’ll need (at minimum) the following four dependencies in your Maven or Gradle build:

  • org.springframework:spring-core
  • org.springframework:spring-web
  • org.springframework:spring-webmvc
  • com.fasterxml.jackson.core:jackson-databind

Additionally, you want to apply declarative validation per the JSR-303 specification and serve the application using an embedded Tomcat server. Then we have added four more dependencies as below:

  • org.hibernate:hibernate-validator
  • org.apache.tomcat.embed:tomcat-embed-core
  • org.apache.tomcat.embed:tomcat-embed-el
  • org.apache.tomcat.embed:tomcat-embed-logging-juli

It is very tedious and cumbersome tasks for a Developer. And also it increases our build file size. What is the solution to avoid this much dependencies definitions in our build files? The solution is Spring Boot Starter component.

On the other hand, we are using Spring Boot Starter Component; it combines all related jars into single jar file so that we can add only one jar file dependency to our build files. If we were to take advantage of Spring Boot starter dependencies, We could simply add the Spring Boot “web” starter (org.springframework.boot:spring-boot-starter-web) as a build dependency. This single dependency will transitively pull in all of those other dependencies so you don’t have to ask for them all.
One thing we have to notice that by adding the “web” starter to our build, we’re specifying a type of functionality that your application needs. Here our app is a web application, so we add the “web” starter. Likewise, if our application will use JPA persistence, then we can add the “jpa” starter. If it needs security, we can add the “security” starter. In short, we no longer need to think about what libraries we’ll need to support certain functionality; we simply ask for that functionality by way of the pertinent starter dependency. Also note that Spring Boot’s starter dependencies free us from worrying about which versions of these libraries we need.

Major Advantages of Spring Boot Starter

  • Spring Boot Starter reduces defining many dependencies simplify project build dependencies.
  • Spring Boot Starter simplifies project build dependencies.
  • Spring Boot Starter sync version compatibilities of dependencies.

2. Spring Boot AutoConfigurator
There is one of them important key component of Spring Boot is Spring Boot AutoConfigurator. In any given Spring application’s source code, we’ll find either Java configuration or XML configuration (or both) that enables certain supporting features and functionality for the application that is why it requires lot of configuration (either Java configuration or XML configuration).

The main responsibility of Spring Boot AutoConfigurator is to reduce the Spring Configuration. If we develop Spring applications in Spring Boot, then We don’t need to define single XML configuration and almost no or minimal Annotation configuration. Spring Boot AutoConfigurator component will take care of providing that information.

For example, if we’ve ever written an application that accesses a relational database with JDBC, we’ve probably configured Spring’s JdbcTemplate as a bean in the Spring application context. I’ll bet the configuration looked a lot like this:
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
This very simple bean declaration creates an instance of JdbcTemplate, injecting it with its one dependency, a DataSource. Of course, that means that we’ll also need to configure a DataSource bean so that the dependency will be met. To complete this configuration scenario, suppose that we were to configure an embedded H2 database as the DataSource bean:
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.addScripts('schema.sql', 'data.sql')

This bean configuration method creates an embedded database, specifying two SQL scripts to execute on the embedded database. The build() method returns a Data-Source that references the embedded database.

Neither of these two bean configuration methods is terribly complex or lengthy. But they represent just a fraction of the configuration in a typical Spring application. Moreover, there are countless Spring applications that will have these exact same methods. Any application that needs an embedded database and a JdbcTemplate will need those methods. In short, it’s boilerplate configuration.

If it’s so common, then why should you have to write it? The solution to this problem is Spring Boot AutoConfigurator.

And also Spring Boot reduces defining of Annotation configuration. If we use @SpringBootApplication annotation at class level, then Spring Boot AutoConfigurator will automatically add all required annotations to Java Class ByteCode.
public @interface SpringBootApplication

That is, @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiration.

Note- in Shot, Spring Boot Starter reduces build’s dependencies and Spring Boot AutoConfigurator reduces the Spring Configuration.
3. Spring Boot CLI
In addition to auto-configuration and starter dependencies, Spring Boot also offers an intriguing new way to quickly write Spring applications. Spring Boot’s CLI leverages starter dependencies and auto-configuration to let us focus on writing code. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all dependencies and execute the application.

Spring Boot’s CLI is an optional piece of Spring Boot’s power. Although it provides tremendous power and simplicity for Spring development, it also introduces a rather unconventional development model.

Spring Boot CLI has introduced a new “spring” command to execute Groovy Scripts from command prompt.
spring run app.groovy
Spring Boot CLI component requires many steps like CLI Installation, CLI Setup, Develop simple Spring Boot application and test it.

4. Spring Boot Actuator
The final key component of the Spring Boot is the Actuator. Where the other parts of Spring Boot simplify Spring development, the Actuator instead offers the ability to inspect the internals of your application at runtime. Spring Boot Actuator components gives many features, but two major features are

  • Providing Management EndPoints to Spring Boot Applications.
  • Spring Boot Applications Metrics.

The Actuator exposes this information in two ways: via web endpoints or via a shell interface.

We inspect the inner workings of your application with Actuator as below:

  • What beans have been configured in the Spring application context
  • What decisions were made by Spring Boot’s auto-configuration
  • What environment variables, system properties, configuration properties, and command-line arguments are available to your application
  • The current state of the threads in and supporting your application
  • A trace of recent HTTP requests handled by your application
  • Various metrics pertaining to memory usage, garbage collection, web requests, and data source usage

When we run our Spring Boot Web Application using CLI, Spring Boot Actuator automatically provides hostname as “localhost” and default port number as “8080”. We can access this application using “http://localhost:8080/” end point.

We actually use HTTP Request methods like GET and POST to represent Management EndPoints using Spring Boot Actuator.

Congratulation!!! today we have learned about key components of Spring Boot. That’s it about Spring Components and Internals. We will discuss about these components in details with some Spring Boot examples in coming posts.

Happy Spring Boot Learning!!!

No comments:

Post a Comment