@interface Annotation{ } 定义一个注解 @Annotation,比如我定义如下:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface LogAnnotation{
//模块名
String moduleName();
//日志类型
int logType();
//操作具体内容
String content();
}
其中:
用@Retention(RetentionPolicy.RUNTIME )修饰的注解,表示注解的信息被保留在class文件中,当程序编译时,会被虚拟机保留在运行时,
所以他们可以用反射的方式读取出来。
@Target({ElementType.METHOD})
表示这是一个方法的注解
例如
public class LogConstant {
public final static String MODULE_AA = "t_AA";
public final static int LOG_OPERATE = 1;
}
@LogAnnotation(moduleName=LogConstant.MODULE_DEPT, logType=LogConstant.LOG_OPERATE, content="添加部门权限")
public String add(String record) {
return "添加日志成功" ;
}