目录
一、JAVA注解简介
1.1java注解的定义
Java注解是附加在代码中的一些元信息,用于一些工具在编译、
运行时进行解析和使用,起到说明、配置的功能。注解相关类都包含在java.lang.annotation包中。
2.2java注解的分类
2.1 JDK基本注解
2.2.1.1 @Override
重写
2.2.1.2 @Deprecated
已过时
2.2.1.3 @SuppressWarnings(value = "unchecked")
压制编辑器警告
2.2 JDK元注解
2.2 .1 @Retention:定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到2.2 .2 @Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)
@Target(ElementType.TYPE) //接口、类
@Target(ElementType.FIELD) //属性
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE) //局部变量
@Target(ElementType.ANNOTATION_TYPE) //注解
@Target(ElementType.PACKAGE) //包
注:可以指定多个位置,例如:
@Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用2.2 .3 @Inherited:指定被修饰的Annotation将具有继承性
2.2.4@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.
2.3 自定义注解
接来下就是小编来详细来讲述自定义注解的组成以及写一个自定义注解和使用
3、注解分类
根据Annotation(注解)是否包含成员变量,可以把Annotation分为两类:
3.1标记Annotation:
没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息
3.2 元数据Annotation:
包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;
二、自定义注解
2.1自定义注解的方法:
使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型, 而且我们还可以使用default关键字为这个成员变量设定默认值
例如创建一个MyAnnotation1将其定义为可用在类、方法以及参数上:
package com.zq.ssm.annotation;
/**
* @author张强
* @site www.zq.com
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//@interface 是修饰注解类
//ElementType.TYPE 指注解可以用在类上面
//ElementType.METHOD 指注解可以用在属性上面
//ElementType.FIELD 指用在方法上面
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
public @interface MyAnnotation1 {
}
@interface 是修饰注解类
@Target(决定自定义注解用在哪)
//ElementType.TYPE 指注解可以用在类上面
//ElementType.METHOD 指注解可以用在属性上面
//ElementType.FIELD 指用在方法上面@Retention(定义注解的保留策略)
//RetentionPolicy.SOURCE注解仅存在于源码中,在class字节码文件中不包含
//RetentionPolicy.CLASS默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
//RetentionPolicy.RUNTIME注解会在class字节码文件中存在,在运行时可以通过反射获取到
在注解类中定义的属性方法,都称为注解类的属性,我们接下来使用我们自定义的注解进行属性的使用,其中注解的属性为value时是可以省略的一般都会给一个default默认值
MyAnnotation1:
package com.zq.ssm.annotation; /** * @author张强 * @site www.zq.com */ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //@interface 是修饰注解类 //ElementType.TYPE 指注解可以用在类上面 //ElementType.METHOD 指注解可以用在属性上面 //ElementType.FIELD 指用在方法上面 @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) public @interface MyAnnotation1 { //指的是注解的属性 public String value() default "value可以修饰类、方法、属性"; //指的是注解的属性 public String desc() default "desc可以修饰类、方法、属性"; }
MyAnnotation2:
package com.zq.ssm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author张强 * @site www.zq.com */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation2 { // 指的是注解的属性 public String desc() default "desc可以修饰类、属性、方法"; public String value() default "value可以修饰类、属性、方法"; }
测试类:
package com.zq.ssm.controller; import com.zq.ssm.annotation.MyAnnotation1; import com.zq.ssm.annotation.MyAnnotation2; /** * @author张强 * @site www.zq.com * @create 2022-11-02 10:09 */ @MyAnnotation1(desc="在类上面标记") public class TestController { @MyAnnotation1("标记在属性id上面") private String id; @MyAnnotation1("标记在属性name上面") private String name; @MyAnnotation1 public void text(@MyAnnotation2("用来修饰参数id") String id, @MyAnnotation2("用来修饰参数name") String name){ System.out.println("测试。。。。。。。"); } }
Demo1我们用来获取类注释上的类属性:
package com.zq.ssm;
import com.zq.ssm.annotation.MyAnnotation1;
import com.zq.ssm.controller.TestController;
/**
* @author张强
* @site www.zq.com
*/
public class Demo1 {
public static void main(String[] args) {
// 获得类上自定义注释中的内容
MyAnnotation1 annotation = TestController.class.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());
System.out.println(annotation.desc());
}
}
点击运行时我们会发现错误:
这时我们需要在MyAnnotation1中修改
@Retention(RetentionPolicy.SOURCE)为@Retention(RetentionPolicy.RUNTIME)
结果我们就可以发现我们想要拿到的类属性效果:
Demo2我们用来展示获取方法上的自定义注解的类属性:
package com.zq.ssm;
import com.zq.ssm.annotation.MyAnnotation1;
import com.zq.ssm.controller.TestController;
import java.lang.reflect.Method;
/**
* @author张强
* @site www.zq.com
*/
public class Demo2 {
public static void main(String[] args) throws Exception {
// 获得方法上自定义注释中的内容
Method test1 = TestController.class.getDeclaredMethod("text", String.class, String.class);
MyAnnotation1 m1 = test1.getAnnotation(MyAnnotation1.class);
System.out.println(m1.value());
System.out.println(m1.desc());
}
}
运行结果因为我们在测试类中的方法上面是没有修改类属性的,所以这里就会展示默认值:
Demo3我们用来展示获得获得属性上自定义注释中的内容:
package com.zq.ssm;
import com.zq.ssm.annotation.MyAnnotation1;
import com.zq.ssm.controller.TestController;
import java.lang.reflect.Field;
/**
* @author张强
* @site www.zq.com
*/
public class Demo3 {
public static void main(String[] args) throws Exception {
// 获得属性上自定义注释中的内容
Field id = TestController.class.getDeclaredField("id");
Field name = TestController.class.getDeclaredField("name");
System.out.println(id.getAnnotation(MyAnnotation1.class).value());
System.out.println(id.getAnnotation(MyAnnotation1.class).desc());
System.out.println(name.getAnnotation(MyAnnotation1.class).value());
System.out.println(name.getAnnotation(MyAnnotation1.class).desc());
//一次性遍历所有的属性
// Field[] declaredFields = TestController.class.getDeclaredFields();
// for (Field f : declaredFields) {
// MyAnnotation1 annotation1 = f.getAnnotation(MyAnnotation1.class);
// if(annotation1 != null){
// System.out.println(annotation1.value());//false
// }
// }
}
}
运行结果:
Demo4我们用来获得方法参数上的注释内容:
package com.zq.ssm;
import com.zq.ssm.annotation.MyAnnotation2;
import com.zq.ssm.controller.TestController;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
/**
* @author张强
* @site www.zq.com
* @create 2022-11-02 11:31
*/
public class Demo4 {
public static void main(String[] args) throws Exception {
Method test1 = TestController.class.getDeclaredMethod("text", String.class, String.class);
//获得参数上自定义注释中的内容
for (Parameter p : test1.getParameters()) {
System.out.println(p.getAnnotation(MyAnnotation2.class).value());
}
}
}
运行结果:
三、Aop自定义注解的应用之日志
我们学习了自定义注解可以用在一些切面中,比如我们写一个东西只用到了这个注解才触发切面,和我们写一个东西之前不一样,我们之前的切面定义只能是在哪一个层中用了就会触发切面。
比如我们定义一个注解MyLog作用于方法上:
package com.zq.ssm.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author张强
* @site www.zq.com
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
String value();
}
再来一个方法使用这个注释:
package com.zq.ssm.controller;
import com.zq.ssm.annotation.MyLog;
import org.springframework.stereotype.Controller;
/**
* @author张强
* @site www.zq.com
*/
@Controller
public class DemoController {
@MyLog("这是结合spring aop知识,讲解自定义注解应用的一个案例")
public void test(){
System.out.println("这里随便来点啥");
}
}
再来一个我们测试所需要的切面AOP:
package com.zq.ssm.aspect;
import com.zq.ssm.annotation.MyLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author 张强
* @site www.zq.com
*/
@Component
@Aspect
public class MyLogAspect {
private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
/**
* 只要用到了com.zq.ssm.annotation.MyLog这个注解的,就是目标类
*/
@Pointcut("@annotation(com.zq.ssm.annotation.MyLog)")
private void MyValid() {
}
@Before("MyValid()")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
logger.debug("[" + signature.getName() + " : start.....]");
System.out.println("[" + signature.getName() + " : start.....]");
MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);
logger.debug("【目标对象方法被调用时候产生的日志,记录到日志表中】:"+myLog.value());
System.out.println("【目标对象方法被调用时候产生的日志,记录到日志表中】:" + myLog.value());
}
}