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:
Create the Classes (Beans): These are the regular Java classes that will act as the dependencies. You’ll inject these dependencies into other classes.
Create a Configuration Class:
- Mark the configuration class with
@Configuration. - Define
@Beanannotated methods that return instances of the classes (beans) you want Spring to manage.
- Mark the configuration class with
Inject Dependencies:
- You can inject dependencies through Constructor Injection or Setter Injection by defining appropriate methods in the configuration class.
Create Main Class: The main class is where you'll use
AnnotationConfigApplicationContextto 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
Department. We inject the Department into the Student via the constructor.- Create the Configuration Class
@Bean tells Spring to manage Student and Department as beans.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:
Two Different Department Beans: You have two
Departmentbeans (department1anddepartment2) defined in theBeanConfigclass. The@Beanmethods create these two departments with different names:department1for "CSE" anddepartment2for "BBA."Ambiguity in Dependency Injection: When you call the
student1orstudent2methods, eachStudentneeds aDepartmentobject as a dependency. Since there are twoDepartmentbeans available, Spring wouldn’t know automatically which one to use for eachStudentwithout some guidance.How
@QualifierSolves This:@Qualifierhelps resolve this ambiguity by specifying the exact bean to use for each injection. For example:student1method has@Qualifier("department1"), which tells Spring to inject thedepartment1bean (the "CSE" department) intostudent1.student2method has@Qualifier("department2"), which tells Spring to inject thedepartment2bean (the "BBA" department) intostudent2.
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
Post a Comment