이미지의 윤곽의 수, 벡터의 크기는 윤곽의 점수를 나타냅니다. Javabeans를 이해하십시오
해당 영어 단어 내재들은 introspector이며 주로 Javabeans를 운영하는 데 사용됩니다. Javabean은 특별한 Java 클래스이며 일부 방법은 특정 명명 규칙을 충족합니다. Java 클래스의 일부 방법이 특정 명명 규칙을 충족하면 Javabeans로 사용할 수 있습니다.
Javabean은 주로 데이터 정보를 전달하는 데 사용되는 특별한 Java 클래스입니다. 이 Java 클래스의 방법은 주로 개인 필드에 액세스하는 데 사용되며 메소드 이름은 특정 이름 지정 규칙을 준수합니다.
두 모듈 사이에 여러 정보를 전달하려면이 정보를 Javabean으로 캡슐화 할 수 있으며,이 정보는 일반적으로 valueObject (vo)라고합니다. 이 정보는 개인 필드를 사용하여 클래스에 저장됩니다. 이 필드의 값이 읽거나 설정되면 해당 방법을 통해 액세스해야합니다. 이 방법의 이름이 좋다고 생각하십니까? Javabean의 특성은 멤버 변수를 기반으로하지 않고 세터 및 getter 메소드를 기반으로 결정됩니다. 이 메소드를 setID라고하는 경우 중국어로 ID를 설정하는 것을 의미합니다. 어떤 변수를 저장하는지에 대해 작동합니까? 이 방법을 getID라고하는 경우 중국어로 ID를 얻는 것을 의미합니다. 어떤 변수를 가져 오는지에 대해 유용할까요? 세트 접두사를 제거하고 나머지 부분은 속성 이름입니다. 나머지 부분의 두 번째 문자가 소문자 인 경우 나머지 부분의 첫 글자를 작은 문자로 변경하십시오.
예를 들어:
setid () -> id의 속성 이름
islast ()의 속성 이름 -> last
SETCPU의 속성 이름은 무엇입니까? -> CPU
getups의 속성 이름은 무엇입니까? -> UPS
요컨대, 클래스가 Javabean으로 사용될 때, Javabean의 속성은 메소드 이름에 따라 추론되며 Java 클래스 내부의 멤버 변수를 전혀 볼 수 없습니다.
Javabeans의 특성을 준수하는 클래스는 정상적인 클래스로 사용될 수 있지만 Javabean으로 사용하면 Javabeans를 이해하고 적용하기 전에 몇 가지 추가 혜택을 가져올 수 있습니다! 이점은 다음과 같습니다.
Javaee 개발에서 Javabeans가 종종 사용됩니다. 많은 환경에는 Javabean 모드에서 작동이 필요합니다. 다른 사람들이 사용하고 이것을 요구한다면, 당신은 선택의 여지가 없습니다!
JDK는 Javabeans를 운영하기위한 API를 제공 하며이 API 세트를 내성이라고합니다. getx 메소드를 통해 개인 X에 직접 액세스하려면 어떻게하기가 어렵습니까? 내성을 사용하여 Javabeans를 운영하는 것이 일반 클래스를 사용하는 것보다 더 편리합니다.
Javabean의 간단한 내성
java.beans.propertydescriptor 클래스는 주로 특정 클래스 객체 속성 세트에서 javabean 속성을 얻은 다음 getReadMethod () 및 getWritemEthod () 메소드를 호출하여 해당 GET 및 설정 메소드를 얻는 데 사용됩니다.
Code example:
도메인 클래스 :
[CPP] ViewPlainCopy
intmain ()
패키지 usstc.lichunchun.bean; import java.util.date; public class reflectpoint {private date quiltpoint = new date (); public int y; public string str1 = "ball"; public String str2 = "basketball"; public string str3 = "itcast"; public reflectpoint (int x, int y) {super (this.x = x); {최종 int prime = 31; int result = 1; result = result = 1; prime * result + x; result = prime * result + y; return result;}@public boolean equals (객체 obj) {if (this == obj) return true; if (obj == null) false; if (getclass ()! = obj.getclass ()) return return return return return return (x! false; if (y! = other.y) return false; return true; return true;}@public string () {return str1 + "{return str1 +": " + str2 +": " + str3;} public int getx () {return x;} public void setx (int x) {this.x = x;} public int gety () {public void setty (int y) {it. getbirthday () {return return;} public void setbirthday (날짜 생일) {this.birthday = 생일;}}간단한 내성 조작 :
패키지 usstc.lichunchun.bean; import java.beans.beaninfo; import java.beans.introspectionException; import java.beans.introspectionException; import java.beans.introspector; import java.beans.propertydescriptor; import java.lang.reffect.invoctiontargetexcemence; java.lang.reflect.method; public class introspectortest {public static void main (string [] args)은 예외 {reflectpoint pt1 = new reflicpoint (3, 5); 문자열 속성 이름 = "x"; // "x"-> "x"-> "getx"-> methodproperty (pt1, getproperty); 7; setProperty (pt1, propertyname, value); system.out.println (pt1.getx ());} private static void setproperty (객체 PT1, String PropertyName, Object Value)는 introspectionException, 불법 행위, invocationDesception {PropertyDescfor (PropertyDescforter) pt1.getClass ()); 메소드 메소드 세트 X = pd.getWriteMethod (); methodsetx.invoke (pt1, value);} private static 객체 getProperty (Object PT1, String PropertyName)는 introspectionException, 불법 행위, invocationDesception {propertydescement (propertydescement) pt1.getClass ()); 메소드 메소드 getx = pd.getReadMethod (); methodgetX.invoke (pt1);}}Javabeans에 대한 복잡한 내성적 작업
모든 BeanInfo의 속성을 가로 지르는 방법을 사용하여 Reectpoint 객체의 x 속성을 찾고 설정하십시오. 이 프로그램에서 클래스를 Javabean으로 취급하면 introspector.getBeanInfo 메소드를 호출하는 것을 의미하며 결과 BeanInfo 객체는이 클래스를 Javabean으로 취급하는 결과 정보를 캡슐화합니다.
복잡한 내성적 작동 :
패키지 usstc.lichunchun.bean; import java.beans.beaninfo; import java.beans.introspectionException; import java.beans.introspectionException; import java.beans.introspector; import java.beans.propertydescriptor; import java.lang.reffect.invoctiontargetexcemence; java.lang.reflect.method; public class introspectortest {public static void main (string [] args)은 예외 {reflectpoint pt1 = new reflicpoint (3, 5); String PropertyName = "X"; // "X"-> "X"-> "getX"-> methodgetx = getProperty (pt1, pt1, PropertyName); system.out.println (retval); 객체 값 = 7; setProperty (pt1, propertyname, value); system.out.println (pt1.getx ());} private static void setproperty (객체 pt1, 문자열 propertyname, object value) wrows introspection exception, invelegalaccessect ecompeence pd = new PropertyDescriptor (PropertyName, pt1.getClass ()); 메소드 메소드 세트 X = pd.getWriteMethod (); methodsetx.invoke (pt1, value);} 개인 정적 객체 getProperty (객체 pt1, 문자열 propertyname) throws 내장 내장, 불법적 인 propertordescemention, invocationdescemention, invocationdescemention, PropertyDescriptor (PropertyName, pt1.getClass ()); 메소드 메소드 getx = pd.getReadMethod (); MethodGetX.invoke (PT1); */beanInfo beanInfo = introspector.getBeanInfo (pt1.getClass ()); propertyDescriptor [] pds = beanInfo.getPropertyDescriptors (); object retval = null; for (propertyDescriptor pd : pds) {if (pd.getName (). pd.getReadMethod (); retval = methodgetx.invoke (pt1); break;}} return retval;}}}}Beanutils 툴킷을 사용하여 Javabean을 조작합니다
이전 내성 예제에 따라 Beanutils 클래스를 사용하여 원래 세트 속성을 먼저 가져온 다음 새 값으로 설정하십시오. get 속성이 문자열 일 때 결과가 반환되었습니다. 세트 속성이 있으면 모든 유형의 객체를 허용 할 수 있으며 일반적으로 문자열이 사용됩니다.
PropertyUtils 클래스를 사용하여 원래 세트 속성을 먼저 가져온 다음 새 값으로 설정하십시오. get 속성이 속성의 원래 유형 일 때 결과는 반환되고 세트 속성이 속성의 원래 유형 일 때.
참고 :이 두 클래스를 사용하기 전에 Eclipse Project의 Lib 폴더에서 2 개의 JAR 패키지 Commons-Beanutils.jar와 Commons-Logging-1.jar를 가져와야합니다.
Code example:
패키지 usstc.lichunchun.bean; import java.beans.beaninfo; import java.beans.introspectionException; import java.beans.introspectionException; import java.beans.introspector; import java.beans.propertydescriptor; import java.lang.reffect.invoctiontargetexcemence; java.lang.reflect.method; import org.apache.commons.beanutils.beanutils; import org.apache.commons.beanutils.propertyutils; public static void main (String [] arg) 예외 {Reflectpoint pt1 = new Propertppoint (3, 5); "x"; // "x"-> "x"-> "getx"-> methodgetx-> object retval = getProperty (pt1, propertyname); system.out.println (retval); 객체 값 = 7; setProperty (pt1, propertyname, value); system.out.println (pt1, get1, get1, getsproperty "x"). getClass (). getName ()); // string beanutils.setProperty (pt1, "x", "9"); System.out.println (pt1.getx ());/* map map = {이름 : "zxx", age : 18}; */beanutils.setProperty (pt1, "생일. 타임", "111"); // 속성 체인 시스템 .out.out.println (beanutils.getProperty (pt1, "birthday.time"); propertyUtils.setProperty (pt1, "x", 23); system.out.property (pt1, gget. "x"). getClass (). getName ()); // integer/* beanutils와 propertyutils 간의 차이 : beanutils는 문자열 형태로 Javabeans를 운영하고,지도 클래스를 운영 할 수 있으며, Javabeans와지도를 서로 변환 할 수 있습니다 (설명, 인구는 자사 부동산 자체로 구성된) String PropertyName, Object Value)는 IntrospectionException, INLEGALACCESSException, invocationTargeteXception {propertyDescriptor pd = new PropertyDescriptor (propertyName, pt1.getClass ()); 메소드 메소드 = pd.getWritemEthod (); MethodSetx.invoke (PT1, value);} 개인 정적 객체 getProperty (객체 PT1, String PropertyName)는 IntrospectionException, 불법 행위 텍스크, invocationTargetexception {/* PropertyDescriptor PD = New PropertyDescriptor (PropertyName, Pt1.getClass ()); 메소드 메소드 getx = pd.getReadMethod (); MethodGetX.invoke (PT1); */beanInfo beanInfo = introspector.getBeanInfo (pt1.getClass ()); propertyDescriptor [] pds = beanInfo.getPropertyDescriptors (); object retval = null; for (propertyDescriptor pd : pds) {if (pd.getName (). pd.getReadMethod (); retval = methodgetx.invoke (pt1); break;}} return retval;}}}}요약
위의 내용은 Java 내성적 인 사례의 분석에 관한 것이며, 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!