← Back to Advanced Java Course | Chapter 10: Spring Boot | Lesson 1 of 6

Java Spring Boot परिचय

Spring Boot एक toolkit है जो ज़्यादातर setup पहले से किए हुए एक Java web application को जल्दी चलाने में मदद करता है, जैसे pre-assembled parts वाली एक kit। यह पुर्ज़ों को manage करता है ताकि आप अपनी खुद की features पर ध्यान दें।

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?

java
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");
	}
}

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)

java
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'");
	}
}

Dependency Injection

Dependency Injection (DI) वह mechanism है जिसे Spring Boot उन managed Beans को असल में आपकी classes में पहुंचाने के लिए उपयोग करता है। किसी field या constructor parameter को @Autowired से annotate करना Spring को एक matching Bean ढूंढने और उसे अपने आप inject करने के लिए कहता है, बजाय आपके इसे खुद instantiate करने के।

उदाहरण: Dependency Injection

java
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");
	}
}

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

java
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());
	}
}

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

java
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");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Spring-managed Beans को instantiate करने के लिए new keyword का उपयोग करना, जो Inversion of Control को पूरी तरह बायपास कर देता है।
  2. ज़रूरी component-scanning annotations जोड़ना भूल जाना, जिससे Spring आपकी classes को ढूंढने और inject करने में fail हो जाता है।
  3. अपनी 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.