উদাহরণ সহ Constructor এবং Setter Injection

Constructor Injection এবং Setter Injection - স্প্রিং ডিপেনডেন্সি ইনজেকশন (ডিআই) (Spring Dependency Injection) - Java Technologies

355

ডিপেনডেন্সি ইনজেকশন (DI) কি?

Dependency Injection (DI) হলো একটি ডিজাইন প্যাটার্ন যা অবজেক্টের ডিপেনডেন্সি (অথবা আনুষঙ্গিক অবজেক্ট) ইনজেক্ট বা সরবরাহ করার প্রক্রিয়া। Spring Framework DI ব্যবহারের মাধ্যমে অবজেক্টের ডিপেনডেন্সি সিস্টেম বা কনটেইনার দ্বারা সরবরাহ করা হয়, যা কোডে ঢিলেঢালা সম্পর্ক তৈরি করে এবং ক্লাসগুলিকে স্বাধীনভাবে কাজ করার সুবিধা দেয়। Spring Framework দুই ধরনের DI পদ্ধতি ব্যবহার করে: Constructor Injection এবং Setter Injection


Constructor Injection

Constructor Injection একটি পদ্ধতি যেখানে ডিপেনডেন্সি সরাসরি ক্লাসের কনস্ট্রাকটরের মাধ্যমে ইনজেক্ট করা হয়। এটি সাধারণত অপরিহার্য ডিপেনডেন্সির জন্য ব্যবহৃত হয়, যেখানে ডিপেনডেন্সি অবশ্যই প্রয়োজন এবং ইনস্ট্যান্স তৈরি হওয়ার সময় তা প্রদান করা হয়।

Constructor Injection এর উদাহরণ

ধরা যাক, আমাদের কাছে Employee এবং Department নামক দুটি ক্লাস রয়েছে। Employee ক্লাসের ডিপেনডেন্সি Department ক্লাসের উপর নির্ভরশীল।

Employee.java

public class Employee {
    private Department department;

    // Constructor Injection
    public Employee(Department department) {
        this.department = department;
    }

    public void getEmployeeDetails() {
        System.out.println("Employee belongs to " + department.getDepartmentName() + " department.");
    }
}

Department.java

public class Department {
    public String getDepartmentName() {
        return "IT";
    }
}

Spring Bean Configuration (XML)

<bean id="department" class="com.example.Department"/>

<bean id="employee" class="com.example.Employee">
    <constructor-arg ref="department"/>
</bean>

এখানে, Employee ক্লাসের কনস্ট্রাকটরের মাধ্যমে Department ডিপেনডেন্সি ইনজেক্ট করা হয়েছে।

Spring Application Context

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Employee employee = (Employee) context.getBean("employee");
employee.getEmployeeDetails();

এখানে, Department Bean-টি Employee Bean-এ কনস্ট্রাকটর ইনজেকশনের মাধ্যমে ইনজেক্ট করা হয়েছে।


Setter Injection

Setter Injection হল একটি পদ্ধতি যেখানে ডিপেনডেন্সি অবজেক্টের সিম্পল সেটার মেথডের মাধ্যমে ইনজেক্ট করা হয়। এটি সাধারণত ঐচ্ছিক ডিপেনডেন্সির জন্য ব্যবহৃত হয়, যেখানে ডিপেনডেন্সি ইনজেক্ট না করলেও কাজ করবে।

Setter Injection এর উদাহরণ

আমরা আগের Employee এবং Department ক্লাসগুলোরই উদাহরণ নেব, কিন্তু এবার Setter Injection ব্যবহার করব।

Employee.java

public class Employee {
    private Department department;

    // Setter Injection
    public void setDepartment(Department department) {
        this.department = department;
    }

    public void getEmployeeDetails() {
        System.out.println("Employee belongs to " + department.getDepartmentName() + " department.");
    }
}

Department.java

public class Department {
    public String getDepartmentName() {
        return "HR";
    }
}

Spring Bean Configuration (XML)

<bean id="department" class="com.example.Department"/>

<bean id="employee" class="com.example.Employee">
    <property name="department" ref="department"/>
</bean>

এখানে, Employee Bean-এ Department ডিপেনডেন্সি setDepartment() মেথডের মাধ্যমে ইনজেক্ট করা হয়েছে।

Spring Application Context

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Employee employee = (Employee) context.getBean("employee");
employee.getEmployeeDetails();

এখানে, Department Bean-টি Employee Bean-এ setter ইনজেকশনের মাধ্যমে ইনজেক্ট করা হয়েছে।


Constructor Injection এবং Setter Injection এর মধ্যে পার্থক্য

বিষয়Constructor InjectionSetter Injection
ডিপেনডেন্সি প্রয়োজনীয়তাঅপরিহার্য ডিপেনডেন্সির জন্য ব্যবহৃত।ঐচ্ছিক ডিপেনডেন্সির জন্য ব্যবহৃত।
নাল ভ্যালুএটি নাল ভ্যালু গ্রহণ করতে পারে না।এটি নাল ভ্যালু গ্রহণ করতে পারে।
লিভা/মেনুইনস্ট্যান্স তৈরি হওয়ার সময় ডিপেনডেন্সি ইনজেক্ট করা হয়।মেথড কল করার মাধ্যমে ডিপেনডেন্সি ইনজেক্ট করা হয়।
কনফিগারেশনকনস্ট্রাকটরের মধ্যে ডিপেনডেন্সি ইনজেক্ট করা হয়।setter মেথডের মাধ্যমে ডিপেনডেন্সি ইনজেক্ট করা হয়।
পুনঃব্যবহারযোগ্যতাসাধারণত কম্প্যাক্ট এবং ডিপেনডেন্সির জন্য শক্তিশালী।নমনীয়, তবে নাল ভ্যালু গ্রহণ করতে পারে, যা কিছু ক্ষেত্রে সমস্যার সৃষ্টি করতে পারে।

সারাংশ

Constructor Injection এবং Setter Injection হল Spring Framework-এ DI (Dependency Injection) বাস্তবায়নের দুটি পদ্ধতি।

  • Constructor Injection সাধারণত অপরিহার্য ডিপেনডেন্সি ইনজেকশন করার জন্য ব্যবহৃত হয়, যেখানে ডিপেনডেন্সি ইনজেক্ট করা না হলে অবজেক্টটি তৈরি করা সম্ভব হয় না। এটি শক্তিশালী এবং নির্ভরযোগ্য।
  • Setter Injection ঐচ্ছিক ডিপেনডেন্সির জন্য ব্যবহৃত হয় এবং ডিপেনডেন্সি ইনজেকশন প্রক্রিয়া বেশি নমনীয় করে, তবে এটি ডিপেনডেন্সির সাথে সম্পর্কিত কিছু সমস্যার সৃষ্টি করতে পারে, যেমন নাল ভ্যালু।

Spring Framework-এ DI ব্যবহারের মাধ্যমে আপনি সহজেই অবজেক্টগুলির ডিপেনডেন্সি ইনজেক্ট করে কোডের রক্ষণাবেক্ষণ এবং পুনঃব্যবহারযোগ্যতা উন্নত করতে পারেন।

Content added By
Promotion

Are you sure to start over?

Loading...