Spring boot is becoming very popular now a days for creating web application ,commonly used for creating microservice based architecture.In this blog post, you will know the fundamental of spring boot and how to use spring boot in real world project. The idea here is not just to go through theory part but to have clear understanding of spring boot with doing practice.Here we are going to create a full fledge web application using spring boot.
As we proceed through this course we will be covering below topic-- Spring Boot Starters
- Containerless Deployments
- Auto-configuration
- Testing with Spring Boot
Prerequisite
This course has been developed by keeping in mind that the audience have prior knowledge of basic of Java and Spring fundamentals.- Java 1.8+
- Gradle or Maven
- IDE (Spring STS ,Eclipse etc.)
Creating your first spring boot project
Welcome to the course of creating your first spring boot application. Spring boot is a very large topic and combine with spring.This course will cover the fundamental of working with spring boot and creating a web application. This course is gear towards beginner and intermediate spring developer to introduce spring boot in that context ,giving you an understanding of what it is , how to use it and cover the fundamental concept of architecture of the framework.
This platform is a game changer for java application.If you never had a chance to use spring boot before get ready for paradigm shift and how you think about building web application with Java technology. Just to show how great spring boot is ,we will be building a spring boot application from scratch.
Spring boot can be created by using Maven and Gradle ,In this course we will be using Maven build tool to create a simple web application. In this course we are going to create a promo-api ,it will have simple crud operation like posting a new promotion, updating a promotion, getting list of promotion and deleting a particular promotion.
So let's start - Open up STS if you don't have it you can download from Here,Go to new -> Maven -> maven project->next->finish. Once done below folder structure will look like-
Now to make this application works as spring boot,we have to add parent to our Maven project. It is used to declare that our project is a child to this parent project. After that add the following dependency to the pom.xml file. Here, we are adding web dependency by adding spring-boot-starter-web. After that add Java version for the project. <properties> <java.version>1.8</java.version> </properties> Now save it will download all the dependency of spring framework. After saving if you navigate to the problems tab you will see below error- To resolve this go to project folder, right click ->maven -> update project .In the left hand in maven dependency you can see that maven has downloaded all the dependent jar file to it's classpath. Now we are going to create a simple java application to run as a spring boot application. To create a spring boot application you have to to do two things ,first you have to annotate your class with @SpringBootApplication and inside the main method add SpringApplication.run(app.class, args) ,I will explain this in details later on. When class is annotated with @SpringBootApplication ,it does below things -- Sets up default configuration
- Starts spring application context
- Performs classpath scan
- Starts tomcat server
BOM :Bill of Materials
Spring boot makes life easier when we need to integrate different library in a framework and the spring boot call it a BOM stands for bill of materials.Instead of adding all dependency separately with version spring boot comes with parent dependency management. By including, Spring boot manages all the transitive dependency with version.It saves us an extra effort to manage version of all dependency.Others Spring Boot Initializers
Even if rolling up spring boot application is not that hard ,still spring boot provides with extra tools to create a spring boot application Spring initializer is web tools that spring provide ,and you can create entire spring application with different dependency.I suggest you to go to start.spring.io and try creating spring boot project.Here spring boot has covered almost all dependency needed to create a web application. The other way of creating an spring boot application is through command line(Cli) ,internally spring boot calls the same web api to create an app.As many developers are interested in working with command line it may attract them as well.You can follow below docs to create spring boot using command line. Spring boot also provides some sample projects in spring git hub repository ,please use the below url to get to sample project.How Does Spring Boot Works ?
Now let's dive deeper into how spring boot works internally.As we know spring boot is a java application so for any java application to run JVM needs a public static main(String [] args) method.This method will fire up spring boot but wait a second how it does ? Spring boot has tomcat as embedded sever and bunch of internal configuration that helps spring boot to run .We will go step by step to dive deeply into it. When main class runs it will first starts java then the application ,at top level we have @SpringBootApplication annotation, it has bunch of internal auto configuration primarily for spring boot. @SpringBootApplication : A convenience annotation that wraps commonly used annotation with spring boot.The annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes. @Configuration : This is spring configuration on start up, here in spring boot we don't use any xml for configuration. This annotation is best suited for creating configuration in Java class itself.It allows to register extra beans in the context or import additional configuration classes. @EnableAutoConfiguration : Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration. You can always manually exclude() any configuration that you never want to apply. Auto-configuration is always applied after user-defined beans have been registered. @ComponentScan : This will scan the project for spring components.Spring can auto scan, detect, and instantiate components from pre-defined project packages. Spring can auto scan all classes annotated with the stereotype annotations @Component, @Controller, @Service, and @Repository.Why Move to Containerless Deployments
When an application is deploy inside a container there are lot of pre-set up configuration which is needed to be done.If you have production ,dev and stage environment the container needs to be set up in each places. You will have to set up deployment descriptor web.xml to tell container the working flow of your application.In case if container is embedded into your application it gives to privilege to run your application anywhere without any pre set up configuration and application will tell the container to respond on http request. Below are major difference between them- Now that we have come this far and got to know how spring boot framework works and also created a simple spring boot web application.Spring boot auto configure spring mvc patterns for us.We will go to create full functional spring boot app by creating some more controller, services and data model.Restful Web App
There are lot of ways that you can built user interface for an application these days.let's go ahead and pick one ways to accomplished that in this course. As spring boot inbuilt with spring core ,spring mvc and spring rest .In the diagram above showed how typical web application looks like.Client makes http/https request to server to do some job. A client can be any web browser or a front end application who will utilize the rest controller and ask server to do a task.Integrating Front End Part in Spring Boot
I am not going to cover front end part of application but I can brief about how spring boot used for integrating front front end part is spring boot application.In spring boot you can build front end client assets by including public or static folder in classpath. Please follow below steps to integrate front code with spring boot - 1. Create a resources folder inside src/main directory ,by default anything including into this folder can be taken on classpath in spring boot. 2. Create public or static folder inside src/main/resources folder 3. Create index.html file and put it into src/main/resources/static folder and update the maven project.Any client code can be put into this folder will be taken as part of classpath.Spring MVC Rest Controller
We have done the enough of theory ,now get our hand dirty by extending our working spring boot application.We are going to perform basic CRUD operation inside the RestController- We are going to create following rest Uri for all the CRUD operation - 1. GET promo/get (get the list) 2. POST promo/add (add) 3. PUT promo/update/{Id} (update) 4. DELETE promo/delete/{Id} (update) Update your controller class with below code added- Create a model class - Create a repository for performing all the CRUD operation -That's it !!! You got it now ,we have fully functional spring boot rest controller.You can hit "http://localhost:8080/promo/get" in postman to see our app in working fine. We will add the external database later on until then simple java class is handling all operation.
Spring MVC Integration Overview
At this point we have successfully created spring boot rest apis.let's take a moment and talk about what spring boot did to auto-configure and integrate spring MVC for us. When we added the spring-boot-started-web dependency in pom.xml ,spring boot did more than simple adding jar file to the class path.As we already have auto-configuration enable in @SpringBootApplication annotation ,spring doest some extra steps to set up spring MVC feature for us.Firstly it sets up view resolver automatically.In our case spring boot told spring MVC to set up content negotiating resolver which determine how to respond based on content type. Spring boot also sets up Jackson Json library to handle content type.Next spring boot configure spring MVC to server static content rooted in the classpath under public or static folder.In the end sets up HttpMessageConverter to convert Json into Java object and vice versa.
Comments
Post a Comment