🚀 Spring Boot Daily Learning Are you using @Component on every class and wondering why Spring gives you 4 different stereotype annotations? Here's what actually separates them — and why it matters in production. Spring's stereotype annotations all register beans in the IoC container, but they carry different semantic weight and unlock different framework features: @Component // Generic bean — use as last resort @Service // Business logic layer @Repository // Data access layer + exception translation @Controller // MVC presentation layer (with @RestController for REST) The critical difference? @Repository activates Spring's PersistenceExceptionTranslationPostProcessor — it automatically translates low-level JPA/Hibernate exceptions into Spring's unified DataAccessException hierarchy. Your service layer never leaks Hibernate internals. @Service and @Controller signal architectural intent. Spring AOP, Spring Security, and your teammates all rely on these contracts to apply cross-cutting concerns correctly. Using @Component everywhere breaks that contract. Best practice: Always pick the most specific annotation. @Component is for custom infrastructure beans — not business or data code. #Java #SpringBoot #BackendDevelopment #SpringFramework #CleanCode
Jānis Ošs’ Post
More Relevant Posts
-
Hi everyone 👋 Continuing the Spring Boot Annotation Series 👇 📌 Spring Boot Annotation Series Part 25 – @ModelAttribute @ModelAttribute is used to bind request data (form data / query params) to a Java object. It is part of the Spring Framework and mainly used in Spring MVC applications. 🔹 Why do we use @ModelAttribute? When we receive multiple values from a request (like form data), instead of handling each parameter separately, we can bind them directly to an object. 👉 Makes code clean and structured. 🔹 Where is it used? Form submissions (HTML forms) Query parameters MVC applications (not mostly REST APIs) 🔹 In Simple Words @ModelAttribute takes request data and converts it into a Java object. 👉 🧠 Quick Understanding Binds request data to object Used in form handling Works with query/form data Not mainly used for JSON #SpringBoot #Java #ModelAttribute #SpringMVC #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
Hi everyone 👋 Continuing the Spring Boot Annotation Series 👇 📌 Spring Boot Annotation Series part 24 – @ResponseBody @ResponseBody is used to return data directly in the HTTP response body instead of returning a view (HTML page). It is part of the Spring Framework and is commonly used in REST APIs built with Spring Boot. 🔹 Why do we use @ResponseBody? In Spring MVC: Without @ResponseBody → returns a view (HTML/JSP) With @ResponseBody → returns data (JSON/XML) 👉 It is mainly used for building REST APIs. 🔹 Simple Example @Controller public class UserController { @GetMapping("/user") @ResponseBody public String getUser() { return "User details"; } } 👉 Output will be directly shown in response body, not a view. 🔹 Returning Object as JSON @GetMapping("/user") @ResponseBody public User getUser() { return new User(1, "Asmita"); } 👉 Spring automatically converts it into JSON. 🔹 How does it work? - Spring uses Jackson library internally - Converts Java object → JSON - Sends it in HTTP response 🔹 In Simple Words @ResponseBody tells Spring to send data directly in the response instead of returning a web page. 👉 🧠 Quick Understanding - Returns data, not view - Converts object → JSON - Used in REST APIs - Not needed with @RestController #SpringBoot #Java #ResponseBody #RESTAPI #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
New to Spring Boot? You'll see these annotations in every project. Here's what they actually do: @SpringBootApplication → Entry point. Combines @Configuration, @EnableAutoConfiguration, @ComponentScan @RestController → Marks a class as an HTTP request handler that returns data (not views) @Service → Business logic layer. Spring manages it as a bean @Repository → Data access layer. Also enables Spring's exception translation @Autowired → Inject a dependency automatically (prefer constructor injection instead) @GetMapping / @PostMapping / @PutMapping / @DeleteMapping → Maps HTTP methods to your handler methods @RequestBody → Deserializes JSON from request body into a Java object @PathVariable → Extracts values from the URL path Bookmark this. You'll refer back to it constantly. Which annotation confused you the most when starting out? 👇 #Java #SpringBoot #Annotations #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 47/90 How Spring Boot loads 130+ AutoConfigurations (and how to debug them) Today’s learning connected two very important concepts: 👉 How Spring Boot loads auto-configurations internally 👉 How we can see which ones are applied or skipped 🔹 How Spring Boot loads AutoConfigurations? Spring Boot doesn’t magically configure everything. It follows a structured process: 1️⃣ Your application starts with @SpringBootApplication 2️⃣ This internally includes @EnableAutoConfiguration 3️⃣ Spring Boot then scans a special file: 📄 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports This file contains a list of 100+ AutoConfiguration classes like: - DataSourceAutoConfiguration - HibernateJpaAutoConfiguration - WebMvcAutoConfiguration 4️⃣ These classes are loaded into the Spring context But here’s the catch 👇 👉 They are NOT blindly applied Each auto-configuration uses conditions like: @ConditionalOnClass @ConditionalOnBean @ConditionalOnProperty So Spring Boot decides: ✔ Apply if conditions match ❌ Skip if conditions fail 🔹 How to see this decision (VERY IMPORTANT 🔥) We can enable debugging using this property: debug=true 📍 Add it in application.properties 🔹 What happens after enabling this? At application startup, Spring prints a Condition Evaluation Report in logs: You will see: ✅ Positive Matches Configurations that were successfully applied ❌ Negative Matches Configurations that were skipped + reason ⚠️ Conditional Matches Dependent configurations 🔹 Example Insight You might see logs like: ✔ DataSourceAutoConfiguration matched ❌ JpaRepositoriesAutoConfiguration did not match (missing dependency) This tells you EXACTLY why something is working or not. 🔹 Why this is powerful? ▪️Understand which beans Spring created automatically ▪️Debug issues in: JPA / Hibernate Security DataSource 🔹 Pro Tip ⭐ Instead of full debug, you can use targeted logging: logging.level.org.springframework.boot.autoconfigure=DEBUG 🔹 Today's Tiny Win 💡 Today you moved from: 👉 “Spring Boot works magically” to 👉 “I can SEE and DEBUG Spring Boot decisions internally”🍀☘️ #SpringBoot #AutoConfiguration #Java #BackendDeveloper #90DaysChallenge
To view or add a comment, sign in
-
💡 Types of Dependencies in Spring Framework: In the 🌱 Spring Framework, Dependency Injection (DI) is a core concept that helps achieve loose coupling and better code maintainability. Let’s explore the three main types of dependencies used in Spring: 🔹 1. Primitive Dependency This involves injecting simple values like int, double, boolean, or String. 👉 Example: Injecting a username, age, or configuration value into a bean. ✔️ Configured using @Value annotation or XML ✔️ Commonly used for constants and environment properties ✔️ Lightweight and easy to manage 💡 Use case: Application properties like app name, port number, etc. 🔹 2. Collection Dependency Spring allows injecting collections such as List, Set, Map, or Properties. 👉 Example: List of email recipients Map of key-value configurations ✔️ Supports bulk data injection ✔️ Can be configured via XML or annotations ✔️ Useful for dynamic and flexible data handling 💡 Use case: Storing multiple values like roles, configurations, or URLs. 🔹 3. Reference Dependency (Object Dependency) This is the most important type where one bean depends on another bean. 👉 Example: OrderService depends on PaymentService Car depends on Engine ✔️ Achieved using @Autowired, @Inject, or XML <ref> ✔️ Promotes loose coupling ✔️ Core concept behind Spring IoC Container 💡 Use case: Service layer calling repository layer, or controller calling service. 🚀 Why Dependency Injection Matters in Spring? Reduces tight coupling between classes Makes unit testing easier Improves code reusability and maintainability Enables better separation of concerns 🔥 Pro Tip: Prefer constructor injection over field injection for better immutability and testability. #SpringFramework #Java #DependencyInjection #BackendDevelopment #SoftwareEngineering #TechLearning Anand Kumar Buddarapu
To view or add a comment, sign in
-
-
Understanding Annotations in Spring Boot: Annotations play a key role in how Spring Boot applications are built and configured. But what exactly are they? Annotations are metadata added to Java code that tell the Spring framework how to behave or configure certain components. Instead of writing large configuration files, Spring Boot uses annotations to simplify development. Some commonly used annotations include: 🔹 @SpringBootApplication Marks the main class and enables auto-configuration, component scanning, and configuration support. 🔹 @RestController / @Controller Used to handle incoming HTTP requests and return responses. 🔹 @Service Indicates that a class contains business logic. 🔹 @Repository Used for database interaction and persistence operations. 🔹 @Autowired Allows Spring to automatically inject dependencies. ✅ These annotations help reduce boilerplate code and make Spring Boot applications easier to build and manage. Understanding how and why annotations are used is an important step toward mastering Spring Boot. #SpringBoot #Java #BackendDevelopment #SoftwareDevelopment
To view or add a comment, sign in
-
𝗪𝗵𝗮𝘁 𝗿𝗲𝗮𝗹𝗹𝘆 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝘄𝗵𝗲𝗻 𝘆𝗼𝘂 𝗰𝗮𝗹𝗹 `findById()` 𝗶𝗻 𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗼𝗼𝘁? 🤯 As developers, we often write: `userRepository.findById(1L);` Looks simple, right? But behind this *one line*, there’s a complete system design in action. Let’s break the illusion 👇 ⚙️ 𝗜𝘁’𝘀 𝗡𝗢𝗧 𝘆𝗼𝘂𝗿 𝗰𝗼𝗱𝗲 That repository you wrote is just an interface. Spring creates a runtime proxy using dynamic proxies (JDK/CGLIB). 👉 You are calling a generated object, not your implementation. --- 🧠 𝗧𝗵𝗲 𝗥𝗲𝗮𝗹 𝗙𝗹𝗼𝘄 Controller → Service → Repository (Proxy) → JPA → Hibernate → JDBC → Connection Pool → Database --- 🔥 𝗝𝗣𝗔 𝗶𝘀 𝗝𝘂𝘀𝘁 𝗮 𝗦𝗽𝗲𝗰 JPA is not the engine. Hibernate is the actual implementation doing the heavy lifting. --- ⚡ 𝗛𝗶𝗱𝗱𝗲𝗻 𝗠𝗮𝗴𝗶𝗰 ✔️ EntityManager checks first-level cache ✔️ Hibernate generates SQL dynamically ✔️ JDBC executes query ✔️ Connection is borrowed from pool (HikariCP) ✔️ ResultSet → Entity mapping happens via ORM --- 🧩 𝗗𝗲𝘀𝗶𝗴𝗻 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗘𝘃𝗲𝗿𝘆𝘄𝗵𝗲𝗿 Spring is not just a framework — it’s a collection of patterns: • Proxy Pattern → Repository • Factory Pattern → Bean creation • Singleton Pattern → Default scope • Template Pattern → JdbcTemplate • Strategy Pattern → Transactions • Decorator Pattern → AOP --- 🚨 𝗦𝗲𝗻𝗶𝗼𝗿 𝗟𝗲𝘀𝘀𝗼𝗻 Abstraction makes development fast… But lack of understanding makes debugging slow. That “simple” method call can: ❌ Trigger multiple DB queries (N+1 problem) ❌ Cause performance bottlenecks ❌ Hide transaction issues --- 💡 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Don’t just *use* Spring Boot. 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 𝘁𝗵𝗲 𝗽𝗶𝗽𝗲𝗹𝗶𝗻𝗲 𝗯𝗲𝗵𝗶𝗻𝗱 𝗶𝘁. Because real engineering starts when curiosity goes beyond abstraction. --- #SpringBoot #Java #Backend #SystemDesign #Hibernate #JPA #SoftwareEngineering #LearningMindset
To view or add a comment, sign in
-
🚀 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
-
Stop Googling Spring Boot annotations every 5 minutes. 🛑 When I started with Spring Boot, I spent half my day checking documentation for annotations. "Is it @Param or @PathVariable?" "Do I need @Service or @Component here?" To save myself (and you) time, I’ve compiled a 1-Page Cheat Sheet of the 25 most essential Spring Boot annotations for 2026. It covers: ✅ Web & REST Controller essentials ✅ Dependency Injection (IOC) ✅ Spring Data JPA & Transactions ✅ Modern Java 21/25 configurations Want a copy? It’s free! 🎁 All I ask in return is: 1️⃣ Like this post so others can see it. 2️⃣ Comment "JAVA" below. 3️⃣ (Optional) Connect with me if you’re a fellow developer! I’ll send the PDF link directly to your DM. Let’s build a stronger Java community together! 🚀 #Java #SpringBoot #BackendDevelopment #CodingLife #SoftwareEngineering #AhmedabadJobs #CleanCode
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
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