Java Spring Boot परिचय
In this page:
What is Spring Boot?
Spring Boot एक लोकप्रिय, opinionated framework है जो Spring ecosystem के ऊपर बना है production-ready Java web applications जल्दी बनाने के लिए। यह sensible default configurations और एक embedded web server प्रदान करके project setup को काफी सरल बनाता है, ताकि आप Tomcat या XML files को manually configure किए बिना एक working app चला सकें।
उदाहरण: What is Spring Boot?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
// SpringApplication.run(Main.class, args); // starts an embedded server, no manual Tomcat/XML config
System.out.println("Spring Boot provides sensible defaults out of the box");
}
}
Login to try C/C++/Java/PHP code in the editor
Inversion of Control (IoC)
Spring Boot Inversion of Control (IoC) के इर्द-गिर्द बना है: आपके code द्वारा new से अपनी dependencies खुद बनाने और wire करने के बजाय, आप declare करते हैं कि किसी class को क्या चाहिए और framework उन objects (जिन्हें Beans कहा जाता है) और उनके lifecycles को बनाना आपके लिए manage करता है।
उदाहरण: Inversion of Control (IoC)
import org.springframework.stereotype.Component;
public class Main {
@Component
static class GreetingService {
String greet() { return "Hello"; } // the framework creates and manages this Bean, not your code
}
public static void main(String[] args) {
System.out.println("Spring manages Bean creation instead of manual 'new'");
}
}
Login to try C/C++/Java/PHP code in the editor
Dependency Injection
Dependency Injection (DI) वह mechanism है जिसे Spring Boot उन managed Beans को असल में आपकी classes में पहुंचाने के लिए उपयोग करता है। किसी field या constructor parameter को @Autowired से annotate करना Spring को एक matching Bean ढूंढने और उसे अपने आप inject करने के लिए कहता है, बजाय आपके इसे खुद instantiate करने के।
उदाहरण: Dependency Injection
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
public class Main {
@Component
static class GreetingService {
String greet() { return "Hello"; }
}
@Component
static class GreetingController {
@Autowired
GreetingService service; // Spring injects a matching Bean automatically
}
public static void main(String[] args) {
System.out.println("@Autowired injects the managed Bean for you");
}
}
Login to try C/C++/Java/PHP code in the editor
REST Controllers
REST Controllers वह तरीका हैं जिससे Spring Boot web API endpoints उजागर करता है -- किसी class को @RestController से और उसके methods को @GetMapping जैसे mapping annotations से annotate करें ताकि incoming HTTP requests को specific Java methods पर route किया जा सके, जो फिर data return करते हैं जिसे Spring अपने आप JSON में serialize कर देता है।
उदाहरण: REST Controllers
import org.springframework.web.bind.annotation.*;
@RestController
public class Main {
@GetMapping("/hello")
String hello() {
return "Hello, World!"; // automatically serialized to JSON/text in the response
}
public static void main(String[] args) {
System.out.println(new Main().hello());
}
}
Login to try C/C++/Java/PHP code in the editor
Spring Boot Application Run
हर Spring Boot app में एक केंद्रीय starter class होती है जिसकी main method SpringApplication.run() को call करती है। वह single call पूरे application context को bootstrap करता है, आपके Beans को scan और register करता है, और embedded server को शुरू करता है, वह सब बिना आपके manual startup wiring लिखे।
उदाहरण: Spring Boot Application Run
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
// SpringApplication.run(Main.class, args); // bootstraps the app context and starts the embedded server
System.out.println("main() -> SpringApplication.run() bootstraps everything");
}
}
Login to try C/C++/Java/PHP code in the editor
- Spring-managed Beans को instantiate करने के लिए new keyword का उपयोग करना, जो Inversion of Control को पूरी तरह बायपास कर देता है।
- ज़रूरी component-scanning annotations जोड़ना भूल जाना, जिससे Spring आपकी classes को ढूंढने और inject करने में fail हो जाता है।
- अपनी main starting method के अंदर भारी, blocking code लिखना, जो framework को सफलतापूर्वक bootstrap होने से रोकता है।
- Spring Boot एक production-ready framework है जो natively automatic configurations और embedded web servers प्रदान करता है।
- Inversion of Control (IoC) का मतलब है कि framework आपके object instances (Beans) और lifecycles को अपने आप manage करता है।
- managed Beans को साफ़ तरीके से पहुंचाने के लिए Dependency Injection (DI) का उपयोग करें, और browser paths को API methods पर map करने के लिए REST Controllers का।
Spring Boot और standard Java compilation requirements सभी Java 11+ development kits द्वारा natively supported हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: