SpEL অপারেটর কি?
Spring Expression Language (SpEL) একটি শক্তিশালী এক্সপ্রেশন ল্যাঙ্গুয়েজ যা Spring Framework এর মধ্যে ব্যবহৃত হয়। SpEL এর মাধ্যমে আপনি বিভিন্ন ধরণের অপারেটর ব্যবহার করতে পারেন, যেমন অ্যাথ্রিবিউট অ্যাক্সেস, গণনা, কন্ডিশনাল এক্সপ্রেশন, লজিক্যাল অপারেটর ইত্যাদি। SpEL অপারেটরগুলি ডাইনামিক এক্সপ্রেশন তৈরি করতে ব্যবহৃত হয়, যা Spring Beans বা অবজেক্টের প্রপার্টি অ্যাক্সেস এবং ম্যানিপুলেট করার জন্য উপকারী।
SpEL এর অপারেটরগুলোর মধ্যে প্রধান কয়েকটি অপারেটরের ব্যবহার নিম্নে আলোচনা করা হয়েছে।
SpEL অপারেটরগুলোর ধরন
- গণনামূলক অপারেটর (Arithmetic Operators)
- লজিক্যাল অপারেটর (Logical Operators)
- সম্পর্ক অপারেটর (Relational Operators)
- ত্রৈমাসিক অপারেটর (Ternary Operator)
- অ্যাথ্রিবিউট এক্সপ্রেশন অপারেটর (Attribute Access Operator)
- মেথড কল অপারেটর (Method Call Operator)
- ফাংশনাল অপারেটর (Function Operators)
- কনস্ট্যান্ট অপারেটর (Constant Operator)
১. গণনামূলক অপারেটর (Arithmetic Operators)
SpEL এ গণনামূলক অপারেটর ব্যবহার করে অ্যাডিশন, সাবট্রাকশন, মাল্টিপ্লিকেশন, ডিভিশন এবং মডুলাস অপারেশন করা যায়।
উদাহরণ:
ExpressionParser parser = new SpelExpressionParser();
// অ্যাডিশন
Integer addition = parser.parseExpression("10 + 5").getValue(Integer.class);
System.out.println("Addition: " + addition); // Output: 15
// সাবট্রাকশন
Integer subtraction = parser.parseExpression("10 - 5").getValue(Integer.class);
System.out.println("Subtraction: " + subtraction); // Output: 5
// মাল্টিপ্লিকেশন
Integer multiplication = parser.parseExpression("10 * 5").getValue(Integer.class);
System.out.println("Multiplication: " + multiplication); // Output: 50
// ডিভিশন
Integer division = parser.parseExpression("10 / 5").getValue(Integer.class);
System.out.println("Division: " + division); // Output: 2
// মডুলাস
Integer modulus = parser.parseExpression("10 % 3").getValue(Integer.class);
System.out.println("Modulus: " + modulus); // Output: 1
২. লজিক্যাল অপারেটর (Logical Operators)
SpEL এর মাধ্যমে লজিক্যাল অপারেটর ব্যবহার করে AND, OR, NOT ইত্যাদি অপারেশন করা যায়।
উদাহরণ:
ExpressionParser parser = new SpelExpressionParser();
// AND অপারেটর
Boolean andOperation = parser.parseExpression("true and false").getValue(Boolean.class);
System.out.println("AND Operation: " + andOperation); // Output: false
// OR অপারেটর
Boolean orOperation = parser.parseExpression("true or false").getValue(Boolean.class);
System.out.println("OR Operation: " + orOperation); // Output: true
// NOT অপারেটর
Boolean notOperation = parser.parseExpression("not true").getValue(Boolean.class);
System.out.println("NOT Operation: " + notOperation); // Output: false
৩. সম্পর্ক অপারেটর (Relational Operators)
SpEL-এ সম্পর্ক অপারেটর ব্যবহার করে কোনো মানের মধ্যে তুলনা করা যায়, যেমন ==, !=, >, <, >=, <=।
উদাহরণ:
ExpressionParser parser = new SpelExpressionParser();
// সমান
Boolean equal = parser.parseExpression("5 == 5").getValue(Boolean.class);
System.out.println("Equal: " + equal); // Output: true
// ভিন্ন
Boolean notEqual = parser.parseExpression("5 != 3").getValue(Boolean.class);
System.out.println("Not Equal: " + notEqual); // Output: true
// বড়
Boolean greaterThan = parser.parseExpression("10 > 5").getValue(Boolean.class);
System.out.println("Greater Than: " + greaterThan); // Output: true
// ছোট
Boolean lessThan = parser.parseExpression("5 < 10").getValue(Boolean.class);
System.out.println("Less Than: " + lessThan); // Output: true
// বড় বা সমান
Boolean greaterThanOrEqual = parser.parseExpression("10 >= 5").getValue(Boolean.class);
System.out.println("Greater Than or Equal: " + greaterThanOrEqual); // Output: true
// ছোট বা সমান
Boolean lessThanOrEqual = parser.parseExpression("5 <= 10").getValue(Boolean.class);
System.out.println("Less Than or Equal: " + lessThanOrEqual); // Output: true
৪. ত্রৈমাসিক অপারেটর (Ternary Operator)
SpEL-এ ত্রৈমাসিক অপারেটর (conditional operator) ব্যবহার করে সরল কন্ডিশনাল এক্সপ্রেশন তৈরি করা যায়।
উদাহরণ:
ExpressionParser parser = new SpelExpressionParser();
// ত্রৈমাসিক অপারেটর
String result = parser.parseExpression("10 > 5 ? 'Yes' : 'No'").getValue(String.class);
System.out.println("Ternary Operator Result: " + result); // Output: Yes
৫. অ্যাথ্রিবিউট এক্সপ্রেশন অপারেটর (Attribute Access Operator)
SpEL ব্যবহার করে কোনো অবজেক্টের প্রপার্টি অ্যাক্সেস করা যায়। উদাহরণস্বরূপ, employee.name দিয়ে Employee অবজেক্টের name ফিল্ড অ্যাক্সেস করা।
উদাহরণ:
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
ExpressionParser parser = new SpelExpressionParser();
Employee employee = new Employee("John");
// অ্যাথ্রিবিউট এক্সপ্রেশন
String employeeName = parser.parseExpression("name").getValue(employee, String.class);
System.out.println("Employee Name: " + employeeName); // Output: John
৬. মেথড কল অপারেটর (Method Call Operator)
SpEL-এ মেথড কল করার জন্য .() ব্যবহার করা হয়। আপনি যে কোন মেথড কল করতে পারবেন এবং তার রিটার্ন ভ্যালু ব্যবহার করতে পারবেন।
উদাহরণ:
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getGreeting() {
return "Hello, " + name;
}
}
ExpressionParser parser = new SpelExpressionParser();
Employee employee = new Employee("John");
// মেথড কল
String greeting = parser.parseExpression("getGreeting()").getValue(employee, String.class);
System.out.println("Greeting: " + greeting); // Output: Hello, John
৭. ফাংশনাল অপারেটর (Function Operators)
SpEL-এ বিভিন্ন ফাংশনাল অপারেটর যেমন T(className) ব্যবহার করে Java ক্লাসের কনস্ট্যান্ট এক্সেস করা যায়।
উদাহরণ:
ExpressionParser parser = new SpelExpressionParser();
// Math.PI এর মান ব্যবহার
Double piValue = parser.parseExpression("T(java.lang.Math).PI").getValue(Double.class);
System.out.println("PI Value: " + piValue); // Output: 3.141592653589793
৮. কনস্ট্যান্ট অপারেটর (Constant Operator)
SpEL-এ কনস্ট্যান্ট মান ব্যবহার করতে T() ব্যবহার করা হয়, যা কোনো নির্দিষ্ট ক্লাসের কনস্ট্যান্ট এক্সপ্রেস করতে সহায়তা করে।
উদাহরণ:
ExpressionParser parser = new SpelExpressionParser();
// Static Constant Access
Integer maxValue = parser.parseExpression("T(java.lang.Integer).MAX_VALUE").getValue(Integer.class);
System.out.println("Max Integer Value: " + maxValue); // Output: 2147483647
সারাংশ
SpEL (Spring Expression Language) এর অপারেটরগুলি Spring অ্যাপ্লিকেশনে বিভিন্ন ধরনের এক্সপ্রেশন তৈরি করতে সহায়ক। এগুলোর মাধ্যমে আপনি গণনা, তুলনা, কন্ডিশনাল লজিক, মেথড কল, ফাংশন এক্সপ্রেশন এবং আরো অনেক কিছু করতে পারেন। SpEL এর অপারেটরগুলোর ব্যবহার Spring Beans এবং অবজেক্ট ম্যানিপুলেশন সহজ এবং কার্যকরী করে তোলে।
Read more