BeanUtils এর মাধ্যমে Bean এর মধ্যে Data Populating

Populating Beans (বিন পপুলেশন) - জাভা বীনইউটিলস (Java BeanUtils) - Computer Programming

369

Apache Commons BeanUtils লাইব্রেরি ব্যবহার করে আপনি সহজেই Java Beans-এর মধ্যে ডেটা populate করতে পারেন। এটি সাধারণত Bean-এর মধ্যে ডেটা প্রবাহিত (populate) করার জন্য ব্যবহৃত হয়, যা আপনাকে ইনপুট ডেটা সংগ্রহ, প্রক্রিয়া, এবং আউটপুট ডেটা গঠন করতে সহায়তা করে।

যেমন, আপনি একটি form থেকে ডেটা গ্রহণ করে সেটি একটি Java Bean-এ populate করতে চাইলে, BeanUtils.populate() মেথডটি খুবই কার্যকরী। এটি আপনাকে সরাসরি Bean-এ প্রপার্টি সেট করার পরিবর্তে, একটি map বা অন্যান্য ডেটা সোর্স থেকে Java Bean-এ ডেটা স্থানান্তর করতে সাহায্য করে।


BeanUtils.populate() মেথড

populate() মেথডটি একটি Map বা Properties থেকে Bean-এ ডেটা populate করতে ব্যবহার করা হয়।

সেন্ট্যাক্স:

BeanUtils.populate(Object bean, Map<String, ? extends Object> properties) throws IllegalAccessException, InvocationTargetException
  • bean: যে Bean-এ ডেটা populate করতে হবে।
  • properties: একটি Map (যেমন HashMap) যেখানে key হলো Bean-এর প্রপার্টির নাম এবং value হলো প্রপার্টির মান।

উদাহরণ: BeanUtils.populate() ব্যবহার করে Bean-এ ডেটা Populating

Step 1: Java Bean তৈরি করা

import java.io.Serializable;

public class Person implements Serializable {
    private String name;
    private int age;

    // Constructor, getter and setter methods
    public Person() {}

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

এখানে, Person ক্লাসটি একটি সাধারণ Java Bean যেটিতে দুটি প্রপার্টি (name এবং age) রয়েছে।

Step 2: BeanUtils.populate() মেথড ব্যবহার করে Data Populate করা

import org.apache.commons.beanutils.BeanUtils;
import java.util.HashMap;
import java.util.Map;

public class BeanUtilsPopulateExample {
    public static void main(String[] args) {
        try {
            // Create a new Person object (Bean)
            Person person = new Person();

            // Create a Map with property names and values to populate into the Bean
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("name", "John");
            dataMap.put("age", 25);

            // Use BeanUtils.populate() to populate the Person object with data from the Map
            BeanUtils.populate(person, dataMap);

            // Output the populated properties of the Person object
            System.out.println("Name: " + person.getName());  // Output: John
            System.out.println("Age: " + person.getAge());    // Output: 25
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ব্যাখ্যা:

  1. Map তৈরি করা: আমরা একটি Map তৈরি করেছি, যেখানে key হল Person Bean-এর প্রপার্টি নাম (যেমন "name" এবং "age") এবং value হল সেই প্রপার্টির মান।
  2. populate() মেথড ব্যবহার: BeanUtils.populate() মেথড ব্যবহার করে, এই Map থেকে ডেটা Person Bean-এ populate করা হয়েছে।
  3. Output: পরে, person অবজেক্ট থেকে গেটার মেথড ব্যবহার করে name এবং age প্রপার্টির মান দেখানো হয়েছে।

populate() মেথডের সুবিধা

  1. ডেটা populate করার সহজ পদ্ধতি: populate() মেথড ব্যবহার করে একটি Map বা Properties থেকে Java Bean-এ ডেটা populate করা খুবই সহজ।
  2. নাম এবং টাইপ মিলিয়ে ডেটা সেট করা: এটি নিশ্চিত করে যে Bean-এর প্রপার্টির নামের সাথে Map-এর key মিলছে, এবং টাইপগুলোও সঠিক।
  3. নেস্টেড প্রপার্টি সাপোর্ট: nested properties (যেমন, Bean এর মধ্যে অন্য Bean) populate করা সম্ভব।

উদাহরণ: নেস্টেড প্রপার্টি সাপোর্ট

Step 1: নেস্টেড Bean তৈরি করা

public class Address {
    private String street;
    private String city;

    public Address() {}

    public Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

public class Person {
    private String name;
    private int age;
    private Address address;

    public Person() {}

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // Getter and setter methods
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

Step 2: নেস্টেড প্রপার্টি Populate করা

import org.apache.commons.beanutils.BeanUtils;
import java.util.HashMap;
import java.util.Map;

public class NestedBeanPopulateExample {
    public static void main(String[] args) {
        try {
            // Create a new Person object (Bean)
            Person person = new Person();

            // Create a Map with property names and values to populate into the Bean
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("name", "John");
            dataMap.put("age", 30);

            // Create a Map for the nested Address object
            Map<String, Object> addressMap = new HashMap<>();
            addressMap.put("street", "Street 123");
            addressMap.put("city", "City A");

            // Populate the nested Address object
            BeanUtils.populate(person, dataMap);
            BeanUtils.populate(person.getAddress(), addressMap);

            // Output the populated properties
            System.out.println("Name: " + person.getName());            // Output: John
            System.out.println("Age: " + person.getAge());              // Output: 30
            System.out.println("Street: " + person.getAddress().getStreet());  // Output: Street 123
            System.out.println("City: " + person.getAddress().getCity());      // Output: City A
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ব্যাখ্যা:

  1. নেস্টেড Address Bean: Person Bean-এ একটি Address Bean অন্তর্ভুক্ত করা হয়েছে।
  2. populate() মেথড: BeanUtils.populate() মেথড ব্যবহার করে person এবং person.address প্রপার্টি populate করা হয়েছে।

সারাংশ

  • BeanUtils.populate() মেথড Java Bean-এ ডেটা populate করার একটি সহজ এবং কার্যকরী পদ্ধতি।
  • এটি Map বা Properties থেকে Bean-এ ডেটা সেট করার জন্য ব্যবহৃত হয়, যা ডেটা ট্রান্সফার, ফর্ম হ্যান্ডলিং এবং টেস্টিং এর ক্ষেত্রে খুবই সহায়ক।
  • নেস্টেড properties সাপোর্ট করে, অর্থাৎ এক Bean এর মধ্যে অন্য Bean থাকলে, সেটিও populate করা সম্ভব।
Content added || updated By
Promotion

Are you sure to start over?

Loading...