自定义注解
1. JDK常见注解
@Override:重写了接口方法
@Deprecated:方法过时了,不建议使用
@SuppressWarnings("deprecation"):关闭出现的警告
下面列出@SuppressWarnings注解参数的几个常见用法
1.deprecation:表示不检查过时的方法
2.serial:关闭可序列化的类上缺少 serialVersionUID 定义时的警告
3.finally:任何 finally 子句不能正常完成时的警告
4.rawtypes:去除传参数时也要带泛型
5.unchecked:执行了未检查的转换时的警告,例如当使用集合时没有用泛型来指定集合保存的类型
6.unused:去除对未使用代码的警告
7:all:去除所有类型的警告
个人习惯去掉项目中的编译警告
2. 元注解
1.@Target:说明了Annotation可修饰的范围
例:@Target(ElementType.TYPE) 多个值以,隔开
1.ElementType.CONSTRUCTOR:用于描述构造器 2.ElementType.FIELD:用于描述域(类的成员变量) 3.ElementType.LOCAL_VARIABLE:用于描述局部变量(方法内部变量) 4.ElementType.METHOD:用于描述方法 5.ElementType.PACKAGE:用于描述包 6.ElementType.PARAMETER:用于描述参数 7.ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明
2.@Retention:定义了该Annotation被保留的时间长短,注解的生命周期
例:@Retention(RetentionPolicy.RUNTIME)
1.RetentionPoicy.SOURCE:在源文件中有效 2.RetentionPoicy.CLASS:在class文件中有效 3.RetentionPoicy.RUNTIME:在运行时有效
3.@Documented:没有成员的注解(标记注解),用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化
4.@Inherited:标记注解,被它标注的类型是可被继承的,比如父class被@Inherited标记,那么一个子类继承父class后,则这个annotation也将被用于子class。
3. 自定义注解的定义
格式:
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnno
{
}
定义注解使用@interface
其中@Target({ ElementType.METHOD })说明此注解用在方法上,
@Retention(RetentionPolicy.RUNTIME)说明注解在运行时有效,
@Documented说明此注解信息会在生成的文档中
至此,自定义注解 @MyAnno 已经创建完成,此时为一个标记注解,没有内部成员
注解成员:
注解成员的权限修饰符只能用publi或者默认的default来修饰,
例如:
public boolean flag() default false;
String limit() default "4";
此时,注解的成员flag的默认值为false,limit的默认值为"4"。
使用时赋值:
@MyAnno(flag = true, limit = "100")
4. 通过反射的AnnotatedElement获取注解信息
此部分后续补充,此处不作重点
5. 使用Spring AOP增强注解
AOP是Spring框架中的一个重要内容。面向切面编程。将一部分行为从主要业务逻辑中分离出来,常见应用:日志,事务,消息等。
@Aspect:作用是把当前类标识为一个切面类
常用切面通知类型:
@Around:环绕增强
@AfterReturning:后置增强
@Before:前置增强
@AfterThrowing:异常抛出增强
@After: final增强,不管是抛出异常或者正常退出都会执行
@Component将类纳入Spring容器
接下来是一个demo来介绍切面类可以做什么
@Order(3)// 若一个方法上有多个注解,@Order可调整优先级,数字越小优先级越高
@Aspect
@Component
public class MyAnnoAspect
{
@Around("@annotation(com.common.annotation.MyAnno)") // 此处使用了环绕通知
public Object method(ProceedingJoinPoint joinPoint) throws Throwable
{
// joinPoint 连接点
try
{
MyAnno myAnno = getAnnotationLog(joinPoint);// 获取注解
// 获取注解成员
boolean flag = myAnno.flag();
String limit = myAnno.limit();
// 增强的逻辑
// Tip:可根据反射和注解的API获取注解所在方法的参数
// 常见的增强操作:日志处理,数据缓存、接口限流...
return joinPoint.proceed();// 放行
}
catch (Exception e)
{
e.printStackTrace();// 最好用日志记录日常
}
return joinPoint.proceed();// 此处应根据业务,执行放行或返回错误信息
}
/**
* 是否存在注解,如果存在就获取
*/
private MyAnno getAnnotationLog(JoinPoint joinPoint) throws Exception
{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
return method.getAnnotation(MyAnno.class);
}
return null;
}
}