স্প্রিং ফ্রেমওয়ার্কে ডিপেনডেন্সি ইনজেকশন (Dependency Injection - DI) একটি গুরুত্বপূর্ণ ডিজাইন প্যাটার্ন, যা অ্যাপ্লিকেশনে বিভিন্ন ক্লাসের মধ্যে সম্পর্ক স্থাপন করে এবং তাদের একে অপরের সাথে যোগাযোগ স্থাপন করতে সাহায্য করে। স্প্রিং DI আপনাকে অবজেক্ট তৈরি এবং ডিপেনডেন্সি ম্যানেজমেন্ট এর কাজ অ্যাপ্লিকেশন কন্টেইনারের মাধ্যমে স্বয়ংক্রিয়ভাবে করতে সাহায্য করে। এতে অ্যাপ্লিকেশনের লজিককে আরও মডুলার, সহজে পরীক্ষণযোগ্য এবং রিইউজেবল করা যায়।
স্প্রিং DI দুটি প্রধান ধরনের ইনজেকশন পদ্ধতি সমর্থন করে:
- Constructor Injection
- Setter Injection
1. Constructor Injection
Constructor Injection হল একটি পদ্ধতি যেখানে ডিপেনডেন্সি একটি কনস্ট্রাক্টরের মাধ্যমে ইনজেক্ট করা হয়। এটি ইনজেকশন পদ্ধতির মধ্যে সবচেয়ে নিরাপদ এবং সুপারিশকৃত পদ্ধতি, কারণ এটি অবজেক্টের ডিপেনডেন্সিগুলো কনস্ট্রাকশন সময় নির্ধারণ করে এবং একবার অবজেক্ট তৈরি হলে ডিপেনডেন্সিগুলি পরিবর্তন করা সম্ভব নয়। এটি ফাইন গ্রানুলার কনট্রোল এবং ইমিউটেবল অবজেক্ট সৃষ্টিতে সহায়ক।
উদাহরণ:
// Service Interface
public interface GreetingService {
void greet();
}
// Service Implementation
public class GreetingServiceImpl implements GreetingService {
@Override
public void greet() {
System.out.println("Hello, Welcome to Constructor Injection!");
}
}
// Client Class
public class Client {
private GreetingService greetingService;
// Constructor Injection
public Client(GreetingService greetingService) {
this.greetingService = greetingService;
}
public void showGreeting() {
greetingService.greet();
}
}
Spring Configuration with Constructor Injection (XML):
<bean id="greetingService" class="com.example.GreetingServiceImpl"/>
<bean id="client" class="com.example.Client">
<constructor-arg ref="greetingService"/>
</bean>
Spring Configuration with Constructor Injection (Java-based):
@Configuration
@ComponentScan("com.example")
public class AppConfig {
@Bean
public Client client() {
return new Client(greetingService());
}
@Bean
public GreetingService greetingService() {
return new GreetingServiceImpl();
}
}
সুবিধা:
- Immutability: কনস্ট্রাক্টরের মাধ্যমে ইনজেকশন করা ডিপেনডেন্সিগুলি অবজেক্ট তৈরির পর পরিবর্তন করা সম্ভব নয়।
- Mandatory Dependencies: যখন নির্দিষ্ট ডিপেনডেন্সি অবশ্যই থাকতে হবে, তখন এটি কার্যকর।
- Clear Dependency Declaration: কনস্ট্রাক্টরের মাধ্যমে ডিপেনডেন্সিগুলি স্পষ্টভাবে ডিফাইন করা থাকে, যা কোডের রিডেবিলিটি বাড়ায়।
2. Setter Injection
Setter Injection হল একটি পদ্ধতি যেখানে ডিপেনডেন্সি setter methods মাধ্যমে ইনজেক্ট করা হয়। এতে কনস্ট্রাক্টর তৈরি না করেও ডিপেনডেন্সি ইনজেকশন করা সম্ভব, যা আপনাকে একাধিক ডিপেনডেন্সি ইনজেক্ট করার সুবিধা দেয়। তবে, এটি কিছু সময়ের জন্য দুর্বল হতে পারে কারণ এখানে ডিপেনডেন্সিগুলি অবজেক্ট তৈরি হওয়ার পর পরিবর্তনযোগ্য থাকে।
উদাহরণ:
// Service Interface
public interface GreetingService {
void greet();
}
// Service Implementation
public class GreetingServiceImpl implements GreetingService {
@Override
public void greet() {
System.out.println("Hello, Welcome to Setter Injection!");
}
}
// Client Class
public class Client {
private GreetingService greetingService;
// Setter Injection
public void setGreetingService(GreetingService greetingService) {
this.greetingService = greetingService;
}
public void showGreeting() {
greetingService.greet();
}
}
Spring Configuration with Setter Injection (XML):
<bean id="greetingService" class="com.example.GreetingServiceImpl"/>
<bean id="client" class="com.example.Client">
<property name="greetingService" ref="greetingService"/>
</bean>
Spring Configuration with Setter Injection (Java-based):
@Configuration
@ComponentScan("com.example")
public class AppConfig {
@Bean
public Client client() {
Client client = new Client();
client.setGreetingService(greetingService());
return client;
}
@Bean
public GreetingService greetingService() {
return new GreetingServiceImpl();
}
}
সুবিধা:
- Optional Dependencies: যে ডিপেনডেন্সিগুলি ঐচ্ছিক, সেগুলির জন্য এটি সুবিধাজনক।
- Flexibility: একবার অবজেক্ট তৈরি হলে, setter এর মাধ্যমে ডিপেনডেন্সিগুলি পরিবর্তন করা সম্ভব।
- Decoupling: কোডের মধ্যে কমপ্লেক্সিটি কমানো যায়, কারণ ডিপেনডেন্সি সরবরাহ করার জন্য সরাসরি কনস্ট্রাক্টর কল করা হয় না।
Constructor Injection vs Setter Injection
| Feature | Constructor Injection | Setter Injection |
|---|---|---|
| Dependency immutability | Yes, dependencies are immutable once set | No, dependencies can be changed after object creation |
| Mandatory Dependencies | Ensures that all required dependencies are provided | Dependencies can be optional or delayed |
| Ease of use | More verbose, especially with multiple dependencies | Simpler with multiple optional dependencies |
| Suitability | Recommended when dependencies are required at the time of object creation | Suitable when dependencies are optional or can be changed later |
সারাংশ
Constructor Injection এবং Setter Injection স্প্রিং ডিপেনডেন্সি ইনজেকশনের দুটি প্রধান পদ্ধতি। Constructor Injection একটি ক্লাসের ডিপেনডেন্সিগুলিকে অবজেক্ট তৈরি হওয়ার সময় ইনজেক্ট করে এবং এটি নিরাপদ এবং দৃঢ়ভাবে ডিপেনডেন্সিগুলির মান নির্ধারণ করে। অন্যদিকে, Setter Injection পদ্ধতিটি ইনজেকশনকে লেটার (পরে) স্থানে অনুমোদিত করে এবং এটি ঐচ্ছিক ডিপেনডেন্সির জন্য উপযুক্ত। আপনার অ্যাপ্লিকেশনের চাহিদা অনুসারে, আপনি কোন পদ্ধতি নির্বাচন করবেন তা নির্ভর করে।
Read more