<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: manju george</title>
    <description>The latest articles on DEV Community by manju george (@manju_george_bca224c066e1).</description>
    <link>https://dev.to/manju_george_bca224c066e1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3825903%2F3c29e7d6-00e3-4777-9772-2b9fa31a529f.png</url>
      <title>DEV Community: manju george</title>
      <link>https://dev.to/manju_george_bca224c066e1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manju_george_bca224c066e1"/>
    <language>en</language>
    <item>
      <title>What Could Go Wrong? A Developer’s Guide to Better Testing</title>
      <dc:creator>manju george</dc:creator>
      <pubDate>Fri, 06 Nov 2026 03:56:21 +0000</pubDate>
      <link>https://dev.to/manju_george_bca224c066e1/what-could-go-wrong-a-developers-guide-to-better-testing-118n</link>
      <guid>https://dev.to/manju_george_bca224c066e1/what-could-go-wrong-a-developers-guide-to-better-testing-118n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Testing Software from Methods to Complete Systems&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A method returns the correct value. &lt;br&gt;
An API responds with 200 OK. &lt;br&gt;
A page renders successfully.&lt;/p&gt;

&lt;p&gt;These are useful checks. But what happens when the backend is unavailable, a user closes the application during an operation, or a hardware command succeeds too late?&lt;/p&gt;

&lt;p&gt;When I think about software testing, I think about several layers of confidence: individual logic, components, interactions, and the complete system. At every layer, I ask two questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does it do what the requirements say? &lt;/li&gt;
&lt;li&gt;What happens when something goes wrong?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Start with unit testing while writing code
&lt;/h2&gt;

&lt;p&gt;Unit tests check a small piece of behavior in isolation, such as a function, method, or class. This is done to verify the behavior itself and not make an exhaustive test for each method.&lt;/p&gt;

&lt;p&gt;In case of backend business logic, we can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Valid inputs and calculations based on them.&lt;/li&gt;
&lt;li&gt;Missing inputs or null/invalid inputs.&lt;/li&gt;
&lt;li&gt;Boundary value, i.e., values like zero, maximum, and values close to them.&lt;/li&gt;
&lt;li&gt;Exceptions and behavior in case of dependencies failure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The validation method should support values up to the permissible limit and fail the values slightly exceeding the limit. Testing only a typical value leaves the boundary unverified.&lt;/p&gt;

&lt;p&gt;Mocking/stubbing helps in isolating dependencies, where the test for service doesn't have to establish a connection with actual database and can mimic the return value or exceptions thrown by a repository.&lt;/p&gt;

&lt;p&gt;Similarly, UI logic can be tested in isolation. Based on the data passed to the UI, Does the UI show the correct label or disable the button? Include the testing of empty/loading data screen too.&lt;/p&gt;

&lt;p&gt;However, one thing must be clear – even when using mocks, you do not have to call a test a unit test. An API test that involves loading routing, serialization and framework behavior might actually be a component test or an integration test, even when downstream services are being mocked.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Test each component or subsystem
&lt;/h2&gt;

&lt;p&gt;Component testing verifies a significant portion of the application as a whole. This can be a user interface component, an API service, or an alarm management system, with external dependencies managed where necessary.&lt;/p&gt;

&lt;p&gt;Different terminology is used by different teams; some use “component” and “unit” interchangeably. In this context, component testing is used for the boundary larger than a single function or class.&lt;/p&gt;

&lt;p&gt;Take the screen that initiates the operation and presents the status of it. The component tests can check that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The starting state is correct.&lt;/li&gt;
&lt;li&gt;Invalid input stops submission. &lt;/li&gt;
&lt;li&gt;The loading state is shown while processing is pending.&lt;/li&gt;
&lt;li&gt;There is an informative message in case of an error. &lt;/li&gt;
&lt;li&gt;Controls return to the appropriate state after an error. &lt;/li&gt;
&lt;li&gt;A stale response must not overwrite the state of a newer request when the latest request should take precedence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the backend component, validate test scenarios, business logic, error handling, and cleanup in case of exceptions.&lt;br&gt;
This level provides answers to the following targeted question: Does this subsystem work properly within its own boundaries?&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Test the interactions through integration testing
&lt;/h2&gt;

&lt;p&gt;Individually correct components can still disagree about request formats, response fields, authentication, or timing.&lt;/p&gt;

