------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
javaBean是一个特殊的java类。
通过PropertyDescriptor类获得get和set方法。
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class JavaBeanTest {
public static void main(String[] args)throws Exception{
P p=new P(2,3);
String propertyName="x";
//"x"-->"X"-->"getX"--->MethodGetX-->
PropertyDescriptor pd=new PropertyDescriptor(propertyName,p.getClass());
Method methodGetX=pd.getReadMethod();//只读
Object retVal=methodGetX.invoke(p);
System.out.println(retVal);
Method methodSetX=pd.getWriteMethod();//只写
methodSetX.invoke(p,9);
System.out.println(p.getX());
}
}
class P
{
public int x;
public int y;
public P(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
当把该类当作javaBean看待时,可以直接调用IntroSpector.getBeanInfo的方法,得到的BeanInfo对象封装了这个类当作javaBean看的结果信息
实例:
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class JavaBeanTest2 {
public static void main(String[] args)throws Exception{
String propertyName ="x";
P p=new P(2,3);
BeanInfo beanInfo=Introspector.getBeanInfo(p.getClass());
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
Object retVal=null;
for(PropertyDescriptor pd:pds){
if(pd.getName().equals(propertyName))
{
Method methodGetX=pd.getReadMethod();
retVal=methodGetX.invoke(p);
}
}
System.out.println(retVal);
}
}
class P
{
public int x;
public int y;
public P(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
使用BeanUtils工具包操作JavaBean。
BeanUtils是以String形式进行操作。
PropertyUtils是以自己本身的类型操作。
这都需要加入工具包。引入外部jar。
commons-beanutils-1.9.2.jar
commons-logging-1.2.jar
代码展示:
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class JavaBeanTest2 {
public static void main(String[] args) throws Exception {
P p = new P(2, 3);
System.out.println(BeanUtils.getProperty(p, "x"));
System.out.println(BeanUtils.getProperty(p, "x").getClass().getName());
//BeanUtils是String类型操作
BeanUtils.setProperty(p, "x", "7");
System.out.println(p.getX());
}
}
class P {
public int x;
public int y;
public P(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
运行出错:
解决办法:
不能把所要访问的类放在同一个class文件,因该单独写个class类,并访问权限是public
Pott.java
public class Pott {
public int x;
public int y;
public Pott(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
JavaBeanTest2.java
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class JavaBeanTest2 {
public static void main(String[] args) throws Exception {
Pott p = new Pott(2, 3);
System.out.println(BeanUtils.getProperty(p, "x"));
System.out.println(BeanUtils.getProperty(p, "x").getClass().getName());
//BeanUtils是String类型操作
BeanUtils.setProperty(p, "x", "7");
System.out.println(p.getX());
//PropertyUtils是以自己本事类型操作的
PropertyUtils.setProperty(p, "x", 9);
System.out.println(PropertyUtils.getProperty(p, "x").getClass().getName());
}
}
修改后实际运行结果: