[size=small]play2框架上实现自己的注解很容易,只要两步[/size]
[size=small]一、定义annotation类:
[/size]
[size=small]二、定义Action类[/size]
[size=small]最后,使用的话,可以在方法或者类上面使用注解@MyLog,或者@MyLog(msg="ooxxoo")。
利用注解,可以用来很容易实现细粒度的权限的过滤,统一登录验证,还有filter的功能等
[/size]
[size=small]一、定义annotation类:
[/size]
package actions;
import play.mvc.With;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@With(MyLogAction.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Inherited
@Documented
public @interface MyLog{
String msg() default "hello world";
}
[size=small]二、定义Action类[/size]
package actions;
import play.libs.F;
import play.mvc.Http.Context;
import play.mvc.Action;
import play.mvc.SimpleResult;
public class MyLogAction extends Action<MyLog>{
@Override
public F.Promise<SimpleResult> call(Context context) throws Throwable{
System.out.println("logger: " + configuration.msg());
return delegate.call(context);
}
}
[size=small]最后,使用的话,可以在方法或者类上面使用注解@MyLog,或者@MyLog(msg="ooxxoo")。
利用注解,可以用来很容易实现细粒度的权限的过滤,统一登录验证,还有filter的功能等
[/size]