&lt;p&gt;Integration tests verify that connected components work together as expected. This could be a service talking to a database, a UI interacting with a backend, or services calling other services.&lt;/p&gt;

&lt;p&gt;Selenium can test browser workflows. Postman and curl can test the API calls. Manual tests could help to investigate connections that require human judgement. The scope of the test determines its level, regardless of the tool used.&lt;/p&gt;

&lt;p&gt;Questions to ask at each boundary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the receiver parse the request correctly? &lt;/li&gt;
&lt;li&gt;Do the response fields and error formats fit together? &lt;/li&gt;
&lt;li&gt;Does the authentication/authorization work? &lt;/li&gt;
&lt;li&gt;What happens if the response is slow or does not come back? &lt;/li&gt;
&lt;li&gt;Could the duplicate requests lead to the duplicate actions?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mock which always gives the expected response will not help you to prove that the real dependency really does so.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Test the complete system in its operating context
&lt;/h2&gt;

&lt;p&gt;System testing validates the behavior of the built system against its requirements. In case of software that works with hardware, this involves physical behavior and timing.&lt;/p&gt;

&lt;p&gt;Consider a scenario when the alarm triggers a light to come on. The backend unit test could confirm that the application calls the function which would make the light go on. This does not mean that it actually does happen in the required timeframe.&lt;br&gt;
The system-level questions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the necessary light activated upon alarm? &lt;/li&gt;
&lt;li&gt;Is the timing within the required bounds? &lt;/li&gt;
&lt;li&gt;Is the UI reflecting the right system state? &lt;/li&gt;
&lt;li&gt;Is the user able to cancel the process via appropriate command? &lt;/li&gt;
&lt;li&gt;How does the system behave in case the device gets disconnected mid-way? &lt;/li&gt;
&lt;li&gt;Is the right safe state achieved after failure?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simulate those scenarios which you can do accurately enough using simulation. Test any other relevant behavior via hardware where necessary. Successful command from simulation does not imply physical action.&lt;/p&gt;

&lt;p&gt;The above approach should be used even in case of pure software systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Treat failure behavior as part of the feature
&lt;/h2&gt;

&lt;p&gt;Testing should cover the API's documented response contract, rather than stopping at a successful response.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;What to verify&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;400 Bad Request&lt;/td&gt;
&lt;td&gt;Invalid input receives an actionable explanation.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;401 Unauthorized&lt;/td&gt;
&lt;td&gt;Missing or invalid authentication follows the defined sign-in or session-recovery flow.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;403 Forbidden&lt;/td&gt;
&lt;td&gt;Access is refused, and restricted operations remain protected.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;404 Not Found&lt;/td&gt;
&lt;td&gt;An unavailable resource is handled without leaving the UI stuck.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;409 Conflict&lt;/td&gt;
&lt;td&gt;Conflicting state is explained, with an appropriate recovery path.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;500 Internal Server Error&lt;/td&gt;
&lt;td&gt;The failure is handled without exposing internal details.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;503 Service Unavailable&lt;/td&gt;
&lt;td&gt;Temporary unavailability produces the defined fallback or recovery behavior.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timeout or connection failure&lt;/td&gt;
&lt;td&gt;The application exits the waiting state and explains what the user can do next.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The status codes describe HTTP outcomes; the application-specific responses in this table are behaviors to define and test. A connection failure may produce no HTTP response at all.&lt;/p&gt;

&lt;p&gt;Do not retry every failed request blindly. If an operation might already have completed, retrying can create duplicate work. Test the intended retry policy, including limits and protection against duplicate effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Check lifecycle, browser, and screen behavior
&lt;/h2&gt;

&lt;p&gt;Unexpected application closure deserves deliberate testing.&lt;br&gt;
If a user closes the UI during an operation, should the backend continue, cancel, or complete a controlled shutdown? There is no universal answer. The requirements and architecture must define it.&lt;/p&gt;

&lt;p&gt;A browser tab closing does not automatically stop a backend process. Browser close warnings also have limitations, so important cleanup should not depend entirely on a warning appearing.&lt;/p&gt;

&lt;p&gt;Test refreshes, reconnects, session expiration, and reopening the application. Can the user recover the actual operation state?&lt;br&gt;
For supported browsers and devices, check more than appearance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are important controls visible at smaller widths? &lt;/li&gt;
&lt;li&gt;Do dialogs remain usable on laptops, tablets, and phones? &lt;/li&gt;
&lt;li&gt;Can users navigate with a keyboard and identify focus? &lt;/li&gt;
&lt;li&gt;Are errors understandable without relying only on color? &lt;/li&gt;
&lt;li&gt;Does resizing preserve data and application state?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compatibility and accessibility are concerns across testing levels. A component test can check a dialog, while a system test checks whether the complete workflow remains usable.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Verify requirements, not just existing behavior
&lt;/h2&gt;

