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
@Bean
annotated 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
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
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
Department
beans (department1
anddepartment2
) defined in theBeanConfig
class. The@Bean
methods create these two departments with different names:department1
for "CSE" anddepartment2
for "BBA."Ambiguity in Dependency Injection: When you call the
student1
orstudent2
methods, eachStudent
needs aDepartment
object as a dependency. Since there are twoDepartment
beans available, Spring wouldn’t know automatically which one to use for eachStudent
without some guidance.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 thedepartment1
bean (the "CSE" department) intostudent1
.student2
method has@Qualifier("department2")
, which tells Spring to inject thedepartment2
bean (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