উদাহরণ সহ Primary এবং Optional Bean Injection

Spring এর মাধ্যমে Primary এবং Optional Bean Injection - স্প্রিং ডিপেনডেন্সি ইনজেকশন (ডিআই) (Spring Dependency Injection) - Java Technologies

331

Primary Bean Injection

Primary Bean Injection স্প্রিং ফ্রেমওয়ার্কে একটি বিশেষ বৈশিষ্ট্য, যা ব্যবহারকারীকে একাধিক Bean এর মধ্যে ডিফল্ট Bean নির্বাচন করতে সহায়তা করে। যখন স্প্রিং কন্টেইনারে একই টাইপের একাধিক Bean থাকে, তখন কোন Bean ইনজেক্ট হবে তা স্পষ্টভাবে নির্ধারণ করতে @Primary অ্যানোটেশন ব্যবহার করা হয়।

কেন @Primary ব্যবহার করবেন?

  • একাধিক Bean এর মধ্যে ডিফল্ট Bean নির্বাচন করার জন্য।
  • যখন আপনার অ্যাপ্লিকেশনে একাধিক Bean একটি নির্দিষ্ট টাইপের, কিন্তু আপনি একটি Bean কে ডিফল্ট হিসাবে নির্বাচন করতে চান।

@Primary এর ব্যবহার

ধরা যাক, আমরা দুটি Vehicle Bean (একটি Car এবং একটি Bike) তৈরি করেছি। এখানে, আমরা Car কে ডিফল্ট Bean হিসাবে নির্বাচন করব। এর জন্য @Primary অ্যানোটেশন ব্যবহার করা হবে।

Step 1: Vehicle Interface এবং তার Implementations তৈরি করা

public interface Vehicle {
    void drive();
}

@Component
@Primary
public class Car implements Vehicle {
    @Override
    public void drive() {
        System.out.println("Driving a car");
    }
}

@Component
public class Bike implements Vehicle {
    @Override
    public void drive() {
        System.out.println("Riding a bike");
    }
}

Step 2: Driver ক্লাস তৈরি করা

@Component
public class Driver {

    private Vehicle vehicle;

    // @Autowired injects the @Primary Vehicle (Car in this case)
    @Autowired
    public void setVehicle(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public void startJourney() {
        vehicle.drive();
    }
}

Step 3: Spring Context থেকে Bean ব্যবহার করা

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Driver driver = context.getBean(Driver.class);
        driver.startJourney(); // Output: Driving a car
    }
}

এখানে, Car Bean @Primary অ্যানোটেশন ব্যবহার করে ডিফল্ট হিসাবে নির্বাচন করা হয়েছে। স্প্রিং কন্টেইনার Vehicle টাইপের জন্য Car Bean ইনজেক্ট করবে, কারণ এটি @Primary দ্বারা চিহ্নিত।


Optional Bean Injection

Optional Bean Injection তখন ব্যবহৃত হয়, যখন আপনার একটি Bean ইনজেক্ট করার প্রয়োজন হতে পারে, কিন্তু সেটা ঐচ্ছিক। যদি Bean উপলব্ধ না থাকে, তাহলে কোন সমস্যা হবে না। এটি মূলত @Autowired অ্যানোটেশনসহ @Optional অ্যানোটেশন বা Optional<T> টাইপ ব্যবহার করে বাস্তবায়ন করা হয়।

@Optional এর ব্যবহার

ধরা যাক, আমাদের একটি Driver ক্লাস রয়েছে, যেখানে GPS ইনজেক্ট করা হবে, কিন্তু এটি ঐচ্ছিক। যদি GPS Bean না থাকে, তাহলে স্প্রিং কন্টেইনারের কোনও সমস্যা হবে না এবং ড্রাইভার যাত্রা চালিয়ে যাবে।

Step 1: GPS ক্লাস তৈরি করা

public class GPS {
    public void navigate() {
        System.out.println("Navigating using GPS");
    }
}

Step 2: Driver ক্লাস তৈরি করা

@Component
public class Driver {

    private Vehicle vehicle;
    private GPS gps;

    // Vehicle is mandatory, GPS is optional
    @Autowired
    public void setVehicle(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    @Autowired
    @Optional
    public void setGps(GPS gps) {
        this.gps = gps;
    }

    public void startJourney() {
        vehicle.drive();
        if (gps != null) {
            gps.navigate();
        } else {
            System.out.println("No GPS available");
        }
    }
}

Step 3: Spring Configuration

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}

Step 4: Spring Context থেকে Bean ব্যবহার করা

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Driver driver = context.getBean(Driver.class);
        driver.startJourney(); // Output: Driving a car, No GPS available
    }
}

এখানে, GPS Bean ইনজেক্ট করার জন্য @Optional ব্যবহার করা হয়েছে। যদি GPS Bean স্প্রিং কন্টেইনারে উপস্থিত না থাকে, তবে gps null হবে এবং একটি ডিফল্ট বার্তা "No GPS available" প্রদর্শিত হবে।


সারাংশ

  • @Primary অ্যানোটেশন ব্যবহৃত হয় যখন একাধিক Bean একই টাইপের থাকে এবং স্পষ্টভাবে ডিফল্ট Bean নির্বাচন করতে হয়।
  • @Optional অ্যানোটেশন বা Optional<T> টাইপ ব্যবহার করে একটি Bean ইনজেক্ট করার ক্ষেত্রে ঐচ্ছিক ডিপেনডেন্সি তৈরি করা যায়। যদি ঐচ্ছিক Bean উপলব্ধ না থাকে, তখন কোনো সমস্যা হবে না এবং এটি null থাকবে।
  • Primary Bean Injection ব্যবহারের মাধ্যমে স্প্রিং কন্টেইনার ডিফল্ট Bean নির্বাচন করতে পারে, যখন একাধিক Bean একই টাইপের থাকে।
  • Optional Bean Injection ব্যবহৃত হয় যেখানে Bean ঐচ্ছিক থাকে এবং এটি উপস্থিত না থাকলেও অ্যাপ্লিকেশন সঠিকভাবে কাজ করবে।

Content added By
Promotion

Are you sure to start over?

Loading...