做用在代码上的功能注解(部分):java
注解名称 | 功能描述 |
---|---|
@Override | 检查该方法是不是重写方法。若是发现其父类,或者是引用的接口中并无该方法时,会报编译错误 |
@Deprecated | 标记过期方法。若是使用该方法,会报编译警告 |
@SuppressWarnings | 指示编译器去忽略注释解中声明的警告 |
@FunctionalInterface | java8支持,标识一个匿名函数或函数式接口 |
让给程序员开发自定义注解的元注解(和关键字@interface配合使用的注解)python
元注解名称 | 功能描述 |
---|---|
@Retention | 标识这个注释解怎么保存,是只在代码中,仍是编入类文件中,或者是在运行时能够经过反射访问 |
@Documented | 标识这些注解是否包含在用户文档中 |
@Target | 标识这个注解的做用范围 |
@Inherited | 标识注解可被继承类获取 |
@Repeatable | 标识某注解能够在同一个声明上使用屡次 |
package java.lang.annotation; public interface Annotation { boolean equals(Object obj); int hashCode(); String toString(); // 返回定义的注解类型,你在代码声明的@XXX,至关于该类型的一实例 Class<? extends Annotation> annotationType(); } -----自定义示例----- @Retention( value = RetentionPolicy.RUNTIME) @Target(value = ElementType.TYPE) public @interface ATest { String hello() default "siting"; }
ATest的字节码文件,编译器让自定义注解实现了Annotation接口程序员
public abstract @interface com/ATest implements java/lang/annotation/Annotation { // compiled from: ATest.java @Ljava/lang/annotation/Retention;(value=Ljava/lang/annotation/RetentionPolicy;.RUNTIME) @Ljava/lang/annotation/Target;(value={Ljava/lang/annotation/ElementType;.TYPE}) // access flags 0x401 public abstract hello()Ljava/lang/String; default="siting" }
System.setProperty("sum.misc.ProxyGenerator.saveGeneratedFiles","true");
可生成注解相应的代理类public enum RetentionPolicy { /** 注解将被编译器丢弃,生成的class不包含注解信息 */ SOURCE, /** 注解在class文件中可用,但会被JVM丢弃;当注解未定义Retention值时,默认值是CLASS */ CLASS, /** 注解信息在运行期(JVM)保留(.class也有),能够经过反射机制读取注解的信息, * 操做方法看AnnotatedElement(全部被注释类的父类) */ RUNTIME }
public enum ElementType { /** 适用范围:类、接口、注解类型,枚举类型enum */ TYPE, /** 做用于类属性 (includes enum constants) */ FIELD, /** 做用于方法 */ METHOD, /** 做用于参数声明 */ PARAMETER, /** 做用于构造函数声明 */ CONSTRUCTOR, /** 做用于局部变量声明 */ LOCAL_VARIABLE, /** 做用于注解声明 */ ;, /** 做用于包声明 */ PACKAGE, /** 做用于类型参数(泛型参数)声明 */ TYPE_PARAMETER, /** 做用于使用类型的任意语句(不包括class) */ TYPE_USE }
TYPE_PARAMETER的用法示例web
class D<@PTest T> { } // 注解@PTest做用于泛型T
TYPE_USE的用法示例spring
//用于父类或者接口 class Test implements @Parent TestP {} //用于构造函数 new @Test String("/usr/data") //用于强制转换和instanceof检查,注意这些注解中用于外部工具 //它们不会对类型转换或者instanceof的检查行为带来任何影响 String path=(@Test String)input; if(input instanceof @Test String) //注解不会影响 //用于指定异常 public Person read() throws @Test IOException. //用于通配符绑定 List<@Test ? extends Data> List<? extends @Test Data> @Test String.class //非法,不能标注class
@Inherited @Retention( value = RetentionPolicy.RUNTIME) @Target(value = ElementType.TYPE) public @interface ATest { } ----被ATest注解的父类PTest---- @ATest public class PTest{ } ---Main是PTest的子类---- public class Main extends PTest { public static void main(String[] args){ Annotation an = Main.class.getAnnotations()[0]; //Main能够拿到父类的注解ATest,由于ATest被元注解@Inherited修饰 System.out.println(an); } } ---result-- @com.ATest()
//Java8前没法重复使用注解 @FilterPath("/test/v2") @FilterPath("/test/v1") public class Test {}
--- 做用于注解的注解---- @Inherited @Retention( value = RetentionPolicy.RUNTIME) @Target(value = {ElementType.ANNOTATION_TYPE}) public @interface AnnotationTest { String value() default "AnnotationTest"; } ------父类------- public class PTest {} ------被注解修饰的package-info.java------ //package-info.java @AnTest("com-package-info") package com; ------------- @AnnotationTest("AnnotationTest") @Inherited @Retention( value = RetentionPolicy.RUNTIME) @Target(value = {ElementType.TYPE_USE,ElementType.PACKAGE,ElementType.FIELD, ElementType.TYPE_PARAMETER,ElementType.CONSTRUCTOR,ElementType.LOCAL_VARIABLE}) public @interface AnTest { String value() default "siting"; }
运行示例编程
//注解类 @AnTest("mainClass") //注解泛型参数 //注解继承父类 public class Main<@AnTest("parameter") T > extends @AnTest("parent") PTest { @AnTest("constructor") //注解构造函数 Main(){ } //注解字段域 private @AnTest("name") String name; //注解泛型字段域 private @AnTest("value") T value; //注解通配符 private @AnTest("list")List<@AnTest("generic") ?>list; //注解方法 @AnTest("method") //注解方法参数 public String hello(@AnTest("methodParameter") String name) throws @AnTest("Exception") Exception { // 注解抛出异常 //注解局部变量,如今运行时暂时没法获取(忽略) @AnTest("result") String result; result = "siting"; System.out.println(name); return result; } public static void main(String[] args) throws Exception { Main<String> main = new Main<> (); Class<Main<Object>> clazz = (Class<Main<Object>>) main.getClass(); //class的注解 Annotation[] annotations = clazz.getAnnotations(); AnTest testTmp = (AnTest) annotations[0]; System.out.println("修饰Main.class注解value: "+testTmp.value()); //构造器的注解 Constructor<Main<Object>> constructor = (Constructor<Main<Object>>) clazz.getDeclaredConstructors()[0]; testTmp = constructor.getAnnotation(AnTest.class); System.out.println("修饰构造器的注解value: "+testTmp.value()); //继承父类的注解 AnnotatedType annotatedType = clazz.getAnnotatedSuperclass(); testTmp = annotatedType.getAnnotation(AnTest.class); System.out.println("修饰继承父类的注解value: "+testTmp.value()); //注解的注解 AnnotationTest annotationTest = testTmp.annotationType().getAnnotation(AnnotationTest.class); System.out.println("修饰注解的注解AnnotationTest-value: "+annotationTest.value()); //泛型参数 T 的注解 TypeVariable<Class<Main<Object>>> variable = clazz.getTypeParameters()[0]; testTmp = variable.getAnnotation(AnTest.class); System.out.println("修饰泛型参数T注解value: "+testTmp.value()); //普通字段域 的注解 Field[] fields = clazz.getDeclaredFields(); Field nameField = fields[0]; testTmp = nameField.getAnnotation(AnTest.class); System.out.println("修饰普通字段域name注解value: "+testTmp.value()); //泛型字段域 的注解 Field valueField = fields[1]; testTmp = valueField.getAnnotation(AnTest.class); System.out.println("修饰泛型字段T注解value: "+testTmp.value()); //通配符字段域 的注解 Field listField = fields[2]; AnnotatedParameterizedType annotatedPType = (AnnotatedParameterizedType)listField.getAnnotatedType(); testTmp = annotatedPType.getAnnotation(AnTest.class); System.out.println("修饰泛型注解value: "+testTmp.value()); //通配符注解 的注解 AnnotatedType[] annotatedTypes = annotatedPType.getAnnotatedActualTypeArguments(); AnnotatedWildcardType annotatedWildcardType = (AnnotatedWildcardType) annotatedTypes[0]; testTmp = annotatedWildcardType.getAnnotation(AnTest.class); System.out.println("修饰通配符注解value: "+testTmp.value()); //方法的注解 Method method = clazz.getDeclaredMethod("hello", String.class); annotatedType = method.getAnnotatedReturnType(); testTmp = annotatedType.getAnnotation(AnTest.class); System.out.println("修饰方法的注解value: "+testTmp.value()); //异常的注解 annotatedTypes = method.getAnnotatedExceptionTypes(); testTmp = annotatedTypes[0].getAnnotation(AnTest.class); System.out.println("修饰方法抛出错误的注解value: "+testTmp.value()); //方法参数的注解 annotatedTypes = method.getAnnotatedParameterTypes(); testTmp = annotatedTypes[0].getAnnotation(AnTest.class); System.out.println("修饰方法参数注解value: "+testTmp.value()); //包的注解 Package p = Package.getPackage("com"); testTmp = p.getAnnotation(AnTest.class); System.out.println("修饰package注解value: "+testTmp.value()); main.hello("hello"); } }
结果app
修饰Main.class注解value: mainClass 修饰构造器的注解value: constructor 修饰继承父类的注解value: parent 修饰注解的注解AnnotationTest-value: AnnotationTest 修饰泛型参数T注解value: parameter 修饰普通字段域name注解value: name 修饰泛型字段T注解value: value 修饰泛型注解value: list 修饰通配符注解value: generic 修饰方法的注解value: method 修饰方法抛出错误的注解value: Exception 修饰方法参数注解value: methodParameter 修饰package注解value: com-package-info hello
spring.AOP至关于动态代理和注解机制在spring框架的结合实现框架
概念 | 描述 |
---|---|
通知(Advice) | 须要切入的增长代码逻辑被称为通知 |
切点(Pointcut) | 定义加强代码在何处执行 |
切面(Aspect) | 切面是通知和切点的集合 |
链接点(JoinPoint) | 在切点基础上,指定加强代码在切点执行的时机(在切点前,切点后,抛出异常后等) |
目标(target) | 被加强目标类 |
切面编程相关注解 | 功能描述 |
---|---|
@Aspect | 做用于类,声明当前方法类是加强代码的切面类 |
@Pointcut | 做用于方法,指定须要被拦截的其余方法。当前方法则做为拦截集合名使用 |
spring通知(Advice)注解 | 功能描述 |
---|---|
@After | 加强代码在@Pointcut指定的方法以后执行 |
@Before | 加强代码在@Pointcut指定的方法以前执行 |
@AfterReturning | 加强代码在@Pointcut指定的方法 return返回以后执行 |
@Around | 加强代码能够在被拦截方法先后执行 |
@AfterThrowing | 加强代码在@Pointcut指定的方法抛出异常以后执行 |
新建spring-web + aop 项目;新建以下class ------ 目标Controller ------ @RestController public class TestController { @STAnnotation @RequestMapping("/hello") public String hello(){ return "hello@csc"; } } ------ Controller注解 ------- @Retention( value = RetentionPolicy.RUNTIME) @Target(value = ElementType.METHOD) public @interface STAnnotation { String value() default "注解hello!"; } ------ Controller切面 ------ @Aspect @Component public class ControllerAspect { //切点:注解指定关联 (对注解进行切面) @Pointcut("@annotation(STAnnotation)") public void controllerX(){} //切点:路径指定关联 @Pointcut("execution(public * com.example.demo.TestController.*(..))") public void controllerY(){} //在controllerY()切点执行以前的链接点加入通知 @Before("controllerY()") public void yBefore(JoinPoint joinPoint) throws Throwable { //能够加入加强代码 MethodSignature methodS = (MethodSignature)joinPoint.getSignature(); Method method = methodS.getMethod(); if (method.isAnnotationPresent(STAnnotation.class)) { STAnnotation annotation = method.getAnnotation(STAnnotation.class); System.out.println(annotation.value()); } System.out.println("controllerY"); } //controllerX()切点执行以后的链接点加入通知 @After("controllerX()") public void xBefore(JoinPoint joinPoint) throws Throwable { //能够加入加强代码 System.out.println("controllerX"); } }
启动项目;执行curl http://127.0.0.1:8080/hello
,控制台输出以下
curl
@FunctionalInterface public interface Func { void hello(String name); } --------------------- public static void main(String[] args) { Func func = (name) -> System.out.println(name); func.hello("siting"); }
查看对应的Main.class字节码文件 javap.exe -p -v -c Main.class
jvm
Constant pool: #1 = Methodref #8.#28 // java/lang/Object."<init>":()V //常量值中前面的#0表示引导方法取BootstrapMethods属性表的第0项(字节码在最下面) #2 = InvokeDynamic #0:#33 // #0:hello:()Lcom/Func; #3 = String #34 // siting #4 = InterfaceMethodref #35.#36 // com/Func.hello:(Ljava/lang/String;)V #5 = Fieldref #37.#38 // java/lang/System.out:Ljava/io/PrintStream; #6 = Methodref #39.#40 // java/io/PrintStream.println:(Ljava/lang/String;)V #7 = Class #41 // com/Main .... // main执行字节码 public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=1 // 动态得到一个CallSite对象,该对象是一个内部类,实现了Func接口 0: invokedynamic #2, 0 // InvokeDynamic #0:hello:()Lcom/Func; 5: astore_1 6: aload_1 7: ldc #3 // String siting // 调用CallSite对象的hello方法 9: invokeinterface #4, 2 // InterfaceMethod com/Func.hello:(Ljava/lang/String;)V 14: return .... //lambda表达式 会编译出私有静态类 private static void lambda$main$0(java.lang.String); descriptor: (Ljava/lang/String;)V flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC Code: stack=2, locals=1, args_size=1 0: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 3: aload_0 4: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 7: return .... //lambda表达式 会编译出一个对应的内部类 SourceFile: "Main.java" InnerClasses: public static final #59= #58 of #62; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles BootstrapMethods: 0: #30 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lan g/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; Method arguments: #31 (Ljava/lang/String;)V //调用Main方法里的lambda$main$0静态方法(真正执行lambda的逻辑的方法) #32 invokestatic com/Main.lambda$main$0:(Ljava/lang/String;)V #31 (Ljava/lang/String;)V
从上面的字节码可看出,1:lambda表达式会被编译成一个私有静态方法和一个内部类;2:内部类实现了函数式接口,而实现方法会调用一个Main.class里一静态方法 3:静态方法lambda$main$0里是咱们本身写的代码逻辑。运行参数加上-Djdk.internal.lambda.dumpProxyClasses
能够查看lambda对应内部类的具体信息
接口 | 描述 |
---|---|
Predicate | 判断:传入一个参数,返回一个bool结果, 方法为boolean test(T t) |
Consumer | 消费:传入一个参数,无返回值, 方法为void accept(T t) |
Function | 转化处理:传入一个参数,返回一个结果,方法为R apply(T t) |
Supplier | 生产:无参数传入,返回一个结果,方法为T get() |
BiFunction | 转化处理:传入两个个参数,返回一个结果,方法R apply(T t, U u) |
BinaryOperator | 二元操做符, 传入的两个参数的类型和返回类型相同, 继承 BiFunction |