Deep dive into Spring Framework – Understanding Beans 🌱 Today, I focused on one of the most important concepts in Spring: Beans. ➤ A Spring Bean is a Java object that is created, configured, and managed by the Spring IoC container. Instead of manually handling objects, Spring takes care of their lifecycle and dependencies. ➤ Beans can be created using annotations like @Component, @Service, @Repository, @Controller, or explicitly using @Bean inside @Configuration classes. These annotations allow Spring to automatically detect and manage objects. ➤ I also learned about the Bean Lifecycle, which includes instantiation, dependency injection, initialization, and destruction—giving better control over how objects behave during application runtime. ➤ Lastly, I explored Bean Scopes such as singleton (default), prototype, request, and session, which define how long a bean instance lives and how it’s shared. Strong fundamentals here make Spring applications clean, scalable, and maintainable. Learning Spring made easier thanks to Anuj Kumar Sharma #SpringFramework #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #LearningJourney
Understanding Spring Beans: Lifecycle and Configuration
More Relevant Posts
-
Understanding the 𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗲𝗮𝗻 𝗟𝗶𝗳𝗲𝗰𝘆𝗰𝗹𝗲 is the "level up" every Java developer needs. It’s not just about creating objects; it’s about how Spring manages their entire existence—from birth to destruction. 📍 𝗣𝗵𝗮𝘀𝗲 𝟭: 𝗧𝗵𝗲 𝗦𝗰𝗼𝗽𝗲𝘀 (𝗪𝗵𝗲𝗿𝗲 & 𝗛𝗼𝘄 𝗹𝗼𝗻𝗴?) Before a bean is born, Spring needs to know its scope. Here are the most common ones: • 𝗦𝗶𝗻𝗴𝗹𝗲𝘁𝗼𝗻 (𝗗𝗲𝗳𝗮𝘂𝗹𝘁): One instance per Spring IoC container. Perfect for stateless services. • 𝗣𝗿𝗼𝘁𝗼𝘁𝘆𝗽𝗲: A new instance every time it's requested. Use this for stateful beans. • 𝗥𝗲𝗾𝘂𝗲𝘀𝘁: One instance per HTTP request (Web-aware). • 𝗦𝗲𝘀𝘀𝗶𝗼𝗻: One instance per HTTP session. • 𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻/𝗚𝗹𝗼𝗯𝗮𝗹 𝗦𝗲𝘀𝘀𝗶𝗼𝗻: Scoped to the Lifecycle of a ServletContext. ⚙️ 𝗣𝗵𝗮𝘀𝗲 𝟮: 𝗧𝗵𝗲 𝗟𝗶𝗳𝗲𝗰𝘆𝗰𝗹𝗲 (𝗧𝗵𝗲 "𝗛𝗼𝘄") The journey of a Bean follows a very specific path: • 𝗜𝗻𝘀𝘁𝗮𝗻𝘁𝗶𝗮𝘁𝗶𝗼𝗻: The JVM creates the bean instance. • 𝗣𝗼𝗽𝘂𝗹𝗮𝘁𝗲 𝗣𝗿𝗼𝗽𝗲𝗿𝘁𝗶𝗲𝘀: Dependency Injection (DI) happens here. • 𝗔𝘄𝗮𝗿𝗲 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀: Spring calls setBeanName, setBeanFactory, etc. • 𝗕𝗲𝗮𝗻 𝗣𝗼𝘀𝘁-𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗼𝗿𝘀 (𝗕𝗲𝗳𝗼𝗿𝗲 𝗜𝗻𝗶𝘁): Custom logic before the bean is ready. • 𝗜𝗻𝗶𝘁𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: @PostConstruct or afterPropertiesSet() is triggered. • 𝗕𝗲𝗮𝗻 𝗣𝗼𝘀𝘁-𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗼𝗿𝘀 (𝗔𝗳𝘁𝗲𝗿 𝗜𝗻𝗶𝘁): The bean is wrapped (e.g., for AOP/Proxies). • 𝗥𝗘𝗔𝗗𝗬: The bean is now live in the container! • 𝗗𝗲𝘀𝘁𝗿𝘂𝗰𝘁𝗶𝗼𝗻: When the context closes, @PreDestroy cleans everything up. #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #SpringFramework #CodingTips
To view or add a comment, sign in
-
Hi everyone 👋 Continuing the weekday Spring Boot series with another useful annotation 👇 📌 Spring Boot Annotation Series – Part 6 ✅ @Value annotation The @Value annotation is used to inject values into Spring beans 👇 🔹 Why do we use @Value? - To read values from application.properties or application.yml - To avoid hardcoding values in code - To manage environment-specific configurations 🔹 Where can we use @Value? To inject: - Configuration values - Environment variables - Default values 🔹 Simple example properties - server.port=8081 app.name=MySpringApp in java code - @Value("${server.port}") private int port; @Value("${app.name}") private String appName; 🔹 In simple words @Value helps us take values from configuration files and use them directly in our code. 👉 🧠 Quick Understanding (My Notes) - @Value injects external values into fields - Mostly used with application.properties - Helps keep code clean and configurable #SpringBoot #Java #ValueAnnotation #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
🚀 What Exactly Is a Bean in Spring? (Simple but Important) Sometimes the simplest questions are the ones we forget to explain clearly. As Spring developers, we often say: “Bean is just an object managed by Spring IoC.” But what does that really mean? In the Spring Framework: A Bean is an object that is: ✔ Created by the Spring IoC container ✔ Managed by the container ✔ Injected using Dependency Injection (DI) ✔ Stored inside the ApplicationContext ✔ Controlled through its lifecycle If you create an object using new, it’s just a normal object. If Spring creates and manages it — it becomes a Bean. Example: @Service public class PaymentService { } Because of @Service, Spring registers this class as a Bean and manages it inside the container. 💡 Why this matters? Understanding Beans properly helps in: Debugging injection issues Avoiding NoUniqueBeanDefinitionException Managing lifecycle correctly Writing clean architecture Sometimes we use Spring daily but forget the fundamentals behind it. Still learning. Still refining basics. #SpringBoot #Java #BackendDevelopment #CleanCode #SoftwareEngineering
To view or add a comment, sign in
-
🔍 Confused about JDK, JRE, and JVM? Let me break it down: JVM (Java Virtual Machine): The engine that runs Java code. Makes Java platform-independent.JRE (Java Runtime Environment): JVM + libraries needed to run Java applications.JDK (Java Development Kit) JRE + development tools (compiler, debugger). Think of it like a car: JVM = Engine, JRE = Complete car to driveJDK = Car + mechanic tools to build/fix. You need the JDK to write Java programs, and the JRE to run them.#JavaBasics #Programming #TechEducationHave Have you ever felt confused about JDK, JRE, and JVM? Let me clarify these terms for you. The JVM, or Java Virtual Machine, is the engine that runs Java code, making Java platform-independent. The JRE, or Java Runtime Environment, includes the JVM along with the libraries needed to run Java applications. On the other hand, the JDK, or Java Development Kit, encompasses the JRE along with development tools like the compiler and debugger. To put it simply, think of it like a car analogy: - JVM = Engine - JRE = Complete car to drive - JDK = Car + mechanic tools to build or fix To write Java programs, you need the JDK, and to run them, you require the JRE. Understanding these components is crucial for any Java developer.
To view or add a comment, sign in
-
📌 Spring Boot Annotation Series – Part 3 ✅ @Configuration Ever wondered where Spring creates beans from @Bean methods? That’s exactly what @Configuration is for 👇 🔹 Why do we use @Configuration? @Configuration tells Spring: 👉 “This class contains bean definitions.” It is used to define beans using Java code instead of XML. 🔹 When do we use @Configuration? 1)When we want to create beans using @Bean 2)When we need to set up things like: - Database connection (DataSource) - RestTemplate - Security configuration - Common utility beans 3) When we are using external libraries and need to create beans manually 🔹 How does it work? Spring creates a proxy of the configuration class, so that each @Bean method returns the same singleton object. 🔹 Simple example @Configuration public class AppConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } In simple words, when a class is annotated with @Configuration, Spring treats it like a configuration file and scans it for @Bean methods. These beans are then managed by the Spring container. #SpringBoot #Java #Configuration #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
STOP choosing between "Readable Code" and "High Performance." Java 21+ just ended that debate. ☕️ For years, Java developers faced a frustrating trade-off: 1️⃣ 𝐓𝐡𝐞 𝐒𝐢𝐦𝐩𝐥𝐞 𝐖𝐚𝐲: Write standard blocking code. It’s easy to debug but hits a "memory wall" because OS threads are heavy (~1MB each). 2️⃣ 𝐓𝐡𝐞 𝐑𝐞𝐚𝐜𝐭𝐢𝐯𝐞 𝐖𝐚𝐲: Use WebFlux or CompletableFuture. It scales beautifully but turns your codebase into "Callback Hell." 𝐄𝐧𝐭𝐞𝐫: 𝐉𝐚𝐯𝐚 𝐕𝐢𝐫𝐭𝐮𝐚𝐥 𝐓𝐡𝐫𝐞𝐚𝐝𝐬 (𝐏𝐫𝐨𝐣𝐞𝐜𝐭 𝐋𝐨𝐨𝐦). After 6 years of working with traditional threading, I’m finally seeing the "Third Way." 𝐖𝐡𝐚𝐭 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐜𝐡𝐚𝐧𝐠𝐞𝐝? Traditional threads are like 𝐒𝐞𝐦𝐢-𝐓𝐫𝐮𝐜𝐤𝐬 - they take up massive space on the highway. Virtual Threads are like 𝐁𝐢𝐜𝐲𝐜𝐥𝐞 𝐂𝐨𝐮𝐫𝐢𝐞𝐫𝐬. They are managed by the JVM, not the OS. You can literally spawn MILLIONS of them on a single laptop. 💻 𝐓𝐇𝐄 𝐈𝐌𝐏𝐋𝐄𝐌𝐄𝐍𝐓𝐀𝐓𝐈𝐎𝐍 (𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 𝟑.𝟐+) The best part? You don't even need to change your business logic. 𝐁𝐄𝐅𝐎𝐑𝐄 (𝐓𝐫𝐚𝐝𝐢𝐭𝐢𝐨𝐧𝐚𝐥 𝐏𝐨𝐨𝐥𝐢𝐧𝐠): You had to carefully tune your thread pool to avoid crashing your server. server.tomcat.threads.max: 200 𝐍𝐎𝐖 (𝐕𝐢𝐫𝐭𝐮𝐚𝐥 𝐓𝐡𝐫𝐞𝐚𝐝𝐬): One line of config tells Spring Boot to run every request on a new Virtual Thread. spring.threads.virtual.enabled: true 𝐓𝐡𝐞 𝐫𝐞𝐬𝐮𝐥𝐭? Your standard, easy-to-read @RestController can now handle 10,000+ concurrent requests without breaking a sweat. 𝐌𝐲 𝐓𝐚𝐤𝐞 : We spent a decade making code more complex to gain performance. Now, the JVM is doing the heavy lifting so we can get back to solving business problems. Are you already migrating to Java 21, or sticking with the "Tried and True" for now? Let’s discuss below! 👇 #Java #SpringBoot #ProjectLoom #BackendDevelopment #SoftwareArchitecture #Scalability
To view or add a comment, sign in
-
📌 Spring Boot Annotation Series ✅ @Configuration Ever wondered where Spring creates beans from @Bean methods? That’s exactly what @Configuration is for 👇 🔹 Why do we use @Configuration? @Configuration tells Spring: 👉 “This class contains bean definitions.” It is used to define beans using Java code instead of XML. 🔹 When do we use @Configuration? 1)When we want to create beans using @Bean 2)When we need to set up things like: - Database connection (DataSource) - RestTemplate - Security configuration - Common utility beans 3) When we are using external libraries and need to create beans manually 🔹 How does it work? Spring creates a proxy of the configuration class, so that each @Bean method returns the same singleton object. 🔹 Simple example @Configuration public class AppConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } In simple words, when a class is annotated with @Configuration, Spring treats it like a configuration file and scans it for @Bean methods. These beans are then managed by the Spring container. #SpringBoot #Java #Configuration #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
🚀 Stop Writing Boilerplate: Java Records in 17+ If you are still writing private final fields, constructors, getters, equals(), hashCode(), and toString() for simple data carriers, it's time to switch to Records. Introduced as a standard feature in Java 17, Records provide a compact syntax to model immutable data. Why use them? ✅ Conciseness: 1 line of code replaces 30+ lines of boilerplate. ✅ Immutability by default: Thread-safe and predictable. ✅ Intent: Explicitly declares that a class is a pure data carrier. // The old way (before Java 14/16) public class User { private final String name; private final int age; public User(String name, int age) { this.name = name; this.age = age; } // ... getters, equals, hashCode, toString ... } // The Java 17 way public record User(String name, int age) {} #java #java17 #programming #softwareengineering #backend
To view or add a comment, sign in
-
Explore related topics
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