The Challenge of Spring Framework Configuration
Welcome back, aliens. My name is [Navin] Reddy and in this video, we'll talk about Spring Boot. So before we talk about Spring Boot, the target for this video is 250 comments. Now, till this point, we have talked about Spring framework, right? And we are excited to understand how this framework works and whatever things it promises, how it deals with that. So yes, Spring framework is amazing, right? And whatever application we are going to build now is with the help of Spring.
So let's say you want to build an application, this can be anything in the world. Even it can be a huge application or it can be an application for hello world. Typically what happens is when you want to build the application, of course you have your Java code with you. And then to make it work, Spring says, "Hey, you know, doesn't matter how many classes you have, I will take care of the object creation." And you are quite happy because now you don't have to worry about the objects. Spring says I will take care of it.
So let's say in your application you have three classes and you say, "Hey Spring, create object of this three classes," and Spring is happy to do that. So let's say in your application now you have 100 or thousand classes. Can Spring handle all the objects? Of course it can. But the question is, do you want Spring to handle all the classes? You know, most of the time, we don't even want the object of few classes. Let's say you have a lot of classes here, and out of all these classes, you need objects of only few classes. How do you talk to your Spring framework by saying, "Hey, don't create objects for everything, I don't want all those things, I just want few"? Spring says, "Okay, tell me which one you want."
Manual Setup and Time Consumption
And that's where when you talk to the framework, we have to do that in the configuration file. It can be an XML file or it can be a property file, but you have to talk to the framework. That means just because you're using a framework, you will not be able to run your code in the first go. You have to first do the configuration.
Next part, let's say if you are building a web application. Now, if you want to run your web application, basically you need a server. In terms of Java, we need something called a Tomcat server. Of course, we have multiple options. Let's say if you're building a web application in Spring, you need to have a Tomcat server. That means in your machine, even before you run your code, you have to make sure that you have installed Tomcat, you have configured Tomcat, then only you can run your application.
So you'll be saying, "Hey, what's wrong with that? I mean, of course we can do configuration, we can install Tomcat." See, the problem is when you have a long project, it makes sense to spend few hours or days doing the configuration because the project will go for a year. But what if you are doing some experiment? What if you want to get started in few minutes? See, most of the languages nowadays, they are doing that. If you talk about Python or JavaScript, they have their frameworks, and if you want to at least print hello world, you can do that in minutes. Now, this is what Spring was lagging way back, and Java was lagging way back.
Introducing Spring Boot: The Solution
And that's where we got something called Spring Boot. Now, Spring Boot says all your problems of configuration, let me take care of it. What if you can get your project running in few minutes? And that's what Spring Boot gives you. It's not a new project; it's there from a long time, but it basically solves your problem of building a project in less time.
So does it mean that Spring Boot is different from Spring framework? Not exactly. See, underlying, it's all Spring framework, right? On top of that, you have one more layer of Spring Boot. So you can actually build an application directly using Spring framework, or if you want to make it easy, you can use Spring Boot. Again, that's an optional stuff, but this optional thing is very, very important and it will make your work faster.
So basically, we can use a Spring Boot, which is an opinionated framework, which means it will give you certain things the way it wants, and you can simply use it to run your application. Let me show you how, what I'm talking about.
Using the Spring Initializr
So let's say if I go to the spring.io, the official website, and instead of this website, if I just say start.spring.io, it will take you to a website. Now, see, when you talk about building a project in different IDEs, we have an option of directly saying a new project and you can create a Spring project. In a Spring project, basically you create a Maven project. Again, we'll see that in sometime or in the upcoming videos, but basically you have to create a Maven project at the Spring dependency, a lot of different steps, right?
IntelliJ IDEA Ultimate version gives you an option of creating a Spring Boot project directly. But since we are not going to go for the ultimate version, we don't want to pay for it, we have a good alternative. What you can do is you can go to Spring Initializr, which is this website, start.spring.io, and you can mention what kind of project you are building. So I'm building a Maven project, this is a build tool. The language I'm going for is Java, and then the Spring Boot version. So this is the update version so you can see we have 3.2.5. We can even go for the RC1 but I just want to stick to the stable one which is 3.2.5 and depend upon when you're watching this, you can just use that particular version. The group I will say com.telusko and the project name is let's say DemoApp. And then the packaging is I'm going for JAR. Now this is where the beauty lies.
JAR Packaging and Embedded Tomcat
You know, if you talk about a Spring framework or any web application in Java, you can create a project and if you want to deploy it on the cloud, basically you create a WAR file. So WAR stands for web archive. And then you basically push your WAR file in the Tomcat to run it. Multiple steps, right? Spring Boot says, don't worry, you can create a JAR file. But the problem is a JAR file, we cannot run a JAR file on the Tomcat. Then how it would work? What if you don't need external Tomcat? What if the project itself has a Tomcat? I mean, that will be awesome, right? So basically Spring Boot says if you want to build a web project, you will get an embedded Tomcat. So I will select jar and I'll show you what I'm talking about.
And then I can select my Java version. So in this machine, I got Java 21 so I'll go with that. It is an LTS version, so it's safe to use. And dependency, so what are things we need? So of course if you remember when we talked about Spring framework, I mentioned that there are multiple projects inside Spring and we don't need all. Depending upon your use case, you will choose one.
Configuring and Generating the Project
So I will click on Add Dependencies. So you can see there are a lot of options here to choose from and you don't have to know everything, use whatever you need. Example here, I want to build a web application so I will simply say Spring Web. If you want to add database connectivity, you can also add JPA, but we don't need that at this point. If you want to get Lombok support, you can choose that. If you want to use GraphQL, you can use that. So as I mentioned, there are so many options here but I just want to stick to one which is Spring Web and click on generate. Now before I click on generate, I will show you how this configuration looks like. So it will give you the entire configuration. We can see we got a dependency which is spring-boot-starter-web here. The Java version is 21. This is my project name and this is the Spring Boot version which we are using and behind this it will use Spring framework. I will show you the code.
So click on download, you can even click on the generate here. So you can see in the downloads I got this project which is DemoApp. What I will do is I will just unzip it. So unzipping is done on the other screen, but I will just open that project now in my IDE. Now you can use any IDE here. You can use Eclipse, you can use VS Code, you can use IntelliJ IDEA Community version or the Ultimate version. So I'm using a community version here just to show you the proof. I will click on, not update, I will click on about, and you can see this is a community version. And I will click on open the project. So this is the project which I've downloaded. Click on open.
Exploring Project Dependencies in IntelliJ IDEA
And voila, you got your project. And the beauty is, if I expand the dependencies, I think it will take some time to download. Okay, downloaded too fast. And you can see it added so many dependencies here. For the Jackson for JSON conversion, we got micrometer for observing, we got Tomcat. You can see we got embed Tomcat. So basically you don't need to have the external Tomcat here. And if you go down, we got Spring Boot, but also we got Spring framework. You can see we got Spring Core. So all the things which are required for Spring Core or Spring project, we got it here. Okay, let me just drop it here.
Building a "Hello World" REST Controller
And now let me create a Hello World. So what I will do is just to create Hello World. Now if you have worked with servlets or any other language before, basically we do multiple steps, right? But let me show you the code in Spring which will print Hello World. I'm not going to explain everything in this video how Spring Web works, we have a separate section on that, but just to show you how to build an application, I will get a class and I will name this class as Hello. I'll just simply say Hello. And then I will just return... I will create a method here and this method is responsible to print "Hello World" on the screen. So I will say public String greet() and this will return "Hello World". A very simple method, right? Nothing fancy for a server. A simple class is just that if you want this to work, you simply say here @RestController and we have to map it with... again I'm not explaining it here, we have a detailed topic on this, what is @RequestMapping, what is @RestController, we'll talk about it. At this point, just go for two annotations and I will say /. So basically whenever you request for the home page, it will return the hello world. That's what I want. Or maybe I can say, "Hello world, welcome to Telusko."
Running the Application and Testing
Okay, cool. And now let's run this. How do we run this? So you go back to your application or the main file and simply say 'Run'. Behind the scene, it will of course compile the code, it will run this on a Tomcat. Just have a look. And when you run for the first time, it will print this Spring pattern here. You can change it if you want, but we are concerned about this, or we want to focus on this. So Tomcat started on port number 8080. Nowhere externally I'm using Tomcat. Okay, a very simple stuff. And I'm saying run on 8080. I got it. How do I test it? Of course you can use a REST client like Postman or I will just open the browser and here I will say localhost:8080. Enter, and if you can see, we got the answer, we got a response which is "Hello World, welcome to Telusko". It's so simple, right? And how much time it took me to run this? Of course, I was talking to the camera and doing this, so it took a lot of time. But if you want to do this, you can do that in minutes. So you got your application up and running in minutes. That's not the case with Spring framework, and you will see that once we start with Spring later.
The Pros and Cons of Spring Boot
So this is Spring Boot. Behind the scene, it is still Spring framework. Now the question is, is it better than Spring? Of course, right? It will help you, but there are certain issues with Spring Boot as well. Of course, not a big issue. One of the issue is by default, it will do a lot of stuff for you. So it basically follows convention over configuration, because in Spring, we do a lot of configuration. Here it says, "I will give you stuff, you tell me what you want, I will give it to you." And most of the time, it also gives you certain things which you don't want. Example, if you expand this libraries here, there are so many things maybe I will not even going to use this but Spring Boot says, "Take it." Okay?
So that's one. And next, since it gives you a lot of default stuff, if you want to configure, then of course you have to do those configuration by yourself. That means if you want more control, it is better to work with Spring framework than Spring Boot. Again, debatable. Personally, I prefer Spring Boot if I want to work on a project. I'm happy to do the configuration in Spring Boot, but yeah, people might prefer Spring framework for that reason. It's still debatable, but Spring Boot is awesome.
So yeah, that's one of the issue. And, yeah, that's about it. I wanted to show you what is Spring Boot. And people who cry about Java being slow, Java being verbose, Spring Boot is here. It will make your work easy. See you in the next video.