Examining Spring Boot Essentials

By now, we doved into the ways of creating Spring Boot applications. But, How does the Spring Boot brings magic to Spring application development?

There are four tricks that simplifies the Spring application development with Spring Boot:

  • Automatic configuration—Automatically provide configuration for application functionality common to many Spring applications.
  • Starter dependencies—You tell Spring Boot what kind of functionality you need, and it will ensure that the libraries needed are added to the build.
  • The command-line interface—This optional feature of Spring Boot lets you write complete applications with just application code, but no need for a traditional project build.
  • The Actuator—Gives you insight into what’s going on inside of a running Spring Boot application.

Let’s take a look at these four.

Automatic Configuration:

For example, if you’ve ever written an application that accesses a relational database with JDBC, you’ve probably configured Spring’s JdbcTemplate as a bean in the Spring application context. The configuration looked a lot like this:

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

In fact, this  simple bean declaration creates an instance of JdbcTemplate, injects its dependency DataSource.

Of course, we need to configure a DataSource bean so that the dependency will be met. To do this, we need to configure DataSource bean as below. Here we assume the database is H2 database.

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScripts('schema.sql', 'data.sql')
.build();
}

The above 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.

The above two bean configuration are really quite small. But, in realworld scenario, we might see lengthy or complex configuration.

Spring boot detects the available libraries in application classpath and automatically configures. For example, If Spring Boot detects that you have the H2 database library in your application’s classpath, it will automatically configure an embedded H2 database.

There’s a lot more to Spring Boot auto-configuration than embedded databases and JdbcTemplate. There are several dozen ways that Spring Boot can take the burden of configuration off your hands, including auto-configuration for the Java Persistence API (JPA), Thymeleaf templates, security, and Spring MVC.

Starter Dependencies:

The challenging part of Spring project is to add the dependencies.

What libraries do we need?

What are its group and artifact?

Which version do we need? Is this version play well with other dependencies in the same project?

Spring Boot solves all these problems with starter dependencies. For example, suppose we are going to build a REST API with Spring MVC that works with JSON resource representations. And we want to apply declarative validation and serve the application using an emdedded Tomcat server.

To accomplish this, we will need minimum eight dependencies as show below

  • org.springframework:spring-core
  • org.springframework:spring-web
  • org.springframework:spring-webmvc
  • com.fasterxml.jackson.core:jackson-databind
  • 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

By using Spring Boot starter dependencies, we can simply add the Spring Boot “web” starter (org.springframework.boot:spring-boot-starter-web) as build dependency. This single dependency will transitively pull in all of those other dependencies automatically.

Likewise, If we need JPA persistance, then we need to add “jpa” starter. If we need security, we need to add “security” starter.

The command-line Interface (CLI):

Spring Boot’s CLI leverages starter dependencies and auto-configuration to let us focus on writing code. 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. We will see how it helps in microservices in my later posts.

The Actuator:

All the above parts of Spring Boot simplifies Spring development, the actuator offers the ability to inspect the internals of our application at runtime like..

  • 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

We will see more details about the actuator and how it helps in microservices in my later posts.

harinathk
 

Click Here to Leave a Comment Below 0 comments