Posts

✅ Spring Boot Annotations

🏁 1. Main Application Setup πŸ”° @SpringBootApplication – This is a convenience annotation that combines: @Configuration , @EnableAutoConfiguration , and @ComponentScan . It is used on the main class to bootstrap and launch a Spring Boot application. @Configuration – Indicates that the class contains Spring bean definitions. Beans defined here will be registered in the application context. @EnableAutoConfiguration – Tells Spring Boot to automatically configure beans based on the classpath, properties, and existing beans. Helps reduce manual configuration. @ComponentScan – Enables automatic scanning of packages for components like @Component , @Service , @Repository , and @Controller . 🧩 2. Component & Bean Annotations 🧩 @Component - Spring-managed component (auto-detected). πŸ”§ @Service - For business logic layer. πŸ—ƒ️ @Repository - For data access layer with exception translation. ...

⚙️ApplicationContext in Spring Boot – The Core of IoC

Spring ApplicationContext Explained 🧠 What is ApplicationContext? ApplicationContext is Spring's brain. It is the container that: ✅ Creates all your beans (objects) ✅ Wires them together (dependency injection) ✅ Manages their life cycle ✅ Provides access to them anywhere in the app 🎯 One-Line Definition: ApplicationContext is the place where Spring keeps and manages all your @Component , @Service , and @Controller beans. πŸ“† Example: How It Works 1. Bean class: @Component public class MyBean { public void sayHi() { System.out.println("Hi from MyBean!"); } } 2. Spring Boot App: @SpringBootApplication public class MyApp implements CommandLineRunner { @Autowired MyBean myBean; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); // Creates ApplicationContext } @Override publi...

πŸ“¦ Spring Boot IoC Container

Spring IoC Container Explained 🧠 What is IoC Container? The IoC (Inversion of Control) Container is the core part of Spring that: ✅ Creates your objects (beans) ✅ Manages them ✅ Injects their dependencies ✅ Controls their lifecycle You don’t manually create or connect objects — Spring does it for you. πŸ” Inversion of Control? Normal (manual) object creation: MyService service = new MyService(); Spring Boot style (IoC-managed): @Component public class MyService { } @Autowired MyService service; // Spring injects this This is Inversion of Control — you give up control to the container. πŸ“¦ In Spring Boot Spring Boot automatically starts the IoC container when your application runs: @SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); // starts IoC container } } Under the hood, this creates an ApplicationCont...

🌱 Spring Bean

Spring Boot Bean Lifecycle 🧠 What is a Bean in Spring Boot? A bean is a Java object managed by the Spring framework. In Spring Boot, you often create beans using: @Component @Service @Repository @Configuration @Bean Spring Boot automatically detects and manages these beans through component scanning . πŸ”„ How Does the Bean Lifecycle Work in Spring Boot? Spring Boot uses the standard Spring bean lifecycle, but simplifies it with annotations and auto-configuration. ✅ Common Lifecycle Hooks You Might Use: @PostConstruct - Run code after dependencies are injected: @Component public class MyService { @PostConstruct public void init() { System.out.println("Bean is initialized"); } } @PreDestroy - Run code before bean is destroyed (e.g., app shutdown): @PreDestroy public void cleanup() { System.out.println("Bean is being destroyed"); } InitializingBean and DisposableBean interfaces: ...

🌿⚙️ Spring Boot Simplified

🌱 Spring Boot Basics Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. In this post, you'll learn its core concepts, including auto-configuration, starter dependencies, and embedded servers. πŸ“˜ 1. What is Spring Boot? Spring Boot is a framework built on top of the Spring Framework. Its goal is to simplify the development of production-ready Spring applications by reducing the amount of boilerplate code and configuration. ✅ Key Benefits: Auto-configuration: Smart defaults based on your dependencies Embedded servers: Like Tomcat or Jetty — no need to deploy WARs Opinionated setup: Makes decisions for you, but still customizable Single entry point: Uses @SpringBootApplication to bootstrap the app πŸ†š Spring Boot vs Spring Framework Feature Spring Framework Spring Boot Configuration Manual (XML or Java) Auto-confi...

