Copy Properties from One Bean to Another

Spring provides copyProperties method which allows to copy values of properties from one bean to another, the thing is property name & type should be same in both beans .

package com.test;

public class JavaT {

public class hello {

String name;
String surname;

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getName() {
return name;
}

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

public hello(String name, String surname) {
this.name = name;
this.surname = surname;
}
}

public class hi {

String surname;

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public hi() {
}
}

public void go() {
JavaT.hello hello = new JavaT.hello("taher", "tinwala");
JavaT.hi hi = new hi();
try {
org.springframework.beans.BeanUtils.copyProperties(hello, hi);
} catch (Exception ex) {
System.out.println("Erroor :: "+ex);
}
System.out.println("hi : "+hi.getSurname());
}

public static void main(String[] args) {
JavaT javaT = new JavaT();
javaT.go();
}
}
Advertisements
This entry was posted in Spring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s