[ת]JavaÖ®metadata(ÔªÊý¾Ý)Ïê½â(3)
ValidateExprµÄÇ°Á½¸öAnnotation²»Óöི£¬Ö÷Ҫ˵˵@Validate(ValidateExprHandler.class)
µÄº¬Ò壬ÕâÑù½â¾öÁËÎÒÃÇÇ°±ßÌáµ½µÄÁ½¸öÎÊÌ⣬µÚÒ»¡¢ÎÒÃÇ¿ÉÒÔ¿´¿´Ò»¸öAnnotationÊÇ·ñÓÐValidateÀàÐ͵ÄAnnotationÀ´È·¶¨Õâ¸öAnnotationÊDz»ÊÇÎÒÃÇУÑé¿ò¼ÜÄÚ²¿Ê¹ÓõÄAnnotation¡£Í¬Ê±ÎÒÃÇÒ²ÌṩÁËÒ»¸ö¾ßÌåµÄÀàValidateExprHandlerÀ´´¦ÀíУÑéÂß¼¡£
½ÓÏÂÀ´ÎÒÃÇ¿´¿´ValidateExprHandlerµÄʵÏÖ:
------------------------------------------------------------------------------------------------------------------------------------------
package annotations.validates;
import java.lang.annotation.Annotation;
import javax.xml.bind.ValidationException;
//¶¨ÒåÁËÒ»¸öValidateHandler½Ó¿Ú£¬
//Õâ¸ö½Ó¿ÚÓÐÒ»¸öAnnotationÀàÐ͵ÄÄ£°æ²ÎÊý
public interface ValidateHandler<T extends Annotation> {
public void validate(T settings, Object value) throws ValidationException;
public Class<T> getSettingsType();
}
------------------------------------------------------------------------------------------------------------------------------------------
package annotations.validates;
import java.util.regex.Pattern;
import javax.xml.bind.ValidationException;
//Ò»¸öValidateHandlerµÄʵÀý£¬ÓÃÀ´´¦ÀíÕýÔò±í´ïʽµÄÑéÖ¤£¬
//ÆäÖеÄAnotationÀàÐ͵IJÎÊýΪValidateExpr
public class ValidateExprHandler implements ValidateHandler<ValidateExpr> {
public void validate(ValidateExpr settings, Object value)
throws ValidationException {
// TODO Auto-generated method stub
String i = (value != null) ? value.toString() : "";
if (!Pattern.matches(settings.value(), i)) {
throw new ValidationException(i + " does not match the pattern "
+ settings.value());
}
}
public Class<ValidateExpr> getSettingsType() {
// TODO Auto-generated method stub
return ValidateExpr.class;
}
}
------------------------------------------------------------------------------------------------------------------------------------------
˵Ã÷£º
1. ÎÒÃǶ¨ÒåÁËÒ»¸öAnnotation£¨Validate£©À´±ê¼ÇÎÒÃÇËùÓеÄУÑéÓÃAnnotation£¬
ͬʱÿһ¸ö¾ßÌåµÄУÑéÓõÄAnnotation£¨ValidateExpr£©¶¼±ØÐëÌṩһ¸öÓÃÀ´¾ßÌå´¦Àí
УÑéÂß¼µÄÀࣨValidateExprHandler£©¡£
2. Annotation²»ÔÊÐí¼Ì³Ð£¬ËùÒÔÎÒÃÇûÓа취ÊÊÓÃinstance ofµÄ·½·¨À´Ê¶±ðÒ»¸öУÑé¿ò¼Ü
ʹÓõÄAnnotation£¬µ«ÊÇͨ¹ý¶ÔÎÒÃÇʹÓõÄУÑéÓõÄAnnotationÌí¼ÓAnnotation£¨Validate£©
ÎÒÃÇͬÑù¿ÉÒÔ´ïµ½ÒÔÉϵÄÄ¿µÄ¡£
3. ValidateHandler½Ó¿ÚÔÊÐíÒ»¸öУÑéÓõÄAnnotation½«¾ßÌåµÄУÑ鹦ÄÜÒÑ´úÀíµÄ·½Ê½ÈÃÆäËüµÄÀàÀ´Íê³É¡£
ÎÒÃÇ¿ÉÒÔʹÓÃÈçϵķ½Ê½À´´¦ÀíУÑéµÄ¾ßÌå¹ý³Ì£º
˵Ã÷£ºÔÚJDK1.5ÖÐMethodʵÏÖÁËAnnotatedElement½Ó¿Ú£¬ÎÒÃÇ¿ÉÒÔʹÓÃAnnotatedElementÀ´×ö´¦Àí²Ù×÷
// ¶ÔÒ»¸ö·½·¨ºÍ½«Òªµ÷ÓõIJÎÊýÖµ½øÐÐУÑé
public static void validate(AnnotatedElement element, Object value) {
Validate v;
ValidateHandler vh;
Annotation a;
// ´Ó¸Ã·½·¨ÉÏ·µ»ØËùÓеÄAnnotation
Annotation[] annotations = element.getAnnotations();
for (int i = 0; i < annotations.length; i++) {
// Èç¹û¸ÃAnnotationÓÐÀàÐÍΪValidateµÄAnnotation£¬Ôò˵Ã÷ÕâÊÇÎÒÃÇУÑé
// ¿ò¼ÜËùʹÓõÄAnnotation¡£
v = annotations[i].annotationType().getAnnotation(Validate.class);
if (v != null) {
try {
// ʹÓÃAnnotationÖж¨ÒåµÄValidateHandlerÀàÀ´Éú³ÉValidateHandlerµÄʵÀý
vh = v.value().newInstance();
// ʹÓô´½¨µÄValidateHandlerÀ´×öУÑé²Ù×÷¡£
// УÑé¹ý³ÌÖпÉÒÔÅ׳öValidationException
vh.validate(annotations[i], value);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
ÍƼöÐÅÏ¢
- ÔÝÎ޼Ǽ
ÈÈÃÅÐÅÏ¢
- 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Ò³ÃæµÄÎåÖÖÌøת·½·¨
ÆÀÂÛ