[ת]JavaÖ®metadata(ÔªÊý¾Ý)Ïê½â(2)
±ê×¼µÄAnnotation
JDK1.5ÌṩÁËÈô¸ÉµÄ±ê×¼AnnotationÀ´²¹³äÓï·¨¶¨Ò壬»òÕß±ê¼ÇAnnotation¡£±ê×¼µÄAnnotationÓÐÒÔϼ¸¸ö£º
Annotation |
ʹÓ÷¶Î§ |
ÓÃ; |
Deprecated |
ËùÓÐ |
½«Ä¿±ê±ê¼ÇΪ²»ÍƼöʹÓà |
SuppressWarnings |
³ýÁË°üºÍAnnotation |
½ûÖ¹±ê¼Ç¶ÔÏó·¢³ö±»±ê¼ÇµÄ¾¯¸æÐÅÏ¢ |
Override |
·½·¨ |
±ê¼ÇÕâ¸ö·½·¨ÖØдÁ˸¸ÀàµÄ·½·¨ |
Target |
Annotation |
±ê¼ÇAnnotationµÄÊÊÓ÷¶Î§ |
Retention |
Annotation |
±ê¼ÇAnnotation×îÖÕפÁôµÄµØ·½ |
Documented |
Annotation |
¸ÃAnnotationÔÚJavaDocÎĵµÖгöÏÖ |
Inherited |
Annotation |
¸ÃAnnotationĬÈϱ»Ê¹ÓøÃAnnotationµÄËùÓÐ×ÓÀà¼Ì³Ð |
ÏÂÃæ¾ßÌå½²½â±ê×¼AnnotationµÄÓ÷¨¡£
³£ÓõÄAnnotation°üÀ¨ÒÔÏÂÈý¸ö£º@Deprecated @SuppressWarnings @Override£¬ËûÃǵÄÓÃ;·Ö±ðÈçÉϱíËùʾ¡£
ÒÔÏÂ˵Ã÷µÄAnnotationÓÐÒ»¸ö¹²Í¬µÄÌصã¾ÍÊÇËûÃǶ¼Ö»ÄÜÓÃÔÚAnnotationµÄÉùÃ÷ÉÏ¡£
@TargetÓÃÀ´±ê¼ÇAnnotationÊÊÓõķ¶Î§£¬@TargetÓÐһЩԤ¶¨ÒåµÄÊôÐÔ£¬ÈçϱíËùʾ£º
ÀàÐÍ |
ÊÊÓ÷¶Î§ |
ANNOTATION_TYPE |
Ö»ÄÜÓÃÔÚAnnotationµÄÉùÃ÷ÖÐ |
PACKAGE |
ÓÃÔÚ°üÉÏ |
TYPE |
Àࣨ°üÀ¨Ã¶¾Ù£©»òÕß½Ó¿Ú£¨°üÀ¨Annotation£© |
METHOD |
·½·¨ |
CONSTRUCTOR |
¹¹Ôì·½·¨ |
FIELD |
×ֶΣ¨°üº¬Ã¶¾ÙÄÚ²¿µÄ³£Á¿£© |
PARAMETER |
·½·¨»òÕß¹¹Ôì·½·¨µÄ²ÎÊý |
LOCAL_VARIABLE |
±¾µØ±äÁ¿ |
@RetentionÓÃÀ´±ê¼ÇAnnotationÔÚÄÇЩ·¶Î§£¨Ô´´úÂ룬ÀàÎļþ»òÕßÔËÐÐʱ£©ÄÚÊÇ¿ÉÓõġ£@RetentionÓ붨ÒåÁËһЩÊôÐÔ£¬ÈçϱíËùʾ£º
פÁô²ßÂÔ |
ÃèÊö |
SOURCE |
AnnotationÖ»´æÔÚÓÚÔ´´úÂëÖУ¬²»°üÀ¨ÔÚ±àÒëÉú³ÉClassÎļþÖÐ |
CLASS |
Annotation´æÔÚÓÚÔ´´úÂëÖУ¬Ò²´æÔÚÓÚ±àÒëÉú³ÉµÄClassÎļþÖУ¬µ«ÊÇÔÚÔËÐÐʱÕâЩAnnotation²»±»JVM×°ÔØ¡£ |
RUNTIME |
Annotation´æÔÚÓÚÔ´´úÂëÖУ¬Ò²´æÔÚÓÚ±àÒëÉú³ÉµÄClassÎļþÖУ¬Í¬Ê±ÔÚÔËÐÐʱÕâЩAnnotation±»×°Ôص½JVMÄÚ²¿£¬¿ÉÒÔʹÓ÷´ÉäµÄ»úÖÆÔÚÔËÐÐʱʹÓᣠ|
@DocumentedÓÃÀ´½«AnnotationÏÔʾÔÚÉú³ÉµÄJavaDocÖС£
@InheritedÖ»ÄÜÓÃÀ´±ê¼ÇÓÃÔÚÀàÉϵÄAnnotation£¬ÓÃÀ´ËµÃ÷±»±ê¼ÇµÄAnnotation»á±»¸ÃÀàµÄËùÓÐ×ÓÀà×Ô¶¯µÄ¼Ì³Ð¡£
AnnotationÓ¦ÓÃʵÀý
/**
* http://www.onjava.com/pub/a/onjava/2005/01/19/metadata_validation.html
* by Jacob Hookom
*/
ʹÓÃAnnotationµÄÒ»¸öÀý×Ó¾ÍÊǽ¨Á¢Ò»¸ö¼òµ¥µÄÓû§ÊäÈëÑéÖ¤¿ò¼Ü£¬Ê¹ÓÃÕâ¸ö¿ò¼Ü×îÖÕÓû§¿ÉÒÔÒÔÈçϵķ½Ê½À´¶¨Òå×ֶεÄУÑéÊôÐÔ£º
@ValidateRequired
@ValidateEmail
public void setEmail(String email) {
this.email = email;
}
@ValidateRequired
@ValidateLength(min=6,max=12)
public void setPassword(String password) {
this.password = password;
}
ÒÔÉϵĴúÂë˵Ã÷£¬email×Ö¶ÎÊDZØÐëµÄ£¬ÇÒ±ØÐëÂú×ãemailµÄУÑéÒªÇó£¬Í¬Ê±password×Ö¶ÎÒ²ÊDZØÐëµÄ£¬ÇÒ³¤¶È±ØÐëÔÚ6~12Ö®¼ä¡£ÓÐÁËÕâЩ¶¨ÒåÖ®ºóÎÒÃÇÄܹ»Ê¹ÓÃÈçϵĴúÂë´ïµ½ÑéÖ¤µÄЧ¹û£º
Validator.validate(loginBean, "email", "yourname@onjava.com"); //pass
Validator.validate(loginBean, "password", ""); // failure
ÒªÄܹ»´ïµ½ÉÏÊöµÄÒªÇó£¬ÎÒÃDZØÐ붨ÒåһЩAnnotation£¬ÒÔÏ´úÂëÊÇValidateLengthºÍValidateExprµÄÉùÃ÷£º
package annotations.validates;
//Example @ValidateLength(min=6,max=8)
public @interface ValidateLength {
int min() default 0;
int max() default Integer.MAX_VALUE;
}
//Example @ValidateExpr("^(\\w){0,2}$");
public @interface ValidateExpr {
String value();
}
¾ßÌ忪·¢µÄ¹ý³ÌÖÐÎÒÃÇ»áÓöµ½Ò»Ð©ÎÊÌ⣬ÕâÖ÷ÒªÓÉÓÚÁ½·½ÃæµÄÔÒâ²úÉú
µÚÒ»£¬AnnotationÄÚ²¿²»Äܶ¨Òå·½·¨£¬Ö»Äܶ¨ÒåһЩ״̬¡£
µÚ¶þ£¬Annotation²»ÔÊÐíʹÓü̳У¨extends»òÕßimplements£©£¬ÕâÒâζ×ÅÎÒÃDz»ÄÜÔÚ·´ÉäµÄ¹ý³ÌÖÐʹÓÃinstance ofÕâÑùµÄ¹¦ÄÜ¡£
ΪÁËʶ±ð³öÓÚÎÒÃǶ¨ÒåµÄУÑéÏà¹ØµÄAnnotationÎÒÃǶ¨ÒåÁËÒ»¸öÈçϵÄAnnotation£º
package annotations.validates;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Validate {
//Class<ValidateExprHandler> value();
Class<? extends ValidateHandler> value();
}
ÕýÈçÄãËù¿´µ½µÄValidate¿ÉÒÔפÁôÔÚJVMÄÚ²¿£¬¼´Ëü¿ÉÒÔÔÚÔËÐÐʱͨ¹ý·´ÉäµÄ·½Ê½Ê¹Óá£Í¬Ê±Ëü±ØÐëÓÃÀ´±ê¼ÇÆäËûµÄAnnotation£¬Í¬Ê±ËûÓÐÒ»¸öClassÀàÐ͵ÄvalueÔªËØ£¬Õâ¸öÀàÐͱØÐë´ÓValidateHandler¼Ì³Ð¶øÀ´£¬Ö÷ÒªÓÃÀ´´¦Àí¾ßÌåµÄÑéÖ¤Âß¼¡£
ÔÚ´ËÉè¼Æ֮Ͽ´¿´ÎÒÃÇÈçºÎÉùÃ÷Ò»¸öValidateExprµÄAnnotation¶ÔÏó£º
package annotations.validates;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Validate(ValidateExprHandler.class)
public @interface ValidateExpr {
String value();
}
ÍƼöÐÅÏ¢
- ÔÝÎ޼Ǽ
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- java urlÖØд¼¼Êõ(UrlReWriter)
- [ת]JavaµÄÊý×飨Array£©¡¢Vector¡¢ArrayLis....
- jsp»·¾³ÏÂajaxÂÒÂëÎÊÌâµÄ½â¾ö
- JSP¼¼Êõ Ö¸Áî ÂÒÂëÎÊÌâ ÔËÐÐÔÀíºÍ¾Å´óÒþʽ¶ÔÏó web¿ª·¢µÄË....
- JAVA ˮӡ ͼƬ ÎÄ×Ö
- java¶þ½øÖÆ£¬ÔÂë¡¢·´Âë¡¢²¹Â롢λÔËËã
- Java WebÓ¦ÓÃÆô¶¯¼ä¸ôÖ´ÐеijÌÐò
- jspÉú³ÉxmlÎļþʾÀý
- javaÖÐÀà±äÁ¿¡¢Àà·½·¨¡¢ÊµÀý±äÁ¿¡¢ÊµÀý·½·¨
- JSPÒ³ÃæµÄÎåÖÖÌøת·½·¨
ÆÀÂÛ