Spring Core Modules Spring Core is the heart of the Spring Framework. It provides: • IoC (Inversion of Control) • Dependency Injection (DI) • Bean Lifecycle Management • Application Context Instead of creating objects manually, Spring manages them for us. This improves: Loose coupling Testability Maintainability Scalability ============================ Simple Example (Without Spring) ============================ class Engine { void start() { System.out.println("Engine started"); } } class Car { Engine engine = new Engine(); / Tight coupling void drive() { engine.start(); } } Here, Car is tightly coupled to Engine ================================ With Spring (Dependency Injection) ================================ @Component class Engine { void start() { System.out.println("Engine started"); } } @Component class Car { private final Engine engine; @Autowired public Car(Engine engine) { this.engine = engine; } void drive() { engine.start(); } } Spring injects the dependency. Car does not create Engine - Spring provides it. That’s the power of IoC. --------------------------------------------------------------- Spring Core is not just a framework feature. It’s a design philosophy - write loosely coupled code and let the container manage object lifecycle. Tomorrow: Bean Lifecycle & ApplicationContext deep #Java #SpringFramework #SpringBoot #BackendDevelopment #Microservices #LearningInPublic
Spring Core: IoC, DI, and Loose Coupling with Spring Framework
More Relevant Posts
-
🚀 Spring Annotation-Based Configuration (Beginner Friendly Guide) If you’re starting with Spring, understanding how the IOC container works is super important. Let’s break it down with a simple example 👇 👉 1. Main Class (Entry Point) import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // Step 1: Load Spring configuration file ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml"); // Step 2: IOC container is ready and beans are created automatically System.out.println("IOC Container Loaded Successfully ✅"); } } 👉 2. applicationContext.xml (Configuration File) <beans xmlns="https://lnkd.in/d858D-Nk" xmlns:xsi="https://lnkd.in/dNJZbNmc" xmlns:context="https://lnkd.in/dEDWzfEq" xsi:schemaLocation=" https://lnkd.in/d858D-Nk https://lnkd.in/dbit7sVK https://lnkd.in/dEDWzfEq https://lnkd.in/daN7U-FT"> <!-- Scan this package for annotations --> <context:component-scan base-package="org.annotationbasedconfiguration"/> </beans> 💡 What does this mean? ✔️ Spring reads the XML file ✔️ Scans the given package ✔️ Finds classes with annotations like: @Component @Service @Repository ✔️ Automatically creates objects (Beans) and manages them 🎯 Why use an Annotation-Based Approach? ✅ Less XML configuration ✅ Cleaner code ✅ Easy to manage ✅ Industry standard approach 👉 Simple Understanding: "Just add annotations in your class, and Spring will create & manage objects for you." #Java #SpringFramework #Beginners #BackendDevelopment #IoC #DependencyInjection #CodingJourney
To view or add a comment, sign in
-
🔹 Spring Concept: Bean Lifecycle In the Spring Framework, every object managed by the Spring IoC container is called a Bean. Understanding the Bean Lifecycle helps developers control how objects are created, initialized, and destroyed. 📌 Stages of Spring Bean Lifecycle 1️⃣ Instantiation Spring creates the bean instance. 2️⃣ Dependency Injection Spring injects required dependencies. 3️⃣ Initialization Custom initialization logic runs using: • @PostConstruct • InitializingBean • custom init-method 4️⃣ Bean Ready for Use The bean is now fully initialized and used in the application. 5️⃣ Destruction When the application context closes, cleanup logic runs using: • @PreDestroy • DisposableBean • custom destroy-method 💡 Example: @Component public class NotificationService { @PostConstruct public void init() { System.out.println(“Bean Initialized”); } @PreDestroy public void destroy() { System.out.println(“Bean Destroyed”); } } ✨ Understanding the Bean lifecycle helps developers manage resources efficiently and write better Spring applications. #Java #SpringBoot #SpringFramework #BackendDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
A quick question that made me curious: What actually happens behind the scenes when we use @Transactional in Spring Boot? Most of us just add the annotation and trust it to handle everything. But under the hood, something interesting is happening. Spring doesn’t directly modify your method. Instead, it creates a proxy around the bean. So when a transactional method is called, the flow looks like: Client → Proxy → Transaction Manager → Your Method → Commit/Rollback Here’s what the proxy does: • Starts a transaction before method execution • Executes your business logic • Commits if everything is fine • Rolls back if an exception occurs But here’s another catch 👇 Not all exceptions trigger rollback. By default, Spring only rolls back for: • Runtime exceptions (RuntimeException) • Errors (Error) But checked exceptions (like IOException, SQLException) 👉 do NOT trigger rollback by default So sometimes: • Your code throws an exception • But the transaction still commits 😳 If you want rollback for all exceptions, you need: @Transactional(rollbackFor = Exception.class) And one more important catch: The proxy only works when the method is called from outside the bean. If one method inside the same bean calls another method annotated with @Transactional, the call bypasses the proxy. So the transaction may not even start. That’s why sometimes: • Transactions don’t work as expected • Rollbacks don’t happen • Bugs are hard to trace Spring isn’t “magic” — it’s just smart use of proxies and AOP. Now the interesting question: If method A and method B are in the same bean, and B is annotated with @Transactional, and A calls B internally… 👉 How would you make sure the transaction actually works? #SpringBoot #BackendEngineering #Java #SystemDesign #Transactional #AOP
To view or add a comment, sign in
-
-
Day 03 – Bean Scopes (Singleton vs Prototype vs Request vs Session) What is Bean Scope? Bean Scope defines: • How many instances of a bean Spring creates • How long the bean lives By default, Spring creates only one object per bean. ==================================== Singleton (Default Scope) ==================================== Only ONE object is created for the entire application. @Component public class StudentService { } Spring creates one StudentService object and shares it everywhere. Memory efficient Best for stateless services Used in most backend applications In real projects: Controllers, Services, Repositories → Mostly Singleton ===================================== Prototype Scope ===================================== Every time the bean is requested → New object is created. @Component @Scope("prototype") public class StudentService { } Used when: • Object holds state • You need independent instances Not very common in REST APIs, but useful in specific cases. ======================================= Request Scope (Web Applications) ======================================= One bean per HTTP request. Each client request gets a new object. Used when: • You need request-specific data • Temporary processing per request ==================================== Session Scope ==================================== One bean per user session. Each logged-in user gets a separate object. Used when: • Storing user-specific session data Why This Matters in Real Backend Systems? Because scope : • Memory usage • Performance • Thread safety • Scalability ================================== Today’s Learning Spring does not just create objects. Understanding Bean Scope helps you think like a backend engineer. Tomorrow: Types of Dependency Injection (Constructor vs Setter vs Field) #Java #SpringBoot #SpringFramework #BackendDevelopment #Microservices #LearningInPublic
To view or add a comment, sign in
-
🚀 Dependency Injection: The Heart of Spring Most developers write code where objects create their own dependencies. Spring flips this on its head — and that's what makes it powerful. Dependency Injection (DI) means an object receives its dependencies from the outside instead of creating them itself. The IoC (Inversion of Control) container manages object creation, wiring, and lifecycle. // ❌ Without DI — tight coupling public class OrderService { private PaymentService payment = new PaymentService(); // hard-coded! } // ✅ With DI — loose coupling @Service public class OrderService { private final PaymentService paymentService; public OrderService(PaymentService paymentService) { this.paymentService = paymentService; // injected by Spring } } Why does this matter? ✔ Testability — swap real dependencies with mocks ✔ Flexibility — change implementations without touching consumers ✔ Single Responsibility — objects focus on their job, not wiring ✔ Less boilerplate — Spring handles object lifecycle Spring's ApplicationContext is the IoC container. It scans your classes, creates beans, resolves dependencies, and injects them automatically. You declare what you need; Spring figures out how to provide it. This is the concept everything in Spring Boot builds upon. Master this, and the rest clicks into place. #Java #SpringBoot #BackendDevelopment #DependencyInjection #IoC #SoftwareEngineering
To view or add a comment, sign in
-
Spring and Boot always have struck me as unusual names for a framework. Someday I'll have to ask someone about the history behind those choices. There's no doubt though that Spring Boot is the play in the Enterprise Java space, and I'm pleased to present Testing Spring Boot Applications by Daniel Garnier-Moiroux as one of our newest additions to the Manning Early Access Program. Spring Boot With Microservices #Spring #Boot #Java Manning Publications Co. https://lnkd.in/gTmQxJ97
To view or add a comment, sign in
-
Day 02 – Bean Lifecycle & ApplicationContext ================================================ Yesterday I started with Spring Core fundamentals. Today I went deeper into something that actually makes Spring powerful Bean Lifecycle and ApplicationContext. Most developers use Spring. But not everyone understands what happens internally when the application starts. ================================================ What is ApplicationContext? ApplicationContext is the Spring container. When we run a Spring Boot application: SpringApplication.run(Application.class, args); Spring creates an ApplicationContext. From that moment, Spring starts: • Creating objects (Beans) • Injecting dependencies • Managing lifecycle • Handling configurations • Managing resources It becomes the brain of the application. ============================================= What is a Bean Lifecycle? A Spring Bean goes through multiple stages from creation to destruction. Here is what actually happens: 1. Spring creates the object (Constructor runs) 2. Dependencies are injected 3. Initialization logic executes 4. Bean is ready to use 5. When application shuts down → destroy logic runs Practical Example @Component public class StudentService { public StudentService() { System.out.println("Constructor called"); } @PostConstruct public void init() { System.out.println("Bean initialized"); } @PreDestroy public void cleanup() { System.out.println("Bean destroyed"); } } What happens here? * Constructor → Object creation * @PostConstruct → Runs after dependency injection * @PreDestroy → Runs before application shutdown This is the full lifecycle. ============================================= Spring is not just annotations. It is a container that controls object creation, initialization, and destruction. Understanding Bean Lifecycle makes you think like a backend engineer not just someone who writes APIs. Tomorrow: Bean Scopes (Singleton vs Prototype vs Request vs Session) * #Java #SpringBoot #SpringFramework #BackendDevelopment #Microservices #LearningInPublic
To view or add a comment, sign in
-
Hi everyone 👋 📌 Spring Boot Annotation Series Part 22– @RequestParam @RequestParam is used to read query parameters from the request URL and bind them to method parameters. It is part of the Spring Framework and commonly used in REST APIs built with Spring Boot. 🔹 Why do we use @RequestParam? Sometimes we need to pass additional information through the URL. Example: GET /users?age=25 Here, age is a query parameter. @RequestParam helps us capture that value in the controller method. 🔹 Simple Example @RestController @RequestMapping("/users") public class UserController { @GetMapping("/search") public String getUserByAge(@RequestParam int age) { return "User age is: " + age; } } 👉 Request URL: GET http://localhost:8080/users/search?age=25 Output: User age is: 25 🔹 In Simple Words @RequestParam takes values from query parameters in the URL and passes them to the controller method. 👉 🧠 Quick Understanding Used in filtering, search APIs Can be optional using required=false Can provide default values Used to read query parameters from URL Mostly used in GET APIs #SpringBoot #Java #RESTAPI #RequestParam #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
🚀 Dependency Injection in Spring –(Constructor & Setter Injection) In the Spring Framework, Dependency Injection (DI) is a core concept used to build flexible and loosely coupled applications. 🧩 What is Dependency Injection? (Detailed Definition) Dependency Injection is a design pattern in which: The required dependencies of a class are provided from outside by the Spring container, instead of being created inside the class. 👉 In simple words: A class does not create the objects it needs — it receives them from the container. 🧠 Why Dependency Injection is Important Without DI: Classes create their own dependencies Leads to tight coupling Difficult to modify and test With DI: ✔️ Promotes loose coupling ✔️ Improves code reusability ✔️ Makes testing easier ✔️ Enhances maintainability ⚙️ How Dependency Injection Works in Spring Spring IoC container starts It creates all objects (called beans) It identifies dependencies in each class Injects required dependencies Manages object lifecycle 🔍 Types of Dependency Injection 1️⃣ Constructor Injection 📖 Definition: In Constructor Injection, dependencies are provided to a class through its constructor at the time of object creation. 🧠 Explanation: A constructor is used to initialize the object All required dependencies are passed as parameters The object is created only when all dependencies are available 🎯 Characteristics: ✔️ Ensures mandatory dependencies are provided ✔️ Object is created in a fully initialized state ✔️ Promotes immutability (dependencies cannot be changed later) ✔️ Safer and more reliable ⚠️ Limitation: Not suitable for optional dependencies Can become complex if too many dependencies are required 2️⃣ Setter Injection 📖 Definition: In Setter Injection, dependencies are provided using setter methods after the object is created. 🧠 Explanation: Object is created first (empty state) Dependencies are injected later using setter methods Allows modifying dependencies anytime 🎯 Characteristics: ✔️ Supports optional dependencies ✔️ More flexible than constructor injection ✔️ Dependencies can be changed at runtime ⚠️ Limitation: Object may remain in an incomplete state if dependency is not set Less safe compared to constructor injection 🎯 When to Use What? 👉 Use Constructor Injection when: Dependency is mandatory Object must be fully initialized 👉 Use Setter Injection when: Dependency is optional You need flexibility to change it ✨ Conclusion Dependency Injection is a fundamental concept in Spring that separates object creation from business logic, making applications more modular and maintainable. #Java #SpringFramework #DependencyInjection #BackendDevelopment #SoftwareEngineering #Coding thanks to: Anand Kumar Buddarapu Saketh Kallepu Uppugundla Sairam
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development