Java Config based DI

 Java Configuration-based Dependency Injection

In Spring, you can configure dependencies using Java classes instead of XML. This is called Java-based configuration. To enable Java configuration, you typically create a class annotated with @Configuration, which acts like a configuration file, and methods annotated with @Bean to define the beans (objects) that Spring should manage.


Steps to Implement Java Config-based DI:

  1. Create the Classes (Beans): These are the regular Java classes that will act as the dependencies. You’ll inject these dependencies into other classes.

  2. Create a Configuration Class:

    • Mark the configuration class with @Configuration.
    • Define @Bean annotated methods that return instances of the classes (beans) you want Spring to manage.
  3. Inject Dependencies:

    • You can inject dependencies through Constructor Injection or Setter Injection by defining appropriate methods in the configuration class.
  4. Create Main Class: The main class is where you'll use AnnotationConfigApplicationContext to load the configuration and retrieve the beans.




Example 1: Constructor Injection

With Constructor Injection, the dependency is provided when you create the object by passing it through the constructor.

Step-by-Step Example

  • Create a Class with a Dependency    

                                                                               
Here,  the Student depends on the Department. We inject the Department into the Student via the constructor.

  • Create the Configuration Class                                                                                     

In BeanConfig, @Bean tells Spring to manage Student and Department as beans.
The student1() method injects Department into Student via the constructor.

Example 2: Setter Injection

In Setter Injection, the dependency is provided through a setter method after the object is created.

Step-by-Step Example

  • Create the Classes with Dependencies                                                                         
             

  •  Create the Configuration Class                                                                               

           

Example 3: Main Class 

                         


Summary 

  • Constructor Injection: Pass dependencies when creating the object, so they can be used immediately.
  • Setter Injection: Set dependencies after the object is created, using a setter method.

In both cases, the Java Configuration Class (BeanConfig) defines which objects Spring will create and how to inject dependencies. You can load these configurations in your Main class, letting Spring manage everything.


Important Message for @Qualifier annotation

In this configuration, the @Qualifier annotation is used to tell Spring exactly which Department bean to inject into each Student bean.

Here's why this is important:

  1. Two Different Department Beans:  You have two Department beans (department1 and department2) defined in the BeanConfig class. The @Bean methods create these two departments with different names: department1 for "CSE" and department2 for "BBA."

  2. Ambiguity in Dependency Injection:  When you call the student1 or student2 methods, each Student needs a Department object as a dependency. Since there are two Department beans available, Spring wouldn’t know automatically which one to use for each Student without some guidance.

  3. How @Qualifier Solves This @Qualifier helps resolve this ambiguity by specifying the exact bean to use for each injection. For example:

    • student1 method has @Qualifier("department1"), which tells Spring to inject the department1 bean (the "CSE" department) into student1.
    • student2 method has @Qualifier("department2"), which tells Spring to inject the department2 bean (the "BBA" department) into student2.

Summary:

@Qualifier is used here to clearly tell Spring which Department bean to inject, making sure student1 is assigned to the "CSE" department and student2 to the "BBA" department. Without @Qualifier, Spring would be confused about which department to use for each student, as there are multiple options available.


            

                                        

Comments