&lt;p&gt;A test can accurately describe the current implementation and still approve the wrong behavior.&lt;/p&gt;

&lt;p&gt;Write formal verification cases from approved requirements and acceptance criteria. Use observed behavior to investigate the implementation, not as the sole authority for the expected result.&lt;/p&gt;

&lt;p&gt;For an illustrative alarm requirement, a verification case might look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Requirement&lt;/td&gt;
&lt;td&gt;The indicator activates within the specified interval after the alarm event.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Preconditions&lt;/td&gt;
&lt;td&gt;System ready; indicator connected; timing measurement available.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Action&lt;/td&gt;
&lt;td&gt;Trigger the defined alarm condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Expected result&lt;/td&gt;
&lt;td&gt;The correct indicator activates within the requirement's timing limit.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Evidence&lt;/td&gt;
&lt;td&gt;Recorded event and activation times, plus relevant logs.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Verdict&lt;/td&gt;
&lt;td&gt;Pass or fail based on the specified limit.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Do not invent a timing limit because it seems reasonable. If a requirement says “immediately” without a measurable definition, clarify it before using it as a pass criterion.&lt;/p&gt;

&lt;p&gt;During execution, record the actual result for each verification step. An unmet expected result is a failure. If the test cannot proceed, record it as blocked or not executed according to the team's process; do not mark it passed.&lt;/p&gt;

&lt;p&gt;When a defect is found:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a defect report with reproduction steps, expected and actual results, environment details, and evidence.&lt;/li&gt;
&lt;li&gt;Link it to the affected requirement and test case.&lt;/li&gt;
&lt;li&gt;Fix the cause and add an appropriate automated regression test where practical.&lt;/li&gt;
&lt;li&gt;Rerun the failed case on the corrected build.&lt;/li&gt;
&lt;li&gt;Run relevant regression tests to check for effects elsewhere.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Passing a rerun is evidence about the corrected build. Keep the original failure and its investigation traceable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make testing a way of thinking
&lt;/h2&gt;

&lt;p&gt;These levels are complementary, and their execution need not follow a rigid sequence. Fast isolated tests support development; broader tests provide evidence about interactions and complete behavior.&lt;/p&gt;

&lt;p&gt;Choose the smallest test that can reveal a particular defect, then add broader coverage where boundaries, operating conditions, or risk require it.&lt;/p&gt;

&lt;p&gt;Before calling a feature complete, ask:&lt;br&gt;
What could fail, what would the user experience, and what evidence proves the system responds as required?&lt;br&gt;
That question connects method-level correctness to the reliability of the complete system.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Selenium: &lt;a href="https://www.selenium.dev/documentation/test_practices/testing_types/" rel="noopener noreferrer"&gt;Types of Testing&lt;/a&gt;&lt;br&gt;
Selenium: &lt;a href="https://www.selenium.dev/documentation/test_practices/overview/" rel="noopener noreferrer"&gt;Overview of Test Automation&lt;br&gt;
&lt;/a&gt;Google web.dev: &lt;a href="https://web.dev/learn/testing/get-started/test-types" rel="noopener noreferrer"&gt;Types of Automated Testing&lt;/a&gt;&lt;br&gt;
RFC 9110: &lt;a href="https://www.rfc-editor.org/rfc/rfc9110.html" rel="noopener noreferrer"&gt;HTTP Semantics&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>softwareengineering</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Designing Better Spring Boot Applications with Smart Dependency Injection</title>
      <dc:creator>manju george</dc:creator>
      <pubDate>Mon, 16 Mar 2026 02:17:44 +0000</pubDate>
      <link>https://dev.to/manju_george_bca224c066e1/designing-better-spring-boot-applications-with-smart-dependency-injection-4o7m</link>
      <guid>https://dev.to/manju_george_bca224c066e1/designing-better-spring-boot-applications-with-smart-dependency-injection-4o7m</guid>
      <description>&lt;p&gt;Most Spring Boot developers rely heavily on @Autowired—and it works—until it doesn’t.&lt;/p&gt;

