Nearby lessons
2 of 10Spring Boot - Key Components
- Understand Spring Boot - Key Components
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring Boot - Key Components step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
Key Components of Spring Boot Framework
-----------------------------------------------------------
Spring Boot Framework has mainly four major Components.
1.Spring Boot Starters
2.Spring Boot AutoConfigurator
3.Spring Boot CLI
4.Spring Boot Actuator
1.Spring Boot Starters:
------------------------------
--> The main intention of Spring Boot Starter is to combine a group of common or related
dependencies into single dependencies.
--> If we want to prepare any web application with tomcat server and with Database
interaction then we have to add the following JArs minimum to the project library.
Spring beans jat file(sprinmg-beans-xx.jar)
Spring core Jar file(spring-core-xx.jar)
Spring context Jar file(spring-context-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)
Spring JDBC Jar file(spring-jdbc-xx.jar)
Spring ORM Jar file(spring-orm-xx.jar)
Spring Transaction Jar file(spring-transaction-xx.jar)
The above approach will increase no of dependencies in our build file, it will increase build
file size and it is very much difficult task to manage all these dependencies.
--> To overcome all the above problems, we have to use 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 jars files to
our build file, we need to add one and only one jar file like
"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.
Spring-boot-starter-web dependency will provide the following jars to our project library.
- spring-boot-starter
a.spring-boot
b.spring-boot-autoconfiogure
c.spring-boot-starter-logging
- spring-web
- spring-webmvc
- spring-boot-starter-tomcat
a. tomcat-embeded-core
b.tomcat-embeded-logging-juli
2.Spring Boot AutoConfigurator:
---------------------------------------------
In general, in spring based applications we have to provide lot of configuration details
either through XML files or through Annotations.
EX: Beans configurations and their dependencies
HandlerMapping Configuration
ViewResolver configurations
To avoid all the above configurations Spring Boot has provided an autoConfigurator. Spring
Boot Autoconfigurator will come automatically when we add "spring-boot-starter-web"
dependency in build file, it will resolve views and Viewresolvers in Spring web MVC
applications with out having Spring configuration file and if we use @SpringBootApplication
annotation then it will avoid the explicit utilization of @Configuration, @ComponentScan
and @EnableAutoConfiguration and provide them all these annotations to the Java Class
internally.
@SpringBootApplication = @Confioguration + @ComponentScan +
@EnableAutoConfiguration
3.Spring Boot CLI:
------------------------
--> Spring Boot CLI(Command Line Interface) is a Spring Boot software, it will provide very
good environment 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 AutoConfigurator components to resolve all dependencies and
execute the application.
--> Spring Boot Command Line Interface has provided "spring" command to execute
"Groovy" scripts from Command Prompt, that is, we have to prepare Controller classes in
".groovy" file.
Syntax: spring run HelloWorld.groovy
Where HelloWorld.groovy is like Javas HelloWorld.java file name.
--> When we run the above "spring" command on Command Line Interface then Spring
Boot will activate Groovyc[Groovy Compiler] and Groovy Grap to add all the
dependencies to project library and to execute and test Groovy script.
4.Spring Boot Actuator:
-------------------------------
--> Spring Boot Actuators are providing no of Production-Ready features like
- Spring Boot Actuator is providing environment to manage and monitor your application
by using HTTP endpoints or with JMX.
- Spring Boot Actuators are providing Auditing, health, and metrics automatically to your
application.
--> 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.
--> To activate Spring Boot Actuators we have to use the following dependecies in build file.
EX:
Key Components of Spring Boot Framework
Steps to Prepare Spring Boot Applications:
------------------------------------------------------------
Spring Boot Applications are prepared and executed in the following four ways.
- Using Simple MAVEN project in Eclipse IDE.
- STS IDE
- Spring Boot Initializer
- Spring Boot CLI[Command Line Interface]
- Using Simple MAVEN project in Eclipse IDE:
---------------------------------------------------------------
- Make Ready Setups for JDK1.8 and Eclipse IDE latest version.
- Create MAVEN Project in Eclipse IDE.
- Provide the required Dependencies in pom.xml file.
- Prepare Java Class With main() method and the required Spring Boot Annotations.
- Run Spring Boot Application.
- Make Ready Setups for JDK1.8 and Eclipse IDE latest version:
--------------------------------------------------------------------------------------
Already we have JDK and Eclipse in our system, no need to reinstall them seperately.
- Create MAVEN Project in Eclipse IDE:
--------------------------------------------------------
Key Components of Spring Boot Framework
--------------------------------------------------------------------------
open pom.xml and provide the following dependencies in pom.xml.
a)Provide Java-compiler-plugin in build path.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Right CLick on Project, Select Maven and Select Update Project and Click on OK toget JAVA8
version in lib.
b)Provide Spring Boot Starter Parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
c)Add Spring Boot Starter web in pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Observe all the dependent JARs under MAVEN Dependencies in MAVEN Project.
- Prepare Java Class With main() method and the required Spring Boot Annotations:
--------------------------------------------------------------------------------------------------------------------
package com.durgasoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
Key Components of Spring Boot Framework
Key Components of Spring Boot Framework
Where @SpringBootApplication annotation will provide the required @Component,
@ComposentScan and @EnableAutoConfigurator annotations.
Where SpringApplications provided run() method will start spring boot application.
If 8080 is busy in our system then Change Server port number from 8080 to 1111 [any
number] by providing properties file:
src/main/resources/application.properties
--------------------------------------
server.port = 1111
- Run Spring Boot Application:
-------------------------------------------
- Right Click on project
- Select "RunAs"
- Select "Java Application".
- Select "HelloWord"
Then the following output on Console.
| . ____ _ | __ _ _ |
|---|
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.1.RELEASE)
2019-10-06 22:02:48.309 INFO 14800 --- [ main] com.durgasoft.HelloWorld :
Starting HelloWorld on DESKTOP-LTICM7V with PID 14800
(D:\spring_boot_practice\helloapp\target\classes started by Nagoor in
D:\spring_boot_practice\helloapp)
2019-10-06 22:02:48.312 INFO 14800 --- [ main] com.durgasoft.HelloWorld :
No active profile set, falling back to default profiles: default
2019-10-06 22:02:48.396 INFO 14800 --- [ main]
ationConfigEmbeddedWebApplicationContext : Refreshing
org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplication
Context@44a3ec6b: startup date [Sun Oct 06 22:02:48 IST 2019]; root of context hierarchy
2019-10-06 22:02:49.587 INFO 14800 --- [ main]
trationDelegate$BeanPostProcessorChecker : Bean
'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type
[class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is
not eligible for getting processed by all BeanPostProcessors (for example: not eligible for
auto-proxying)
2019-10-06 22:02:49.686 INFO 14800 --- [ main]
trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible
for getting processed by all BeanPostProcessors (for example: not eligible for auto-
proxying)
2019-10-06 22:02:50.277 INFO 14800 --- [ main]
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 1111 (http)
2019-10-06 22:02:50.294 INFO 14800 --- [ main]
o.apache.catalina.core.StandardService : Starting service Tomcat
2019-10-06 22:02:50.295 INFO 14800 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2019-10-06 22:02:50.424 INFO 14800 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]
: Initializing Spring embedded WebApplicationContext
2019-10-06 22:02:50.424 INFO 14800 --- [ost-startStop-1] o.s.web.context.ContextLoader
: Root WebApplicationContext: initialization completed in 2033 ms
2019-10-06 22:02:50.635 INFO 14800 --- [ost-startStop-1]
o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-10-06 22:02:50.644 INFO 14800 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-10-06 22:02:50.645 INFO 14800 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-10-06 22:02:50.645 INFO 14800 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-10-06 22:02:50.645 INFO 14800 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-10-06 22:02:51.217 INFO 14800 --- [ main]
s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice:
org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplication
Context@44a3ec6b: startup date [Sun Oct 06 22:02:48 IST 2019]; root of context hierarchy
2019-10-06 22:02:51.387 INFO 14800 --- [ main]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public
org.springframework.http.ResponseEntity<java.util.Map<java.lang.String,
java.lang.Object>>
org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.
HttpServletRequest)
2019-10-06 22:02:51.388 INFO 14800 --- [ main]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}"
onto public org.springframework.web.servlet.ModelAndView
org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.
http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-10-06 22:02:51.511 INFO 14800 --- [ main]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler
of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-10-06 22:02:51.512 INFO 14800 --- [ main]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type
[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-10-06 22:02:51.618 INFO 14800 --- [ main]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto
handler of type [class
org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-10-06 22:02:51.810 INFO 14800 --- [ main] o.s.j.e.a.AnnotationMBeanExporter
: Registering beans for JMX exposure on startup
2019-10-06 22:02:51.937 INFO 14800 --- [ main]
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 1111 (http)
2019-10-06 22:02:51.942 INFO 14800 --- [ main] com.durgasoft.HelloWorld :
Started HelloWorld in 4.047 seconds (JVM running for 4.509)
Hello From First Spring boot Application
Note: If we want to access the same application through Web Client then we have to
prepare a Controller class , where we have to provide handler method with a particular
return value.
EX:
----
- Key ideas of Spring Boot - Key Components explained simply
- Ready-to-use code examples
- Exam-style questions at the end