Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - Basic


If you are already working with Spring framework and tired of all sorts of initial configuration needed to write a Spring application. Spring Boot comes as a solution for you.


What is Spring Boot


Spring Boot consists of two parts, Spring and Boot. Where Spring is a framework that lets us write Java Enterprise Applications and Boot stands for Bootstrap.


So, Spring Boot is something that lets you create a stand-alone, production ready Spring application that is ready to run.


In simple words Spring Boot is something that lets you create Spring based Application without an additional effort.


So, don't you think Spring Boot does something magical?


Now, let's see how Spring Boot creates that magic.


First Spring Boot Application


Example :



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyFirstApplication {

   public static void main(String[] args) {
   
      SpringApplication.run(MyFirstApplication.class, args);
   }
}



And that's all. Your Spring Boot application is ready.


You might be wondering, what is,


@SpringBootApplication

Or what is SpringApplication.run(...)


SpringApplication.run(MyFirstApplication.class, args);

So, let us break it into four steps :


1st Step


For now, just remember the above application is just a normal java application with class name MyFirstApplication.


public class MyFirstApplication {
	public static void main(String[] args) {

	}
}

2nd Step


In the above java application, we are placing an annotation @SpringBootApplication, just above the main() method.


@SpringBootApplication
public class MyFirstApplication {
	public static void main(String[] args) {

	}
}

The @SpringBootApplication is used to convert the above java application to a Spring Boot Application.


3rd Step


Now, that the java application has become a Spring boot application. It is time to run the Spring Boot Application.


And for that SpringApplication.run(...) is used inside the main() method.


@SpringBootApplication
public class MyFirstApplication {

	public static void main(String[] args) {

		SpringApplication.run(MyFirstApplication.class, args);
	}
}

So, SpringApplication.run(...) has two arguments. The first one is the class which is annotated with @SpringBootApplication(i.e. MyFirstApplication) and the second one is the argument from the main() method(i.e. args).

Spring-Boot-Basic

4th Step


And finally, to support @SpringBootApplication and SpringApplication(Used for the above run method), we need to import them.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

And we have created a stand-alone, production ready Spring application that is ready to run.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyFirstApplication {

	public static void main(String[] args) {

		SpringApplication.run(MyFirstApplication.class, args);
	}
}

So, in simple words,

  1. Create a java application with a main() method.


  2. Use '@SpringBootApplication' to make the Java application a Spring Boot Application.


  3. Run the Spring Boot Application using the 'SpringApplication.run()' method to run the application.


Now, some of you who have quite good knowledge in spring, must be thinking, how to include the dependencies for,


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

And how to configure the Maven file for the above?


Well! The next tutorial explains that.