&lt;p&gt;As applications grow, this convenience can quietly introduce hidden dependencies, tight coupling, and code that becomes difficult to test and maintain.&lt;/p&gt;

&lt;p&gt;This article explores smarter approaches to building scalable, maintainable Spring Boot applications by going beyond basic dependency injection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;One of the biggest strengths of Spring Boot is how easy dependency injection feels. Add @Autowired, and everything just works.&lt;/p&gt;

&lt;p&gt;However, as your application evolves, this simplicity can become a liability.&lt;/p&gt;

&lt;p&gt;You may start encountering issues such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Multiple implementations and conflicts between beans&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hidden dependencies that are difficult to test&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Coupling due to improper dependency injection&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The need to override or customize parts of existing functionality&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this stage, it’s no longer enough to know how to use @Autowired. You need to understand when to use it—and when to avoid it.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore practical, real-world patterns to design cleaner and more maintainable Spring Boot applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Dependency Injection Matters
&lt;/h2&gt;

&lt;p&gt;Dependency Injection is more than just wiring objects—it defines how responsibilities flow across your application.&lt;br&gt;
Key benefits include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loose coupling&lt;/li&gt;
&lt;li&gt;Testability&lt;/li&gt;
&lt;li&gt;Maintainability&lt;/li&gt;
&lt;li&gt;Separation of concerns&lt;/li&gt;
&lt;li&gt;Flexibility in replacing implementations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring eliminates the need to manually create objects using new, and instead manages object creation and wiring for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  Typical Spring Boot Architecture
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Controller → Service → Repository → Database
                ↓
          Helper / Validator

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Dependencies across these layers are seamlessly managed by Spring.&lt;/p&gt;
&lt;h2&gt;
  
  
  When to Use @Autowired
&lt;/h2&gt;

&lt;p&gt;Use Spring injection when working with Spring-managed beans.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Service Injection&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReportService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;DataExportService&lt;/span&gt; &lt;span class="n"&gt;dataExportService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ReportService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DataExportService&lt;/span&gt; &lt;span class="n"&gt;dataExportService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dataExportService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dataExportService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generateReport&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;dataExportService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;export&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Common Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Service → Service communication&lt;/li&gt;
&lt;li&gt;Repository injection&lt;/li&gt;
&lt;li&gt;Helper/utility components&lt;/li&gt;
&lt;li&gt;Strategy pattern implementations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When You DON’T Need @Autowired
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Single Constructor Injection&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReportService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;DataProcessor&lt;/span&gt; &lt;span class="n"&gt;dataProcessor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ReportService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DataProcessor&lt;/span&gt; &lt;span class="n"&gt;dataProcessor&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dataProcessor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dataProcessor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a class has only one constructor, Spring automatically injects dependencies—no @Autowired needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Utility Classes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DateUtil&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LocalDate&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this approach for stateless helpers (e.g., formatters, mappers) where dependency injection is unnecessary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Runtime Objects&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReportCriteria&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;reportTypeKey&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ReportCriteria&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;reportTypeKey&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reportTypeKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reportTypeKey&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when objects are created dynamically at runtime (e.g., request-based data, DTOs, user inputs) instead of being managed by the Spring container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid Field Injection
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Not Recommended:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Autowired&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;AddressValidator&lt;/span&gt; &lt;span class="n"&gt;validator&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Recommended:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;InputValidator&lt;/span&gt; &lt;span class="n"&gt;inputValidator&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProcessingService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InputValidator&lt;/span&gt; &lt;span class="n"&gt;inputValidator&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;inputValidator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputValidator&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is recommended:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explicit dependencies&lt;/li&gt;
&lt;li&gt;Immutable fields&lt;/li&gt;
&lt;li&gt;Easier unit testing&lt;/li&gt;
&lt;li&gt;Better design&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Multiple Beans Causing Confusion? Use @Qualifier
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProcessingService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Handler&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProcessingService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"primaryHandler"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Handler&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when more than one implementation is defined for the same dependency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implicit Injection (The Clean Way)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProfileService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ProfileRepository&lt;/span&gt; &lt;span class="n"&gt;profileRepository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProfileService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ProfileRepository&lt;/span&gt; &lt;span class="n"&gt;profileRepository&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;profileRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;profileRepository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring automatically injects dependencies, even without using @Autowired.&lt;/p&gt;