Internal Working of Hashmap

Internal Working of HashMap in Java πŸ” Internal Working of HashMap in Java ✅ Step-by-Step Explanation 1. Adding a Key-Value Pair map.put("name", "John"); We start by adding a key-value pair to the HashMap. 2. Hash Code Generation Java computes a hash code for the key using: int hash = key.hashCode(); // "name".hashCode() 3. Converting Hash to Array Index To reduce collision risk, it computes the index in the underlying array: index = (hash & (array.length - 1)); 4. Bucket Check If the bucket at the index is empty, the entry is stored directly. If not, a collision has occurred. 5. Collision Handling On collision, Java uses: LinkedList chaining for simple conflicts Red-Black Tree (since Java 8) if bucket size > 8 and capacity > 64 Bucket[5] → ("id", 101) → ("name", "John") 6. Retrieving a...

πŸ“š Java Collections

Java Collections – Explained ✅ What is a Collection in Java? A Collection is a framework in Java that provides architecture to store and manipulate groups of objects. It contains interfaces, classes, and methods to perform various data operations efficiently. 🎯 Why Use Collections? Store dynamic groups of objects Support searching, sorting, insertion, and deletion Reduce boilerplate code using ready-to-use data structures ✅ Core Interfaces in Java Collection Framework Interface Description Common Implementations List Ordered collection with index access ArrayList, LinkedList, Vector Set No duplicates, unordered HashSet, LinkedHashSet, TreeSet Queue FIFO structure Link...

πŸ”₯ Java Exception and Error Handling

Java Exceptions and Errors Java Exceptions and Errors ⚠️ What is an Exception? An exception is an event that disrupts the normal flow of a program. It occurs during runtime and is recoverable . Example: Dividing by zero, accessing an invalid array index. ❌ What is an Error? An error is a serious issue beyond the control of the application. Typically unrecoverable and leads to program termination. Example: Hardware failure, memory issues, JVM crash. 🌳 Java Exception Hierarchy All exceptions and errors inherit from the Throwable class: Throwable ├── Exception → Can be handled by code │ ├── Checked → Must be handled │ └── Unchecked → Optional to handle └── Error → Cannot/should not be handled 🧱 Types of Exceptions ✅ Built-in Exceptions Predefined by Java. Handle common runtime issues. Examples: NullPointerException , IOException , ArrayIndex...

Understanding How OOP Concepts Work with Real-Life Examples

Understanding OOP Principles with a Banking System Example πŸ’Ό Project: Banking System (as an Example) Let’s explore how Object-Oriented Programming (OOP) principles can be applied in a real-world project like a banking system. πŸ” 1. Encapsulation – Hiding Internal Data Consider a class like Account . It has private variables like balance . You can only change the balance using methods like deposit() or withdraw() . ✅ Why? It protects data and ensures only valid changes happen. πŸ‘ͺ 2. Inheritance – Reusing Code We create a base class called Account , and then make child classes: SavingsAccount CurrentAccount ✅ Why? Avoids repeating common code in multiple places. 🎭 3. Polymorphism – Same Method, Different Behavior We define an interface called Notification with a method send() . Implementations: EmailNotification , SMSNotification , etc. ✅ Why? We can use send() without worrying about how ...

Rest Template in Java

Image
  RestTemplate : 1) Why do we need RestTemplate?  ==> To communicate with different services in single application. Create Resttemplate object: // create an instance of RestTemplate RestTemplate restTemplate = new RestTemplate ( ) ; 2) Different methods of RestTemplate. exchange() <http_method>ForEntity      ex    getForEntity(),postForEntity() <http_method>ForObject         ex     getForObject(),postForObject() HttpPost Api with parameters:                                          Returns PostForObject(url,request,classType);                         ResponseBody PostForEntity(url,request,responseType);                    Response status,header,body PostForLocation(url,r...