Spring AOP (Aspect-Oriented Programming) ব্যবহার করে Logging কার্যক্রম একাধিক ক্লাসের জন্য সহজে এবং কার্যকরভাবে পরিচালনা করা যায়। AOP এর মাধ্যমে আমরা Before, After, Around Advice ব্যবহার করে মেথডের আগে, পরে বা আশেপাশে লোগিং কার্যক্রম প্রয়োগ করতে পারি। এতে কোডের পুনঃব্যবহারযোগ্যতা এবং রিডেবিলিটি বৃদ্ধি পায়, কারণ লগিং এর মতো ক্রস-কাটিং কনসার্ন এক জায়গায় সংরক্ষণ করা যায় এবং বার বার একই কোড লেখা থেকে মুক্তি পাওয়া যায়।
এই উদাহরণে আমরা Spring AOP ব্যবহার করে একটি LoggingAspect তৈরি করব, যা UserService ক্লাসের মেথডগুলোতে লোগিং কার্যক্রম চালাবে।
1. Spring AOP Logging উদাহরণ
1.1 ডিপেনডেন্সি ইনস্টল করা
Spring AOP ব্যবহার করতে হলে আপনার pom.xml ফাইলে নিম্নলিখিত ডিপেনডেন্সি যোগ করতে হবে:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
এখানে spring-boot-starter-aop ডিপেনডেন্সি Spring AOP এর জন্য প্রয়োজনীয় কার্যকারিতা প্রদান করবে।
2. Service Class (Target Class)
প্রথমে একটি UserService ক্লাস তৈরি করি, যা ইউজার তৈরি এবং মুছে ফেলার কাজ করবে।
2.1 UserService ক্লাস তৈরি করা
package com.example.demo.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void createUser(String userName) {
System.out.println("User " + userName + " created successfully.");
}
public void deleteUser(String userName) {
System.out.println("User " + userName + " deleted successfully.");
}
}
এখানে, UserService ক্লাসে দুটি মেথড রয়েছে:
createUser(String userName): ইউজার তৈরি করার কাজ করে।deleteUser(String userName): ইউজার মুছে ফেলার কাজ করে।
এখন, আমরা Spring AOP ব্যবহার করে এই মেথডগুলিতে লোগিং কার্যক্রম প্রয়োগ করব।
3. Logging Aspect তৈরি করা
এখন, একটি LoggingAspect তৈরি করব যা Spring AOP এর মাধ্যমে Before, After এবং Around Advice ব্যবহার করে লোগিং কার্যক্রম করবে। LoggingAspect ক্লাসে @Before, @After, এবং @Around অ্যানোটেশন ব্যবহার করা হবে।
3.1 LoggingAspect ক্লাস তৈরি করা
package com.example.demo.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.JoinPoint;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
// Before advice: Logs before method execution
@Before("execution(* com.example.demo.service.UserService.*(..))")
public void beforeMethodExecution(JoinPoint joinPoint) {
System.out.println("Before method execution: " + joinPoint.getSignature().getName());
}
// After advice: Logs after method execution
@After("execution(* com.example.demo.service.UserService.*(..))")
public void afterMethodExecution(JoinPoint joinPoint) {
System.out.println("After method execution: " + joinPoint.getSignature().getName());
}
// Around advice: Logs before and after method execution
@Around("execution(* com.example.demo.service.UserService.*(..))")
public Object aroundMethodExecution(JoinPoint joinPoint) throws Throwable {
System.out.println("Around method execution start: " + joinPoint.getSignature().getName());
Object result = joinPoint.proceed(); // Proceed to the actual method execution
System.out.println("Around method execution end: " + joinPoint.getSignature().getName());
return result;
}
}
এখানে:
- @Before: এই অ্যানোটেশনটি নিশ্চিত করে যে
beforeMethodExecution()মেথডটি টার্গেট মেথডের আগে কল হবে। - @After: এই অ্যানোটেশনটি নিশ্চিত করে যে
afterMethodExecution()মেথডটি টার্গেট মেথডের পরে কল হবে। - @Around: এই অ্যানোটেশনটি নিশ্চিত করে যে
aroundMethodExecution()মেথডটি টার্গেট মেথডের আগে এবং পরে কার্যকর হবে। এটি মেথডটির কার্যকারিতা নিয়ন্ত্রণ করতে সাহায্য করে।
JoinPoint ব্যবহার করে আমরা টার্গেট মেথডের নাম এবং অন্যান্য ডেটা পেতে পারি।
4. Spring Boot Application ক্লাস
Spring Boot অ্যাপ্লিকেশনে CommandLineRunner ইন্টারফেস ব্যবহার করে UserService এর মেথডগুলো কল করা হবে। যখন মেথডগুলো কল হবে, তখন LoggingAspect এর Advice কার্যকর হবে।
4.1 Application ক্লাস তৈরি করা
package com.example.demo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private UserService userService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
userService.createUser("John");
userService.deleteUser("John");
}
}
এখানে, CommandLineRunner ব্যবহার করা হয়েছে, যা অ্যাপ্লিকেশন রান হওয়ার সাথে সাথে UserService এর মেথডগুলি কল করবে।
5. আউটপুট
Spring AOP LoggingAspect এর মাধ্যমে Before, After, এবং Around Advice মেথডগুলির কার্যক্রম প্রদর্শিত হবে।
5.1 আউটপুট:
Around method execution start: createUser
Before method execution: createUser
User John created successfully.
After method execution: createUser
Around method execution end: createUser
Around method execution start: deleteUser
Before method execution: deleteUser
User John deleted successfully.
After method execution: deleteUser
Around method execution end: deleteUser
এখানে:
- Before Advice টার্গেট মেথডের আগে কার্যকর হয়েছে।
- After Advice টার্গেট মেথডের পরে কার্যকর হয়েছে।
- Around Advice টার্গেট মেথডের আগে এবং পরে কার্যকর হয়েছে, এবং এটি মেথডের বাস্তব কার্যকারিতা নিয়ন্ত্রণ করেছে।
6. সারাংশ
Spring AOP এর মাধ্যমে Logging কার্যক্রম সহজে এবং কার্যকরভাবে বাস্তবায়ন করা যায়। Before, After, এবং Around Advice ব্যবহার করে মেথডের আগে এবং পরে লোগিং কার্যক্রম প্রয়োগ করা যেতে পারে। AOP এর মাধ্যমে কোডের পুনঃব্যবহারযোগ্যতা এবং রিডেবিলিটি বৃদ্ধি পায়, কারণ লোগিং বা অন্যান্য ক্রস-কাটিং কনসার্ন এক জায়গায় রাখা যায় এবং একাধিক জায়গায় প্রয়োগ করা যায়।