🚀 Spring Boot Auto-configuration: The Magic Behind the Curtain Ever wondered how Spring Boot "just works" the moment you add a dependency? No XML, no manual bean wiring — it simply configures itself. That's Auto-configuration in action. When your app starts, Spring Boot scans META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (Spring Boot 3) for hundreds of configuration classes. Each one is guarded by @ConditionalOn* annotations — they activate only when the right classes or properties are on the classpath. @AutoConfiguration @ConditionalOnClass(DataSource.class) @ConditionalOnMissingBean(DataSource.class) public class DataSourceAutoConfiguration { @Bean public DataSource dataSource() { return new HikariDataSource(); } } Add spring-boot-starter-data-jpa → DataSource class appears → auto-config fires → you get a connection pool for free. Define your own DataSource bean → @ConditionalOnMissingBean kicks in → auto-config backs off. You stay in control. Key conditionals to know: • @ConditionalOnClass — class must exist on classpath • @ConditionalOnMissingBean — no custom bean defined yet • @ConditionalOnProperty — specific property is set #Java #SpringBoot #BackendDevelopment #AutoConfiguration #SpringFramework
Jānis Ošs’ Post
More Relevant Posts
-
@SpringBootApplication Explained 🚀 ✅ What is @SpringBootApplication? It is the main annotation used to start a Spring Boot application. Just one annotation replaces multiple configurations. 🔍 What does it contain internally? @SpringBootApplication is a combination of 3 annotations: 1️⃣ @Configuration → Marks the class as a configuration class 2️⃣ @EnableAutoConfiguration → Spring Boot automatically configures beans based on dependencies 3️⃣ @ComponentScan → Scans and registers all components (@Component, @Service, etc.) 🧠 In Simple Words This annotation tells Spring Boot: “Configure everything automatically and start the application.” 🧩 Example @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ✔ Starts embedded server ✔ Loads Spring context ✔ Runs the application 💡 Why It’s Important Reduces boilerplate code Makes Spring Boot beginner-friendly Mandatory for every Spring Boot app Understanding how Spring Boot applications actually start 🚀 #SpringBoot #Java #SpringFramework #LearningInPublic #BackendDeveloper
To view or add a comment, sign in
-
-
Spent 25 minutes wondering why my Spring Boot scheduled task was running twice. The code looked fine: @Scheduled(fixedRate = 5000) public void processQueue() { System.out.println("Processing..."); } No errors. App started fine. But the log showed the task running twice every 5 seconds. The problem: I had @SpringBootApplication on my main class AND @EnableScheduling on a separate config class. Spring created two schedulers. The fix: Keep @EnableScheduling only in one place. @SpringBootApplication @EnableScheduling public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } One annotation in the wrong place. That was it. Spring does not warn you when scheduling is enabled multiple times. It just creates duplicate schedulers. What duplicate execution issue has caught you off guard? #Java #SpringBoot #Debugging #BackendDevelopment
To view or add a comment, sign in
-
🚀 Day 6/100: Spring Boot From Zero to Production Topic: @SpringBootApplication Annotation Your setup is done, your dependencies are added, and now you are ready to write some actual code and business logic. It all begins from your Main Class. This class usually carries the name of your application and contains a main function, the exact point where the execution begins. But there is one very important part of this main class: the @Annotation. In simple terms, an annotation is metadata written after an @ sign. it tells the compiler and the framework specific information about your code. In our case, the star of the show is @SpringBootApplication. This is a "Meta-Annotation," meaning it’s a powerful 3-in-1 combo that keeps your code clean and organized: 1. @Configuration This marks the class as a source of bean definitions. It tells Spring that this class can contain methods annotated with @Bean. In the old days of Spring, you had to manage everything in bulky XML files. With this, we use pure Java to define our infrastructure. 2. @EnableAutoConfiguration This is the "secret sauce" that makes Spring Boot feel like magic. The Role: It tells Spring Boot to start adding beans based on what it finds on your classpath. If it sees h2.jar, it sets up an in-memory database for you. No more Boilerplate Nightmares. You don't have to manually set up a Data Source unless you want to override the defaults. 3. @ComponentScan 🔍 Think of this as the "Search Party" for your project. It tells Spring to hunt for other components, configurations, and services in your packages. It specifically looks for: @Component @Service @Repository @RestController The Scope: By default, it scans the package containing your main class and all sub-packages. This is why we always keep the main class in the root package! We’ve cracked open the entry point of every Spring Boot app. 🔓 See you in the next post. #Java #SpringBoot #SoftwareDevelopment #100DaysOfCode #Backend
To view or add a comment, sign in
-
-
🌱 How Spring Boot Starts Internally? 🚀 -->SpringApplication.run(MyApplication.class, args); Let’s break it down step by step 👇 🔄 Step-by-Step Internal Flow 1️⃣ JVM Starts the Main Method The main() method is executed. 2️⃣ SpringApplication.run() is Called This is where Spring Boot startup begins. 3️⃣ Create Application Context Spring Boot creates an ApplicationContext (IOC Container is initialized) 4️⃣ Auto-Configuration Starts Based on dependencies in pom.xml, Spring Boot configures beans automatically. Example: If Spring Web dependency exists → configure DispatcherServlet If JPA exists → configure EntityManager 5️⃣ Component Scanning Spring scans: @Component @Service @Repository @Controller @RestController Beans are created and registered. 6️⃣ Embedded Server Starts Spring Boot starts embedded: Tomcat (default) Jetty Undertow 7️⃣ Application Ready 🎉 App runs on: -->http://localhost:8080 🧠 Simple Understanding Spring Boot automatically: *Creates container *Configures beans *Starts server *Runs application All with one line of code. #SpringBoot #Java #BackendDevelopment #LearningInPublic #JavaDeveloper
To view or add a comment, sign in
-
-
Spring Boot Annotations — The Backbone of Every Application When I started learning Spring Boot, annotations felt confusing… But once I understood them, everything became much simpler. In simple terms: Annotations tell Spring Boot what to do and how to manage your code. --- 🔹 Some important ones: @SpringBootApplication → Entry point of the app ✅ @RestController → Handles API requests ✅ @Service → Business logic layer ✅ @Repository → Database interaction ✅ @Autowired → Injects dependencies automatically ✅ @RequestMapping → Maps HTTP requests --- Why annotations matter: Reduce boilerplate code Enable Dependency Injection Make applications modular & scalable Help Spring manage everything behind the scenes --- Instead of writing everything manually, Spring Boot = “Just add annotations, I’ll handle the rest.” --- Currently learning and applying these concepts step by step #SpringBoot #Java #BackendDevelopment #Annotations #LearningInPublic #FullStackDeveloper
To view or add a comment, sign in
-
-
🚀 Spring Boot Series #006 Spring Bean Lifecycle — The Hidden Magic Behind Your Application Ever wondered what really happens when your Spring Boot app starts? It’s not just objects being created… it’s a well-orchestrated lifecycle 🔄 Let’s break it down 👇 🔹 1. 𝗕𝗲𝗮𝗻 𝗜𝗻𝘀𝘁𝗮𝗻𝘁𝗶𝗮𝘁𝗶𝗼𝗻 Spring creates the bean instance (using constructor or factory method). 👉 “Object is born” — but not ready yet. 🔹 2. 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 Spring injects required dependencies (@Autowired, constructor injection, etc.) 👉 Now your bean starts gaining power ⚡ 🔹 3. 𝗔𝘄𝗮𝗿𝗲 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀 (Optional but powerful) Spring injects internal context via interfaces like: BeanNameAware ApplicationContextAware 👉 Your bean becomes Spring-aware 🧠 🔹 4. 𝗕𝗲𝗮𝗻 𝗣𝗼𝘀𝘁-𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗶𝗻𝗴 (Before Init) Custom logic via BeanPostProcessor 👉 Great place for custom proxies, logging, auditing 🔹 5. 𝗜𝗻𝗶𝘁𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗣𝗵𝗮𝘀𝗲 This is where your bean becomes fully ready! Ways to initialize: @PostConstruct InitializingBean.afterPropertiesSet() Custom init-method 👉 “I’m ready to serve!” 💪 🔹 6. 𝗕𝗲𝗮𝗻 𝗣𝗼𝘀𝘁-𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗶𝗻𝗴 (After Init) Another chance to modify the bean (AOP magic happens here ✨) 🔹 7. 𝗕𝗲𝗮𝗻 𝗶𝘀 𝗥𝗲𝗮𝗱𝘆 𝘁𝗼 𝗨𝘀𝗲 Finally, your bean is in action 🎯 🔹 8. 𝗗𝗲𝘀𝘁𝗿𝘂𝗰𝘁𝗶𝗼𝗻 𝗣𝗵𝗮𝘀𝗲 When the app shuts down: @PreDestroy DisposableBean.destroy() Custom destroy-method 👉 Cleanup resources 🧹 #SpringBeansLifecycle #Java #BackendDevelopment #SpringBoot #SoftwareEngineering #SpringBootwithVC
To view or add a comment, sign in
-
-
🚀 Day 66/100 - Spring Boot - Auto Restart with DevTools One of the most useful features of Spring Boot DevTools is automatic application restart during development... Instead of manually stopping and restarting your application every time you make a change, DevTools does it for you❗ ➡️ How Auto Restart Works? DevTools continuously monitors classpath files. When a change is detected (for example, when a .class file is updated after saving code), Spring Boot automatically restarts the application context. This creates a much faster development feedback loop. ➡️ Example Workflow 1️⃣ Modify a controller method in your IDE 2️⃣ Save the file 3️⃣ DevTools detects the change 4️⃣ Spring Boot restarts automatically 5️⃣ Refresh the browser → changes appear immediately ➡️ Benefits 🔹Eliminates the need to manually stop and start the app 🔹Improves feedback loop during development ➡️ Excluding Certain Files from Being Monitored Sometimes you don't want DevTools to restart for certain file changes (like static assets)... You can configure exclusions in application.properties: spring.devtools.restart.exclude=static/**,public/** This prevents restarts when files in these directories change. Previous post - Spring Boot Developer Tools: https://lnkd.in/djgPTRBJ #100Days #SpringBoot #DevTools #DeveloperProductivity #Java #BackendDevelopment #WebDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 2/100: Spring Boot From Zero to Production Topic: Spring Framework vs Spring Boot. 🛑 Wait... Spring and Spring Boot are NOT the same? We talked about Spring Boot yesterday, but here’s the plot twist: there is an older sibling called the Spring Framework, and they aren’t exactly twins. Before you get confused, here is the spoiler: Spring Boot is an extension of the Spring Framework. It takes everything great about Spring and adds a layer of "auto-magic" features on top. You might be wondering: "Do we even need an extension? Are the extra features worth switching to a whole new way of working?" To understand better, here are the key differences so you can decide for yourself: Configuration: Where Spring Framework still expects you to write explicit configurations (and well, that hurts!), Spring Boot works with Auto-Configurations. ✅ Development Speed: It varies highly on setup, but Spring Framework requires much more setup time compared to the "plug-and-play" nature of Spring Boot. 🚀 The Server: Spring Boot just asks for a database before you can click that green button and BOOM it starts running on a Tomcat server. Spring Framework says "NO!", you better set up and manage that server yourself first. 💥 Dependency Management: This is a manual process in Spring Framework, while Spring Boot walks in with Starter POMs to handle everything for you. 📦 There could be a long discussion on their differences, and both have their best use cases depending on the environment. But for me? Spring Boot takes the crown. 👑 And that is why we are sticking with Spring Boot for the next 99 days. If you wish to learn Spring Boot made simplest, hop along the journey. See you in the next one! ✌️ #Java #SpringBoot #SoftwareDevelopment #100DaysOfCode #Backend
To view or add a comment, sign in
-
-
🚀 Spring Boot journey: ResponseEntity — Full Control Over Your HTTP Response Most Spring Boot tutorials show you @ResponseBody and move on. But if you want real control over your API responses, ResponseEntity is the tool you need. ResponseEntity<T> lets you control three things at once: the HTTP status code, the response headers, and the response body — all in a single, type-safe return value. @GetMapping("/users/{id}") public ResponseEntity<User> getUser(@PathVariable Long id) { return userService.findById(id) .map(user -> ResponseEntity.ok(user)) .orElse(ResponseEntity.notFound().build()); } Why does this matter? Because returning 200 OK when a resource isn't found — or sending 200 after a POST that created something — is simply wrong REST design. With ResponseEntity you can also attach custom headers (e.g. Location after a resource creation), return 201 Created with the new resource URI, or build error responses without throwing exceptions. This is where your API stops being "it works" and starts being "it's correct". #Java #SpringBoot #BackendDevelopment #REST #API #SoftwareEngineering
To view or add a comment, sign in
-
Spent 25 minutes wondering why my Spring Boot application was logging the same message twice. The code looked fine: @Slf4j @Service public class OrderService { public void processOrder(Order order) { log.info("Processing order: {}", order.getId()); // processing logic } } No errors. App worked fine. But every log message appeared twice in the console. The problem: I had both Logback and Log4j2 on the classpath. Spring Boot auto-configured both logging frameworks. The fix: Exclude one of them in pom.xml. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> One exclusion. That was it. Spring Boot does not warn you when multiple logging frameworks are on the classpath. It just logs everything twice. What duplicate logging issue has caught you off guard? #Java #SpringBoot #Debugging #BackendDevelopment
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