Before entering into spring boot just a brief summary about spring
What is Spring??
Spring is a high performance, lightweight, non-invasive, easily testable application framework. Using spring we can develop all layer implementations unlike struts (struts framework supports only developing web applications).
In one line we can say spring is the end to end application development framework.
The core concept of spring framework is dependency injection. By dependency injection, we can achieve loose coupling.
What does it do??
Spring approach is developing enterprise Java applications by using POJOs. Because of using POJOs no need of application server we can use the servlet container.
There are so many enterprise Java applications, all these applications having some similarities. When we write a business service to solve business problem spring handle all those things like connecting to the database, establishing transaction service and so on…Spring provides a template for this to build these applications.
The flexibility with the spring is we can concentrate only building on business services and spring handle rest of the common things like connecting to the database, running queries and handling HTTP requests…
Spring provides infrastructure support, for example, spring connecting to the databases like the relational database and mango DB, spring has infrastructure support when we are building our applications.
Spring framework Pros and Cons….
- Loose Coupling.
- Huge framework.
- Multiple setup and configuration steps.
- Multiple build and deploy steps.
- The learning curve would be high.
So Spring Boot comes as handy…!!!
What is Spring Boot??
“Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can just run.” — from official spring.io
The main aim of spring boot is accelerating (speed up) application development, by the “convention over configuration” approach.
By using Spring Boot, we can develop end to end application with production ready.
What does it do??
Spring Boot reduces the complexity of dependency management with the help of spring boot starters concept. Whereas in spring we need to search for the required libraries for specific spring version.
For example, if we want to declare the web related dependencies, in general, we need to declare each and every dependency related to the web. But in the case of Spring Boot, we declare only one dependency “spring-boot-starter-web” it pulls all the required dependencies for the web module.
Some common configurations such as DataSource, JdbcTemplate, DispatcherServlet, HandlerMapping……. Spring Boot comes with the auto configuration to configure all these configurations by adding @SpringBootApplication or @EnableAutoConfiguration.
By using Spring Boot, we can create the standalone application and just run like normal java application we don’t have to find out servlet container because Spring Boot comes with the embedded servlet container. Whereas traditional Spring application we are building a war file and deploy in the servlet container.
Spring Boot comes with production ready features.
- We will use Maven or Gradle build for Spring Boot application.
- Spring Boot works with Spring 4.x version and later. And JDK 1.8
- Eclipse or STS
To build spring application we need to manage dependencies, so we need to add required jars for the specific version of spring. We will add the jars to the classpath so that the classes in jars are available to that application.
We are not following this approach, we are using Maven (build and dependency management tool) in Maven we will declare all the dependencies in pom.xml file and so that maven tool will automatically import required jars from the repository to our classpath.
Sample Spring Boot Application:
Create a simple Maven project.
Skip the archetype by selecting the create a simple project
Enter the group id, Artifact Id, and Name
Package Structure of SampleSpringBoot project
The default dependency file (pom.xml) is like below.
We adding the dependency spring boot starter parent which will act as a parent for the all remaining spring boot dependencies. And we are adding Java version and spring boot starter web dependency
We are creating SpringBootDemo application this is the starting point for our application and @SpringBootApplication tells spring this is spring boot application and create servlet container and deploy in a container.
What SpringApplication class will do??
Setup default configuration
Starts spring application context
Every spring application has application context which runs the spring application
Performs class path scan
If we declare a class with annotation @Service or @Controller … spring will look at those classes and treated service annotation service, controller annotation as a controller to identify all these classes it has to be scan the class path
Starts Tomcat Server
No need to download any Tomcat server by default it comes with spring boot so the output of the application is as standard application
We are creating Rest Controller to handle HTTP requests.
Overriding Tomcat port number in “application. properties” file, if the Tomcat default port (8080) is listening to some other process.
Run the sample spring boot application as like normal Java application.
By running sample spring boot application as a standalone java application, tomcat will have started automatically on the port 9090.
To verify this in the browser, just enter the localhost port number and request mapping.