Guice এর Inject এবং Provides মেথডের ব্যবহার

Injector এবং Binding - গুইস (Guice) - Java Technologies

283

Guice (Dependency Injection Framework) @Inject এবং @Provides এই দুটি গুরুত্বপূর্ণ অ্যানোটেশন ব্যবহার করে নির্ভরশীলতা (dependencies) ইনজেক্ট করতে সাহায্য করে। এগুলির মাধ্যমে, Guice একটি ক্লাসের কনস্ট্রাক্টর, ফিল্ড, অথবা মেথডে ডিপেনডেন্সি ইনজেক্ট করতে পারে, যা কোডের টাইট কপ্লিং কমায় এবং আরও মডুলার, টেস্টেবল অ্যাপ্লিকেশন তৈরি করতে সহায়ক।


@Inject অ্যানোটেশন

@Inject হল Guice-এর একটি অ্যানোটেশন যা একটি ক্লাসের কনস্ট্রাক্টর, ফিল্ড, বা মেথডে ব্যবহার করা হয়, যাতে Guice স্বয়ংক্রিয়ভাবে সেই ডিপেনডেন্সিটি ইনজেক্ট করতে পারে।

কিভাবে কাজ করে:

  1. Constructor Injection: ডিপেনডেন্সি ইনজেক্ট করতে কনস্ট্রাক্টরে @Inject ব্যবহার করা হয়।
  2. Field Injection: ক্লাসের ফিল্ডে @Inject ব্যবহার করে ডিপেনডেন্সি ইনজেক্ট করা যায়।
  3. Method Injection: মেথডে @Inject ব্যবহার করা হলে, Guice সেই মেথডে ডিপেনডেন্সি ইনজেক্ট করবে।

Constructor Injection Example (@Inject)

import com.google.inject.Inject;

public class BillingService {
    private final PaymentService paymentService;

    @Inject  // Guice will inject PaymentService here
    public BillingService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void processPayment() {
        paymentService.pay();
    }
}

public interface PaymentService {
    void pay();
}

public class PaypalPaymentService implements PaymentService {
    @Override
    public void pay() {
        System.out.println("Payment made via PayPal.");
    }
}

Usage:

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new BillingModule());
        BillingService billingService = injector.getInstance(BillingService.class);
        billingService.processPayment();  // Output: Payment made via PayPal.
    }
}

Explanation:

  • Guice @Inject কনস্ট্রাক্টরে PaymentService ইনজেক্ট করবে।
  • BillingService কনস্ট্রাক্টরটি শুধুমাত্র PaymentService-এর উপর নির্ভরশীল, এবং Guice সেই নির্ভরশীলতাটি সরবরাহ করবে।

Field Injection Example (@Inject)

import com.google.inject.Inject;

public class BillingService {
    @Inject  // Guice will inject PaymentService here
    private PaymentService paymentService;

    public void processPayment() {
        paymentService.pay();
    }
}

Usage:

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new BillingModule());
        BillingService billingService = injector.getInstance(BillingService.class);
        billingService.processPayment();  // Output: Payment made via PayPal.
    }
}

Explanation:

  • @Inject ফিল্ডে ব্যবহার করা হলে, Guice সরাসরি সেই ফিল্ডে নির্ভরশীলতা ইনজেক্ট করবে।

@Provides মেথড

Guice-এর @Provides অ্যানোটেশন ব্যবহার করে, আপনি বিশেষ ধরনের নির্ভরশীলতা তৈরি করতে পারেন যা Guice-এর @Inject দ্বারা সরাসরি ইনজেক্ট করা যায় না বা যেগুলি কাস্টম কনফিগারেশন প্রয়োজন। এটি সাধারণত চালানো সময় (runtime) নির্ভরশীলতা তৈরি করতে ব্যবহৃত হয়।

@Provides ব্যবহার করা হয় যখন:

  • নির্দিষ্ট কনফিগারেশন বা প্যারামিটার প্রয়োজন।
  • @Inject দ্বারা স্বয়ংক্রিয়ভাবে ইনজেক্ট করা সম্ভব নয়।

@Provides মেথড Example

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;

public class BillingModule extends AbstractModule {
    
    @Override
    protected void configure() {
        // Other bindings can be configured here if necessary
    }

    @Provides
    @Singleton  // This makes sure only one instance of PaymentService is created
    public PaymentService providePaymentService() {
        return new PaypalPaymentService();  // Custom logic for providing the dependency
    }
}

Explanation:

  • @Provides মেথডের মাধ্যমে আপনি কাস্টম লগিক অনুসারে ডিপেনডেন্সি সরবরাহ করতে পারেন।
  • @Singleton অ্যানোটেশন ব্যবহার করলে এটি শুধুমাত্র একটি সিঙ্গল ইনস্ট্যান্স প্রদান করবে।

@Inject vs @Provides

বৈশিষ্ট্য@Inject@Provides
কোথায় ব্যবহার করা হয়কনস্ট্রাক্টর, ফিল্ড, বা মেথডেমেথডে
কি সরবরাহ করেGuice সরাসরি নির্ভরশীলতা ইনজেক্ট করেনির্দিষ্ট কাস্টম লগিকের মাধ্যমে ডিপেনডেন্সি সরবরাহ করে
কিভাবে ব্যবহৃত হয়Guice নিজেই নির্ভরশীলতা সনাক্ত করে এবং ইনজেক্ট করেআপনাকে কাস্টম @Provides মেথডে নির্ধারণ করতে হয়
কোডের পঠনযোগ্যতাসহজ এবং স্বাভাবিককিছুটা জটিল কারণ এটি নির্দিষ্ট মেথডের মাধ্যমে নির্ভরশীলতা সরবরাহ করে

@Inject এবং @Provides-এর সমন্বিত ব্যবহার

এখন আমরা @Inject এবং @Provides-এর সমন্বয়ে একটি উদাহরণ দেখি।

import com.google.inject.*;

public class BillingService {
    private final PaymentService paymentService;

    @Inject
    public BillingService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void processPayment() {
        paymentService.pay();
    }
}

public interface PaymentService {
    void pay();
}

public class PaypalPaymentService implements PaymentService {
    public void pay() {
        System.out.println("Payment made via PayPal.");
    }
}

public class BillingModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(PaymentService.class).to(PaypalPaymentService.class);
    }

    @Provides
    @Singleton
    public PaymentService providePaymentService() {
        return new PaypalPaymentService();  // You can return a custom instance or use other logic
    }
}

public class Main {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new BillingModule());
        BillingService billingService = injector.getInstance(BillingService.class);
        billingService.processPayment();  // Output: Payment made via PayPal.
    }
}

  • @Inject হল Guice-এর প্রাথমিক অ্যানোটেশন যা কনস্ট্রাক্টর, ফিল্ড, এবং মেথডে ডিপেনডেন্সি ইনজেক্ট করতে ব্যবহৃত হয়।
  • @Provides মেথড ব্যবহার করে আপনি কাস্টম ডিপেনডেন্সি সরবরাহ করতে পারেন যেখানে Guice সরাসরি ইনজেক্ট করতে সক্ষম নয়।
  • @Inject এবং @Provides একসাথে ব্যবহৃত হলে Guice-এর ডিপেনডেন্সি ইনজেকশন ক্ষমতা আরো শক্তিশালী ও নমনীয় হয়।
Content added By
Promotion

Are you sure to start over?

Loading...