&lt;h2&gt;
  
  
  Excluding Beans from Scanning
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ComponentScan&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;excludeFilters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@ComponentScan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FilterType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ASSIGNABLE_TYPE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;classes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LegacyAuthHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this to prevent unwanted beans from being loaded into the application context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Manual Bean Registration
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SystemConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ObsoleteProcessor&lt;/span&gt; &lt;span class="nf"&gt;processor&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ObsoleteProcessor&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when Spring cannot auto-detect a class and you need manual control.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple Beans? Let &lt;code&gt;@Primary&lt;/code&gt; Pick the Default
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Primary&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PrimaryHandler&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Handler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when you want Spring to automatically select a default implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Partial Override (Inheritance)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LogFormatter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;StandardFormatter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getHeader&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Log Header"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when you need to modify a small part of an existing class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better Approach: Composition
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomBillingCalculator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;BaseBillingCalculator&lt;/span&gt; &lt;span class="n"&gt;baseBillingCalculator&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;CustomBillingCalculator&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseBillingCalculator&lt;/span&gt; &lt;span class="n"&gt;baseBillingCalculator&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;baseBillingCalculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;baseBillingCalculator&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.08&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;baseBillingCalculator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;calculate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prefer composition over inheritance for flexibility.&lt;/p&gt;

&lt;p&gt;Composition allows you to build behavior by combining smaller components, while inheritance creates rigid structures that are harder to change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Injecting Multiple Implementations
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;PromotionEngine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PromotionStrategy&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;promotionStrategies&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;promotionStrategies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;promotionStrategies&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this pattern for dynamic, extensible systems (e.g., plugin architectures).&lt;/p&gt;

&lt;h2&gt;
  
  
  Optional Dependencies
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MessageHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NotificationSender&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;notificationSender&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notificationSender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notificationSender&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when certain features are optional and should not break the application if absent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spring DI: What to Use and When
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Recommended Approach&lt;/th&gt;
&lt;th&gt;Avoid&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Shared dependency across layers&lt;/td&gt;
&lt;td&gt;Use Spring Injection&lt;/td&gt;
&lt;td&gt;Creating manually with &lt;code&gt;new&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple implementations exist&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;@Qualifier&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Relying on ambiguous injection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One default implementation needed&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;@Primary&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Adding &lt;code&gt;@Qualifier&lt;/code&gt; everywhere&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Need fine-grained control&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;@Bean&lt;/code&gt; + exclusion&lt;/td&gt;
&lt;td&gt;Blind component scanning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Customize behavior partially&lt;/td&gt;
&lt;td&gt;Use composition / override&lt;/td&gt;
&lt;td&gt;Deep inheritance chains&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Optional feature (SMS, Email, etc.)&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;Optional&lt;/code&gt; injection&lt;/td&gt;
&lt;td&gt;Forcing bean presence&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Spring DI: When NOT to Use Spring Injection
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;What to Do Instead&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Runtime-created objects (DTOs, request data)&lt;/td&gt;
&lt;td&gt;Create using &lt;code&gt;new&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Utility/helper classes&lt;/td&gt;
&lt;td&gt;Use static methods or simple classes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No lifecycle management needed&lt;/td&gt;
&lt;td&gt;Keep it outside Spring&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Real World pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use @Qualifier when multiple beans exist&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;@Primary&lt;/code&gt; to define a default implementation&lt;/li&gt;
&lt;li&gt;Use component exclusion + custom &lt;code&gt;@Bean&lt;/code&gt; for fine control&lt;/li&gt;
&lt;li&gt;Use composition or partial overrides for flexible customization
This combination enables a clean, scalable, and enterprise-ready architecture.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion: Smarter Spring DI
&lt;/h2&gt;

&lt;p&gt;@Autowired is powerful—but blindly using it everywhere can lead to poor design.&lt;/p&gt;

&lt;p&gt;The real strength lies in choosing the right tool for the right situation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefer constructor injection for clarity and immutability&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;@Qualifier&lt;/code&gt; when multiple implementations exist&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;@Primary&lt;/code&gt; for sensible defaults&lt;/li&gt;
&lt;li&gt;Use custom &lt;code&gt;@Bean&lt;/code&gt; for full control&lt;/li&gt;
&lt;li&gt;Prefer composition over inheritance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Golden Rule:&lt;/strong&gt;&lt;br&gt;
Let Spring manage shared dependencies—but take control when customization and flexibility matter.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>codequality</category>
      <category>java</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
