In my previous post “Introduction to Spring Boot”, we have discussed about Spring Boot basics. Now we will discuss about “What are the main components of Spring Boot” and “How Spring Boot works under-the-hood”.
Key Components of Spring Boot Framework
Spring Boot Framework has mainly four major Components.
- Spring Boot Starters
- Spring Boot AutoConfigurator
- Spring Boot CLI
- Spring Boot Actuator
NOTE:- In addition to these four major components, there are two more Spring Boot components:
- Spring Initilizr
- Spring Boot IDEs
To quick start new Spring Boot projects, we can use “Spring Initializr” web interface. Spring Initializr URL: https://start.spring.io. We have many Spring Boot IDEs like Eclipse IDE, IntelliJ IDEA, Spring STS Suite etc. We will discuss these two components in coming posts. Now we will discuss these Spring Boot four components one by one in detail.
Spring Boot Starter
Spring Boot Starters is one of the major key features or components of Spring Boot Framework. The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies into single dependencies. We will explore this statement in detail with one example. For instance, we would like to develop a Spring WebApplication with Tomcat WebServer. Then we need to add the following minimal jar dependencies in your Maven’s pom.xml file or Gradle’s build.gradle file
- Spring core Jar file(spring-core-xx.jar)
- Spring Web Jar file(spring-web-xx.jar)
- Spring Web MVC Jar file(spring-webmvc-xx.jar)
- Servlet Jar file(servlet-xx.jar)
If we want to add some database stuff, then we need to add database related jars like Spring JDBC jar file, Spring ORM jar files,Spring Transaction Jar file etc.
- Spring JDBC Jar file(spring-jdbc-xx.jar)
- Spring ORM Jar file(spring-orm-xx.jar)
- Spring Transaction Jar file(spring-transaction-xx.jar)
We need to define lot of dependencies in our build files. 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. Spring Boot Starter component combines all related jars into single jar file so that we can add only jar file dependency to our build files. Instead of adding above 4 jars files to our build file, we need to add one and only one jar file: “spring-boot-starter-web” jar file. When we add “spring-boot-starter-web” jar file dependency to our build file, then Spring Boot Framework will automatically download all required jars and add to our project classpath. In the same way, “spring-boot-starter-logging” jar file loads all it’s dependency jars like “jcl-over-slf4j, jul-to-slf4j,log4j-over-slf4j, logback-classic” to our project classpath.
Major Advantages of Spring Boot Starter
- Spring Boot Starter reduces defining many dependencies simplify project build dependencies.
- Spring Boot Starter simplifies project build dependencies.
That’s it about Spring Boot Starter component. We will discuss some more details with some Spring Boot examples in coming posts.
Spring Boot AutoConfigurator
Another important key component of Spring Boot Framework is Spring Boot AutoConfigurator. Most of the Spring IO Platform (Spring Framework) Critics opinion is that "To develop a Spring-based application requires lot of configuration (Either XML Configuration of Annotation Configuration). Then how to solve this problem. The solution to this problem is Spring Boot AutoConfigurator. The main responsibility of Spring Boot AutoConfigurator is to reduce the Spring Configuration. If we develop Spring applications in Spring Boot,then We dont need to define single XML configuration and almost no or minimal Annotation configuration. Spring Boot AutoConfigurator component will take care of providing those information. For instance, if we want to declare a Spring MVC application using Spring IO Platform, then we need to define lot of XML Configuration like views, view resolvers etc. But if we use Spring Boot Framework, then we dont need to define those XML Configuration. Spring Boot AutoConfigurator will take of this. If we use “spring-boot-starter-web” jar file in our project build file, then Spring Boot AutoConfigurator will resolve views, view resolvers etc. automatically. 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. If we go through Spring Boot Documentation, we can find the following definition for @SpringBootApplication.
@Target(value=TYPE)@Retention(value=RUNTIME)@Documented@Inherited@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface SpringBootApplication
That is, @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiration. That’s it about Spring Boot AutoConfigurate component. We will discuss some more details with some Spring Boot examples in coming posts. NOTE:-
- In simple words, Spring Boot Starter reduces build’s dependencies and Spring Boot AutoConfigurator reduces the Spring Configuration.
- As we discussed that Spring Boot Starter has a dependency on Spring Boot AutoConfigurator, Spring Boot Starter triggers Spring Boot AutoConfigurator automatically.
Spring Boot CLI
Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test Spring Boot applications from command prompt. 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. We can run even Spring Web Applications with simple Spring Boot CLI Commands. Spring Boot CLI has introduced a new “spring” command to execute Groovy Scripts from command prompt. spring command example:
spring run HelloWorld.groovy
Here HelloWorld.groovy is a Groovy script FileName. Like Java source file names have *.java extension, Groovy script files have *.groovy extension. “spring” command executes HelloWorld.groovy and produces output. Spring Boot CLI component requires many steps like CLI Installation, CLI Setup, Develop simple Spring Boot application and test it. So we are going to dedicate another post to discuss it in details with some Spring Boot Examples. Please refer my next post on Spring Boot CLI.
Spring Boot Actuator
Spring Boot Actuator components gives many features, but two major features are
- Providing Management EndPoints to Spring Boot Applications.
- Spring Boot Applications Metrics.
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 “https://localhost:8080/” end point. We actually use HTTP Request methods like GET and POST to represent Management EndPoints using Spring Boot Actuator. We will discuss some more details about Spring Boot Actuator in coming posts.
Internals of Spring Boot Framework
It’s always recommended to understand how Spring Boot Framework reduces build’s dependencies,Spring Configuration, etc. How Spring Boot works under-the-hood. If you are familiar with Groovy Programming language, then you know most of the stuff. In Groovy, we don’t need to add some some imports and no need to add some dependencies to Groovy project. When we compile Groovy scripts using Groovy Compiler(groovyc), it will automatically adds all default import statements then compile it. In the same way, Groovy Programming language contains a JAR Dependency Resolver to resolve and add all required jar files to Groovy Project classpath. Spring Boot Framework internally uses Groovy to add some defaults like Default import statements, Application main() method etc. When we run Groovy Scripts from CLI Command prompt, it uses this main() method to run the Spring Boot Application.
Grape is an Embedded Dependency Resolution engine. Grape is a JAR Dependency Manager embedded into Groovy. Grape lets us quickly add maven repository dependencies to our project classpath to reduce build file definitions. Spring Boot Framework programming model is mainly inspired by Groovy Programming model. Spring Boot Framework internally depends on these two major components: Groovy and Grape. You can refer Grape documentation https://docs.groovy-lang.org/latest/html/documentation/grape.html for more details. That’s it about Spring Components and Internals. We will discuss about these components in details with some Spring Boot examples in coming posts. If you have any queries, please drop me a comment.
What are the 3 main concepts spring boot provides when it starts your application? ›
With simple concepts like Dispatcher Servlet, ModelAndView, and View Resolver, it makes it easy to develop web applications.What is the key feature of Spring Framework? ›
The Spring Data Access Framework directly integrates with the Transaction Management Framework with support for messaging and caching. This enables developers to create feature-rich transactional systems that span across the applications without depending on EJB or JTA.What are the core concepts in spring boot? ›
Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation. For example, if MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.What are the different types of component in spring? ›
Spring provides four different types of auto component scan annotations, they are @Component , @Service , @Repository and @Controller . Technically, there is no difference between them, but every auto component scan annotation should be used for a special purpose and within the defined layer.What are the most important module of spring boot? ›
The application module is the main module of the project. It contains the application class in which the main method is defined that is necessary to run the Spring Boot Application. It also contains application configuration properties, Controller, views, and resources.What are the four layers in spring boot? ›
What are the four layers in Spring Boot? Four layers in Spring Boot are Presentation layer, Business layer, Persistence layer, and Database layer. Presentation layer handles the HTTP request, translates it to objects and passes it to the Business layer, which then calls the Persistence layer to update/fetch data.How does Spring Framework work internally? ›
Spring does not generate any code automatically and not using any xml configuration file . so spring uses internally pragmatically configuration done by spring boot developer that are provided by jar. To Enable preconfigured jars we just need to define dependency in pom. xml file.How do you explain spring boot project in interview? ›
- What annotations are used to create an Interceptor? ...
- What is a Swagger in Spring Boot? ...
- What are Profiles in Spring Boot? ...
- What differentiates Spring Data JPA and Hibernate? ...
- How are the @RestController and @Controller Annotation different?
The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, and Test, as shown in the following diagram.What are the core modules of Spring Framework? ›
- Data access/Integration.
- Web (MVC/Remoting)
- AOP (Aspect-oriented programming)
- Core container.
What are the modules of spring boot? ›
The Spring framework comprises of many modules such as core, beans, context, expression language, AOP, Aspects, Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts etc.How many components are in a spring boot? ›
Spring Boot Framework has mainly four major components. Spring Boot Starters: The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies into single dependencies.What is spring boot structure? ›
It is used to create stand-alone, production-grade Spring Based Applications with minimum efforts. It is developed on top of the core Spring Framework. Spring Boot follows a layered architecture in which each layer communicates with the layer directly below or above (hierarchical structure) it.What is REST API in Spring Boot? ›
The REST application follows the REST architectural approach. We use the REST application for developing and designing networked applications. It generates the HTTP request that performs CRUD operations on the data. Usually, it returns data in JSON or XML format.What is the difference between @beans and @component? ›
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.What is difference between @component and @bean and @service? ›
@Component is a generic stereotype for any Spring-managed component or bean. @Repository is a stereotype for the persistence layer. @Service is a stereotype for the service layer. @Controller is a stereotype for the presentation layer (spring-MVC).What does @autowired do in Spring? ›
Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.What are the three main annotations in spring boot? ›
The “@SpringBootApplication” annotation triggers auto-configuration and component scanning. It combines three annotations: @Configuration, @ComponentScan, and @EnabeAutoConfiguration.
Spring Boot helps developers create applications that just run. Specifically, it lets you create standalone applications that run on their own, without relying on an external web server, by embedding a web server such as Tomcat or Netty into your app during the initialization process.What is the primary goal of spring boot? ›
Main Goal of Spring Boot:
The main goal of Spring Boot Framework is to reduce Development, Unit Test and Integration Test time and to ease the development of Production ready web applications very easily compared to existing Spring Framework, which really takes more time.
What is the main class in spring boot? ›
A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.What is maven in spring boot? ›
The Spring Boot Maven Plugin provides Spring Boot support in Apache Maven. It allows you to package executable jar or war archives, run Spring Boot applications, generate build information and start your Spring Boot application prior to running integration tests.What is difference between spring boot and Spring Framework? ›
Spring framework helps to create a loosely coupled application. Spring Boot helps to create a stand-alone application. In the Spring framework to test the Spring Project, we need to set up the servers explicitly. Spring Boot offers built-in or embedded servers such as Tomcat and jetty.What are the spring boot annotations? ›
Spring Boot Annotations are a form of metadata that provides data about a program that is not a part of the program itself. They do not have any direct effect on the operation of the code they annotate. Spring Boot Annotations do not use XML and instead use the convention over configuration principle.What is JPA in spring boot? ›
JPA is a specification which specifies how to access, manage and persist information/data between java objects and relational databases. It provides a standard approach for ORM, Object Relational Mapping. Spring Boot provides a seemless integration with JPA.What is Microservices in spring boot? ›
Microservices are a software architectural style in which a large application is built as a collection of small, independent services that communicate with each other over a network. Each service is a self-contained unit of functionality that can be developed, tested, and deployed independently of the other services.What is Spring Framework summary? ›
The Spring Framework (Spring) is an open-source application framework that provides infrastructure support for developing Java applications. One of the most popular Java Enterprise Edition (Java EE) frameworks, Spring helps developers create high performing applications using plain old Java objects (POJOs).Which of the following is a component of the Spring Framework? ›
The Spring Core Container is the main component of the Spring framework, which is responsible for managing the application's beans and their dependencies. A32. B is correct. The BeanFactory in Spring is an interface that provides a simple way to retrieve bean instances.What are framework modules? ›
A framework is another specialized kind of program module which contains various pre-desinged functionality of code. A framework can be deployed in a form of one or several libraries.Is spring boot a module or framework? ›
Spring Boot is a module of the Spring framework. For building stand-alone applications with minimal or zero configurations, Spring boot is a good option. RESTFUL services or simple Spring-based applications can be built using Spring boot.
How do you test spring boot components? ›
- Overview. ...
- Project Setup. ...
- Maven Dependencies. ...
- Integration Testing With @SpringBootTest. ...
- Test Configuration With @TestConfiguration. ...
- Mocking With @MockBean. ...
- Integration Testing With @DataJpaTest. ...
- Unit Testing With @WebMvcTest.
@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.What are the main components of Spring Batch architecture? ›
As depicted in the figure, the architecture contains three main components namely, Application, Batch Core, and Batch Infrastructure. Application − This component contains all the jobs and the code we write using the Spring Batch framework.What is POM XML in Spring boot? ›
xml. The full form of POM is Project Object Model. It is a fundamental unit of work in Maven. pom is a XML file that contains information about the project and configuration details used by Maven to build the project.What is hibernate in Spring boot? ›
Hibernate is an implementation of JPA. It is an ORM tool to persist java objects into the relational databases. Package. JPA uses javax. persistence package.What type of API is REST? ›
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.What is the structure of spring boot? ›
Spring Boot follows a layered architecture in which each layer communicates with the layer directly below or above (hierarchical structure) it.What are different modules in spring boot? ›
The Spring framework comprises of many modules such as core, beans, context, expression language, AOP, Aspects, Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts etc.How does Spring Batch works internally? ›
Spring Batch follows the traditional batch architecture where a job repository does the work of scheduling and interacting with the job. A job can have more than one step. And every step typically follows the sequence of reading data, processing it and writing it.How many layers are there in Spring Boot? ›
There are four layers in Spring Boot are as follows: Presentation Layer. Business Layer. Persistence Layer.
How do you explain a Spring Boot project? ›
Spring Boot helps developers create applications that just run. Specifically, it lets you create standalone applications that run on their own, without relying on an external web server, by embedding a web server such as Tomcat or Netty into your app during the initialization process.