Spring Framework-এর @Qualifier অ্যানোটেশন ব্যবহার করা হয় Dependency Injection (DI) প্রক্রিয়ায় যেখানে একাধিক Bean একই ধরনের হতে পারে এবং Spring কে বলার জন্য যে কোন Bean ইনজেক্ট করতে হবে। যখন একাধিক Bean একই ধরনের থাকে, তখন Spring ডিফল্টভাবে কোনো নির্দিষ্ট Bean নির্বাচন করতে পারে না, তখন @Qualifier ব্যবহৃত হয় নির্দিষ্ট Bean নির্বাচন করার জন্য।
@Qualifier এর উদ্দেশ্য
যখন Spring IoC Container-এ একাধিক Bean থাকে যেগুলোর টাইপ একই (উদাহরণস্বরূপ একই ইন্টারফেস বা সুপারক্লাস), তখন Spring অটোমেটিকালি সঠিক Bean ইনজেক্ট করতে পারে না। এই পরিস্থিতিতে, @Qualifier অ্যানোটেশন ব্যবহৃত হয় সঠিক Bean নির্ধারণের জন্য।
@Qualifier এর ব্যবহার
1. @Qualifier এর সাথে Bean Definition
ধরা যাক, আমাদের দুটি Engine Bean আছে—একটি ElectricEngine এবং আরেকটি DieselEngine। আমাদের একটি Car Bean রয়েছে, যেখানে আমরা সঠিক Engine Bean ইনজেক্ট করতে চাই।
Step 1: Bean Classes
Engine Interface:
public interface Engine {
void start();
}
ElectricEngine Class:
import org.springframework.stereotype.Component;
@Component("electricEngine")
public class ElectricEngine implements Engine {
@Override
public void start() {
System.out.println("Electric Engine is starting...");
}
}
DieselEngine Class:
import org.springframework.stereotype.Component;
@Component("dieselEngine")
public class DieselEngine implements Engine {
@Override
public void start() {
System.out.println("Diesel Engine is starting...");
}
}
2. @Qualifier ব্যবহার করে Dependency Injection
এখন আমাদের Car ক্লাসে Engine Bean ইনজেক্ট করতে হবে, কিন্তু যেহেতু দুটি Engine Bean আছে, তাই @Qualifier অ্যানোটেশন ব্যবহার করে আমরা নির্দিষ্ট Bean নির্বাচন করব।
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class Car {
private Engine engine;
// Constructor Injection with @Qualifier
@Autowired
public Car(@Qualifier("electricEngine") Engine engine) {
this.engine = engine;
}
public void startCar() {
engine.start(); // This will invoke ElectricEngine's start method
}
}
এখানে, @Qualifier("electricEngine") স্পষ্টভাবে ElectricEngine Bean ইনজেক্ট করার জন্য নির্দেশনা দিচ্ছে। যদি @Qualifier ব্যবহার না করা হত, Spring কোন Bean নির্বাচন করতে পারত না এবং এটি একটি NoUniqueBeanDefinitionException তৈরি করত।
3. Spring Configuration
Spring Configuration class তে আমরা Beans কনফিগার করব:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example") // Specify package for component scanning
public class AppConfig {
}
4. Main Application Class
এখন আমরা ApplicationContext ব্যবহার করে Spring Container লোড করব এবং Car Bean পাবো।
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Get the Car Bean from the context
Car car = context.getBean(Car.class);
// Start the car
car.startCar(); // Output: Electric Engine is starting...
}
}
@Qualifier ব্যবহার করার আরও একটি উদাহরণ
ধরা যাক, আমাদের PaymentService ইন্টারফেস রয়েছে এবং তার দুটি বাস্তবায়ন—CreditCardPayment এবং PayPalPayment। আমরা PaymentService Bean ইনজেক্ট করতে চাই, তবে @Qualifier ব্যবহার করে নির্দিষ্ট ধরনের PaymentService নির্বাচন করতে পারব।
PaymentService Interface:
public interface PaymentService {
void makePayment();
}
CreditCardPayment Class:
import org.springframework.stereotype.Component;
@Component("creditCardPayment")
public class CreditCardPayment implements PaymentService {
@Override
public void makePayment() {
System.out.println("Payment made through Credit Card.");
}
}
PayPalPayment Class:
import org.springframework.stereotype.Component;
@Component("payPalPayment")
public class PayPalPayment implements PaymentService {
@Override
public void makePayment() {
System.out.println("Payment made through PayPal.");
}
}
PaymentProcessor Class:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class PaymentProcessor {
private PaymentService paymentService;
// Constructor Injection with @Qualifier
@Autowired
public PaymentProcessor(@Qualifier("payPalPayment") PaymentService paymentService) {
this.paymentService = paymentService;
}
public void processPayment() {
paymentService.makePayment(); // Will invoke PayPalPayment's makePayment method
}
}
Main Application:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
PaymentProcessor processor = context.getBean(PaymentProcessor.class);
processor.processPayment(); // Output: Payment made through PayPal.
}
}
সারাংশ
@Qualifier অ্যানোটেশন ব্যবহার করে Spring Dependency Injection-এ একাধিক Bean এক ধরনের হলেও সঠিক Bean নির্বাচন করা যায়। যখন একই ধরনের একাধিক Bean থাকে, তখন @Qualifier ব্যবহার করে আপনি কোন Bean ইনজেক্ট করতে চান তা স্পষ্টভাবে নির্ধারণ করতে পারেন। এটি constructor injection, field injection এবং setter injection সকল ক্ষেত্রে কার্যকর। SpEL এবং @Qualifier একসাথে ব্যবহৃত হলে আরও শক্তিশালী এবং নমনীয় অ্যাপ্লিকেশন তৈরি করা যায়।
Read more