After tiger was out of the cage, annotation became a small highlight. Despite the suspicion of plagiarism. net, it makes Java developers much more convenient. Ejb3.0 and hibernate annotation are based on this.
The following is an automatic log/authentication/internationalization application combined with spring:
Public Interface Sessionservice extends Service {
@ Anonymous // Allow calls by Unlogged users
@ Operationlog // Automatic Log
@ Name (zh = " Login " )
Public String login (
@ Name (EN = " User " , ZH = " User " ) String user,
@ Operationlog ( False ) String password ); // No log Password
@ Operationlog
@ Name (zh = " Cancel " )
Public Void Logout ();
@ Anonymous is used for authentication and anonymous access is allowed. The threadlocal variable is used to save the session information of the current user.
@ Operationlog is used to mark whether automatic log is enabled. It can act on classes, methods, and parameters.
@ Name is used for i18n internationalization. The common international solution in Java is the. properties file. I personally think this solution is not good and is suitable for large-scale project development. This is sufficient for small projects. If you want to add a language, you only need to add one more parameter in @ name. Using eclipse reference, you can easily know where to translate.
Similarly, @ name can be added to Bean to automatically construct a table/list in multiple languages, which is very convenient.@ Name (zh = " Security Events " , En = " Security Event " )
Public Class Securityevent extends actemsevent {
String cause;
@ Name (zh = " Cause " )
Public String getcause () {
ReturnCause;
}
}
Attach my i18nutil tool class: /**/ /**
* @ Author steeven
*/
Public Class I18nutil {
Public Static String getname (method) {
ReturnGeti18n (method, name.Class);
}
Public Static String gettip (method) {
ReturnGeti18n (method, tip.Class);
}
Public Static String geti18n (method, Class <? Extends Annotation > I18nclass) {
Annotation i18n=Method. getannotation (i18nclass );
ReturnGetproperty (i18n, method. getname ());
}
Public Static String getproperty (annotation i18n, string defaultvalue) {
If (I18n = Null )
Return Defaultvalue;
Class <? Extends Annotation > Clz = I18n. annotationtype ();
Try {
Method = Clz. getmethod (geti18nmethodname ());
Assert Method ! = Null ;
String R = (String) method. Invoke (i18n );
Return R = Null | R. Length () = 0 ? Defaultvalue: R;
} Catch (Exception E) {
AssertFalse;
ReturnDefaultvalue;
}
}
Private Static String geti18nmethodname () {
ReturnLocale. getdefault (). getlanguage ();
}
@ Suppresswarnings ( " Unchecked " )
Public Static < T extends Annotation > T getannotation (annotation [] argannotations, Class < T > Clz) {
For(Annotation Anno: argannotations)
If(Clz. isinstance (Anno ))
Return(T) Anno;
Return Null;
}
}
The metadata structure seems a little simple. Sometimes you need to add a lot of @ name, @ tip, and @ help. It seems that there is no way to deal with a tag.
In addition, you can add verification and Input Interface Description through annotation.ProgramIt is easy to automate, and a lot of images are useless.Code.
Jdk5.0 provides such an easy-to-use original data mechanism. What are your best practices?