🚀 Day 13/100 - Spring Boot Annotations - 4️⃣ @Configuration & @Bean ➡️ @Configuration 🔹What is it - A class-level annotation which tells Spring that this class contains bean definitions for the IoC container. - It is a modern replacement for XML-based configuration. 🔹Why to use - Makes configuration type-safe - Easy to read and maintain - Fully Java-based (no XML) ➡️ @Bean 🔹What is it - A method-level annotation used inside a @Configuration class to explicitly create and register a bean in the Spring container. 🔹Why to use - Useful when you cannot modify a class (e.g., third-party libraries) - Gives full control over object creation and configuration 📌 Key Takeaway @Configuration → where beans are defined @Bean → how a bean is created Next post: https://lnkd.in/d3pztG-x Previous post: https://lnkd.in/d8NzCVkB #100Days #SpringBoot #Java #BackendDevelopment #SoftwareEngineering
Spring Boot Annotations: @Configuration & @Bean Explained
More Relevant Posts
-
𝗢𝗻𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗺𝗼𝘀𝘁 𝗽𝗼𝘄𝗲𝗿𝗳𝘂𝗹 𝗹𝗶𝗻𝗲𝘀 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗼𝗱𝗲 𝗶𝘀 𝗮𝗹𝘀𝗼 𝗼𝗻𝗲 𝗼𝗳 𝘁𝗵𝗲 𝘀𝗵𝗼𝗿𝘁𝗲𝘀𝘁 𝗶𝗻 𝘀𝗽𝗿𝗶𝗻𝗴 𝗯𝗼𝗼𝘁 𝗮𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝘀. The annotation @𝗦𝗽𝗿𝗶𝗻𝗴𝗕𝗼𝗼𝘁𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻 does more work under the hood It is a 𝗰𝗼𝗺𝗽𝗼𝘀𝗶𝘁𝗲 𝗮𝗻𝗻𝗼𝘁𝗮𝘁𝗶𝗼𝗻 meaning @SpringBootApplication combines multiple annotations into one annotation (@𝗦𝗽𝗿𝗶𝗻𝗴𝗕𝗼𝗼𝘁𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻, @𝗘𝗻𝗮𝗯𝗹𝗲𝗔𝘂𝘁𝗼𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻 and @𝗖𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁𝗦𝗰𝗮𝗻) 1. 𝗦𝗽𝗿𝗶𝗻𝗴𝗕𝗼𝗼𝘁𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻: Marker annotations that marks the class as Configuration class. 2. 𝗘𝗻𝗮𝗯𝗹𝗲𝗔𝘂𝘁𝗼𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻: Enables Spring boot to automatically configure any components that spring framework thinks your application will needs. 3. 𝗖𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁𝗦𝗰𝗮𝗻: Enables Spring to scan your application class path and will register the class that are annotated(with Component, Service, Repository, Controller) as components(bean) in Spring application context( container) When you run your application, 𝗺𝗮𝗶𝗻() method is invoked, this 𝗺𝗮𝗶𝗻() calls static 𝗿𝘂𝗻() method on SpringApplication class which bootstraps the spring application and create Spring Application Context (container) Look at the code example how short and simple it is. 💪 #Day2 #Java #JavaDeveloper #Backend #Spring #Springboot
To view or add a comment, sign in
-
-
📌 Spring Boot Annotation Series – Part 8 ✅ @Component annotation The @Component annotation is used to mark a class as a Spring-managed bean 👇 🔹 Why do we use @Component? To tell Spring: 👉 “Create an object of this class and manage it.” - To enable dependency injection - To let Spring detect the class during component scanning 🔹 How does it work? When Spring Boot starts: - It scans packages (using @ComponentScan) - Finds classes annotated with @Component - Creates and registers them as beans in the Spring container 🔹 Simple example @Component public class EmailService { public void sendEmail() { System.out.println("Email sent"); } } Now this class can be injected using: @Autowired private EmailService emailService; 🔹 In simple words @Component tells Spring to automatically create and manage this class as a bean. 👉 🧠 Quick Understanding - Marks a class as a Spring bean - Detected during component scanning - Enables dependency injection #SpringBoot #Java #Component #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
-
𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗼𝗼𝘁 — 𝗠𝗮𝗱𝗲 𝗦𝗶𝗺𝗽𝗹𝗲 🚀 (𝗣𝗮𝗿𝘁 𝟲) Never trust user input. 𝘜𝘴𝘦𝘳𝘴 𝘤𝘢𝘯 𝘴𝘦𝘯𝘥: 𝘌𝘮𝘱𝘵𝘺 𝘧𝘪𝘦𝘭𝘥𝘴. 𝘞𝘳𝘰𝘯𝘨 𝘧𝘰𝘳𝘮𝘢𝘵𝘴. 𝘐𝘯𝘷𝘢𝘭𝘪𝘥 𝘥𝘢𝘵𝘢. And if you don’t validate properly? Bad data enters your system and causes problems later. In this carousel, I break down: • Why 𝘷𝘢𝘭𝘪𝘥𝘢𝘵𝘪𝘰𝘯 𝘮𝘢𝘵𝘵𝘦𝘳𝘴 • How @𝗩𝗮𝗹𝗶𝗱 𝘄𝗼𝗿𝗸𝘀 • Common constraints like @𝗡𝗼𝘁𝗡𝘂𝗹𝗹, @𝗦𝗶𝘇𝗲, @𝗘𝗺𝗮𝗶𝗹 • How to handle 𝘃𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻 𝗲𝗿𝗿𝗼𝗿𝘀 𝗰𝗹𝗲𝗮𝗻𝗹𝘆 𝗡𝗼 𝗷𝗮𝗿𝗴𝗼𝗻. 𝗡𝗼 𝘂𝗻𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 𝘁𝗵𝗲𝗼𝗿𝘆. 𝗝𝘂𝘀𝘁 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗼𝗼𝘁. 📌 Save this if you're building APIs 👉 Follow for Part 7 - Spring Boot JPA Basics #SpringBoot #SpringFramework #Java #JavaDeveloper #BackendDevelopment #LearnJava #RESTAPI #SoftwareEngineering #SpringBootMadeSimple
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
-
🌱 My Spring Journey : Dependency Lookup -> Here, the target class writes the logic to obtain the dependent class object, and for this it uses the getBean(..) method. -> If the dependent class object is required in multiple methods of the target class, we should prefer Dependency Injection; otherwise, we can go for Dependency Lookup. Limitations of Dependency Lookup : -> For this approach, we need to create or access an extra IOC container inside the business method of the target class. -> Due to this, even singleton-scoped Spring beans may get pre-instantiated multiple times (when multiple containers are created). -> This extra IOC container creation process generates in-memory metadata for the Spring bean configuration files, which can lead to memory issues. -> To overcome this problem we can use Interface injection #SpringFramework #DependencyLookup #DependencyInjection #Java #SpringCore #BackendDevelopment #LearningJourney
To view or add a comment, sign in
-
☕ Why Using var Everywhere Can Hurt Java + Spring Boot Readability var makes Java code shorter but not always clearer. In Spring Boot projects, I’ve seen code like: 🔹 var data = service.process(input); 🔹 var result = repository.findById(id); Now the reader has to guess the type 😅 What this leads to: "Harder code reviews" "Slower onboarding" "Less readable business logic" What works better: ✅ Use var when the type is obvious ✅ Avoid var in public APIs and method returns ✅ Prefer explicit types for domain objects Shorter code is nice — understandable code is better. 👉 Readability always beats cleverness. #CoreJava #SpringBoot #CleanCode #BackendDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
🚀 How a DTO Saved Me in Spring Boot I had a form that needed to save data into two tables from one page. Binding directly to two entities? Errors everywhere. 😅 Solution? DTO. I created a DTO holding all fields, bound the form to it, and then mapped it to the two entities. Result: clean code, no binding errors, form submits perfectly. 💥 Lesson: Sometimes a simple DTO is all it takes to fix complex binding problems. #SpringBoot #Java #DTO #BackendDevelopment #DeveloperLife
To view or add a comment, sign in
-
Great article by Fahim Fahad about how to reduce the Spring Boot start up time using Class Data Sharing (CDS) https://lnkd.in/dyVqe2TY #Spring #SpringBoot #Java #Kotlin #Performance #Optimization #ClassDataSharing
To view or add a comment, sign in
-
🌱 Bean Creation in Spring — More Important Than It Looks In Spring Boot, we use beans everywhere — but many developers don’t fully think about how they are created and managed. A Spring Bean is simply an object managed by the IoC (Inversion of Control) container. 🔹 Beans can be created using: • @Component, @Service, @Repository • @Configuration + @Bean • Auto-configuration 🔹 Why Bean management matters: • Dependency Injection reduces tight coupling • Singleton scope improves performance (default scope) • Lifecycle management (@PostConstruct, @PreDestroy) ensures controlled initialization Good backend systems are not just about writing classes — they’re about letting the container manage object creation efficiently. Understanding bean lifecycle = better Spring architecture. #Java #SpringBoot #SpringFramework #DependencyInjection #BackendEngineering
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