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.
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:
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:
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
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:
@Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }
@Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .addScripts('schema.sql', 'data.sql') .build(); }
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.
Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan 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.
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.
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
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:
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!!!
Spring Boot Related Topics
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…