java利用@interface定义元注解及使用实例
来源:互联网 发布:唱歌录音软件 编辑:程序博客网 时间:2024/06/10 21:54
项目中遇到一个日志记录功能,记录登录系统用户的行为日志(对订单进行验单,发货,退款等操作)。使用了拦截器及注解的形式,来完成日志记录。
参考网络资源,写一个简单的demo,对java元注解知识进行一个梳理
demo资源目录结构如下:
1、利用@interface定义日志记录注解
package com.sunny.core.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Created by Administrator on 2017/3/16. */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface LogRecord { public String id(); public String description() default "no description";}
2、新建密码工具类,使用日志记录注解
package com.sunny.core.annotation;
import org.apache.poi.ss.formula.functions.T;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
*
* Created by Administrator on 2017/3/16.
*/
public class PasswordUtil {
/** * 密码校验 * @param password * @return */@LogRecord(id="47",description = "Passwords must contain at least one numeric")public boolean validatePassword(String password){ return (password.matches("\\w*\\d\\w*"));}/** * 对字符串序列进行反转加密 * @param password * @return */@LogRecord(id="48")public String encryptPassword(String password){ return new StringBuilder(password).reverse().toString();}
}
3、新建测试类
package com.sunny.core.annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Administrator on 2017/3/16.
*/
public class PasswordUtilTest {
public static void main(String[] args) { List<Integer> logIds=new ArrayList<Integer>(); Collections.addAll(logIds, 47, 48, 49, 50); trackLogRecord(logIds,PasswordUtil.class);}/** * 追踪日志记录 */public static void trackLogRecord(List<Integer> logIds,Class<?> cl){ for (Method method:cl.getDeclaredMethods()) { LogRecord logRecord = method.getAnnotation(LogRecord.class); if(logRecord!=null){ System.out.println("Found LogRecord:"+logRecord.id()+" "+logRecord.description()); logIds.remove(new Integer(logRecord.id())); } } for(int logId:logIds){ System.out.println("Warning:Missing LogRecord ,Id:"+logId); }}
}
4、运行测试类,输出测试情况
参考资料:http://www.cnblogs.com/pepcod/archive/2013/02/16/2913474.html
- java利用@interface定义元注解及使用实例
- Interface定义及使用
- java 注解 @Retention @interface 元数据
- java 注解 @Retention @interface 元数据
- 【interface】C#interface定义及使用浅析
- 元注解和使用实例
- Java注解的使用@interface:
- JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited)。
- java元注解及源码浅析
- java入门学习(11)—接口定义(interface)及使用(implements)
- java元注解详解及自定义注解的方法
- 使用@interface 自定义注解
- @interface java注解
- @interface 注解 java
- Java 自定义注解@interface
- @interface java注解
- Java注解@interface (入门)
- @interface java注解
- 后台框架,走起
- Controller及其方法的注解
- HOG特征提取(一)
- Linux rpm 命令参数使用详解[介绍和应用]
- Ubuntu修复引导
- java利用@interface定义元注解及使用实例
- Cannot find the declaration of element 'beans'
- Python 标准库 urllib2 的使用细节
- 真机测试中设备的UDID加满了如何给新设备打包(iPhone上限100台,以xcode7.3为例)
- C/C++编程语言书籍
- centos\linux 下十分钟内 用vim 搭建c++ IDE,以centos 7.0为例
- Easy Problem V
- Spring-MVC开发之全局异常捕获全面解读
- 关于使用adb 打logcat