-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringboot Fundamentals
More file actions
437 lines (316 loc) · 14.7 KB
/
Copy pathSpringboot Fundamentals
File metadata and controls
437 lines (316 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
Welcome to the Spring Boot Course!
Are you ready to dive into the world of backend development with Spring Boot, one of the most powerful and widely-used Java frameworks?
In this course, you’ll go from zero to building real-world REST APIs and secure, layered web applications using modern Spring Boot practices.
Why Learn Spring Boot?
Spring Boot is one of the most in-demand backend technologies in the industry.
It allows you to build scalable, secure, and production-ready applications faster and easier than traditional Java frameworks.
Whether you want to become a backend developer, full-stack developer, or build your own products, Spring Boot gives you the power to do it.
No prior backend experience is required to get started.
############################
Run your first Spring Boot application
Let’s run your first Spring Boot application that says “Hello, From Spring Boot!” in the browser.
Don’t worry if you don’t understand all the code right now — we’ll explain terms like @SpringBootApplication, Controller, and main() later in the course.
How to Run the App
You have two options to run the project:
Click the “Run” button (in your editor or provided interface)
Or open the terminal and run:
cd ~/workspace/demo
mvn spring-boot:run
This will launch the application and you'll see “Hello, From Spring Boot!”.
################################
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/") // handles GET request to root URL
public String home() {
return "Hello, From Spring Boot!";
}
}
###############################
Spring and Spring Boot
Let’s say you’re building a Java app, like a simple Student Management System.
You create classes like:
StudentService → contains the logic
StudentRepository → talks to the database
Now comes the boring part:
You need to manually create objects, connect them, and make sure everything runs smoothly. It works — but it gets messy fast.
How Spring Helps
Spring acts like a smart assistant for your Java app.
Instead of you doing everything yourself, Spring says:
“Just tell me what you need.
I’ll create the objects and connect them for you.”
You don’t create objects manually anymore — Spring handles it.
Spring does this using two superpowers:
IoC (Inversion of Control) – Spring controls object creation, not you
DI (Dependency Injection) – Spring gives each class what it needs
Don’t worry — we’ll explain this in detail later in the course.
Core Idea
Spring is a tool that takes care of object creation and connections so you can focus on writing logic.
That’s why companies love Spring. It’s fast, clean, and powerful.
#####################################
Introduction to Spring Boot
Spring is super helpful… but setting up a Spring project used to feel like setting up a tent without instructions:
You had to configure everything manually
Add a bunch of XML files or annotations
Decide which dependencies to include (and manage them yourself)
In short: It was powerful, but also complicated & hard to get started with.
This is where Spring Boot helps :
Spring Boot was created to make things simple and fast. Instead of struggling with spring setup.
image
So… What is Spring Boot?
Spring Boot is like a pre-configured version of Spring that helps you:
Gives you a ready-to-use project
Configures things automatically (so you don’t have to configure everything)
Lets you start coding immediately
Run your app with a single command
Focus on writing business logic — not setup
Real-life analogy
Let’s say Spring is a DIY furniture kit — all the parts are there, but you need to assemble them.
Spring Boot is the same furniture — but already assembled, tested, and ready to use.
You just unbox it, plug it in, and go.
################################
Role of Spring
Spring helps developers mainly by:
Answer = Managing Java objects and their dependencies...
Spring mainly helps by creating and managing Java objects and injecting their dependencies automatically,
so developers don’t have to manually connect everything.
##################
Role of Spring Boot
What is Spring Boot mainly used for?
Anser = Making Spring applications easier and faster to build...
Spring Boot is used to make Spring applications faster and easier to build by reducing setup and configuration work.
###########3
Spring Initializr Setup
By now, you know:
Spring → helps manage your Java objects and their dependencies
Spring Boot → simplifies Spring by handling setup and configuration automatically.
At this point, you might be thinking :
“Okay… but how do I actually start a Spring Boot project?”
That’s where Spring Initializr comes in.
What is Spring Initializr :
Spring Initializr is a simple website that generates a ready-made Spring Boot project for you.
You don’t have to:
Create folders yourself
Configure files manually
Set up everything from scratch
It does the hard work for you.
How to Set Up a Project Using Spring Initializr
Open Spring Initializr in your browser: https://start.spring.io/
Select:
Project: Maven
Language: Java
Packaging: Jar
Java Version: 17
Add dependency: Spring Web
Click Generate to download the project.
Extract the ZIP and open it in your IDE. Your setup is done!
Take a look at the visualizer given here to see how all the project options are displayed
In this course, your Spring Boot project setup is already done in your lab environment.
This explanation of Spring Initializr is just to help you understand how projects are created in the real world — no manual setup needed.
##########################
Working of first springboot application
Let's see the working of first springboot application.
How to Run the App
You have two options to run the project:
Click the “Run” button (in your editor or provided interface)
Or open the terminal and run:
cd ~/workspace/demo
mvn spring-boot:run
This will launch the application and you'll see “Hello, From Spring Boot!”.
How the Code Works :
The application starts from the main() method in DemoApplication.java.
Spring Boot automatically starts a web server in the background.
While starting, Spring scans the project and finds HelloController.
@GetMapping("/") tells Spring which method should run when a request comes in.
Spring calls the home() method.
The method returns a text message.
Spring sends this text back as the response.
That’s how “Hello, From Spring Boot!” appears in the browser.
Quick Note About Project Setup
Normally, you can go to https://start.spring.io to create a Spring Boot project from scratch.
But for this problem, we’ve already created and given you a basic Spring Boot starter project, which:
Is configured with Spring Web dependency
Has all the folder structure ready
Includes a pre-written controller for you to explore
This is the magic of Spring Boot: easy startup with minimal configuration.
############## CODE ###########
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/") // handles GET request to root URL
public String home() {
return "Hello, From Spring Boot!";
}
}
############################
Understanding Your Spring Boot Project Structure
Now that you have created your first Spring Boot app, let’s slow down and understand what exactly is inside the project and
how Spring Boot works behind the scenes.
Project Structure Overview
workspace/
└── demo/
└── src/
└── main/
├── java/
└── resources/
Let’s break this Project Structure :
src/main/java → Your Application Code
This is where all your Java code lives. Inside it, you will find:
src/main/java/com/example/demo/
HelloController.java ← your controllers
DemoApplication.java ← main class
Think of this folder as the “brain” of your application.
Your controllers, services, repositories, configuration files — everything goes here.
src/main/resources → Your App Settings + Templates + Static Files
Contains important resources used by your application:
src/main/resources/
application.properties ← app settings (port, DB config, etc.)
static/ ← CSS, JS, images for web apps
templates/ ← HTML templates (if using)
#########################
When You Run a Spring Boot Application
When you run your Spring Boot application, several things happen automatically in the background. You don’t have to manually control them.
1. Maven prepares and runs your project
Maven is a project helper tool (similar to npm in Node.js).
It helps by:
Reading a file called pom.xml (this file tells Maven what your project needs)
Downloading required libraries automatically
Compiling your Java code
Running your Spring Boot application
So instead of doing all this manually, Maven does it for you.
2. Spring Boot starts your application
Spring Boot:
Finds the main class marked with @SpringBootApplication
Starts the application from there
Sets up everything your app needs
Creates the Spring environment (also called Spring Context)
You don’t need to configure all this yourself.
3. Built-in web support starts automatically
Your Spring Boot app can run in a browser because it includes built-in web support (the built-in web server is called Tomcat).
You do NOT need to: Install a server, Configure a server
This happens because of this dependency in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
This tells Spring Boot: This is a web application and it should run in a browser..
####################
Understand the Code of Your First Spring Boot App ......
Now that you understand the project structure of a Spring Boot application and
how it runs using Maven and embedded Tomcat, it’s time tolook at the code.
In this lesson, you’ll understand the actual code of your first Spring Boot app, see where the Hello, From Spring Boot! output comes from,
and learn what each file does, step by step..
In your first Spring Boot app, only two files are important:image
DemoApplication.java → starts the application
HelloController.java → returns “Hello World” in the browser
Let’s understand them one by one.
DemoApplication.java (The Entry Point of a Spring Boot App)
### CODE ##########
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Spring Boot application started!");
}
}
################3
What is this file?
This is the starting point of your Spring Boot application.
Just like every Java program starts from:
public static void main(String[] args)
Every Spring Boot application also starts from this file and this method..
Let’s begin by understanding the important annotation that starts the application.
@SpringBootApplication :
Marks this class as the starting point of a Spring Boot application.
Automatically:
Configures the application
Scans all components (like controllers)
Sets up the embedded server (Tomcat)
SpringApplication.run(DemoApplication.class, args);
This line starts the entire Spring Boot application
It:
Creates the Spring container
Starts the embedded Tomcat server
Makes your application ready to handle requests
HelloController java (Hello World Logic)
########### CODE ####
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String home() {
return "Hello, From Spring Boot!";
}
}
############
This file is responsible for: What you see in the browser
Don’t worry about @RestController or @GetMapping("/") for now.
Just focus on understanding how the application starts and returns a response.
We’ll explain these annotations in detail later.
############################################################3
MCQ 1 :
What is the role of @SpringBootApplication?
Ans = Marks a class as main Spring Boot entry point ..
@SpringBootApplication marks the main class of a Spring Boot application.
It tells Spring Boot to start the application, enable auto-configuration, and scan components automatically..
######333
MCQ 2 :
Which folder contains Java source code?
Ans = src/main/java
src/main/java contains all the Java source code of the Spring Boot application, including controllers, services, and the main class..
###########
MCQ 3 :
What is Embedded Tomcat?
Answer = Built-in web server in Spring Boot...
Embedded Tomcat is a built-in web server that comes with Spring Boot, so you can run the application without installing any external server..
#####################
Display your first springboot message - Practice
So far, you have learned:
How a Spring Boot project is structured
How to run a Spring Boot app
How a simple “Hello” app works in the browser
What @SpringBootApplication does
In this practice exercise, you will display "This is my first springboot application" on browser.
Instructions:
Complete your code first.
Click on "Run" to start the server and make sure it runs correctly.
If you make any changes to the code after running, you must run the server again.
Only after running the server with the latest changes, your submission will reflect the updated solution.
Do not submit if the server is not running; otherwise, your solution will fail.
Goal :
Complete the controller code i.e (HelloController.java),
so that your Spring Boot application returns a message in the browser - This is my first springboot application..
########### CODE ###########
HelloController.java...
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String home() {
return "This is my first springboot application";
}
}
#################
DemoApplication.java ...
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication { // <- class name remains App
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Spring Boot application started!");
}
}
############################