首页 > 代码库 > Java的注解
Java的注解
1.什么是注解
语法: @注解名称
注解的作用:代替配置文件,如xml
servlet3.0中,就可以用写web.xml,可以使用注解来配置
注解是由框架来读取的,是给程序看的,而注释是给人看的
2.注解的使用
*定义注解:框架的工作
*使用注解:程序员的工作
*读取注解:框架的工作
3.定义注解类
class A{}//定义类
interface B{}//定义接口
enum C//定义枚举
@interface MyAnno1{} 就像所有的类都是Object的子类,那么所有的注解都是Annotation的子类
4.使用注解
注解的作用目标
*类(接口,枚举)
*方法
*参数
*局部变量
*构造器
*包
5.注解的属性
5.1定义属性的语法:类型 类型名称();
@interface MyAnno1{
int a();
String b();
}
5.2在使用注解时,需要给注解的属性赋值
@MyAnno1(a=100,b="hello")//给注解属性赋值
public class Demo2 {
}
5.3可以在定义注解属性时,给注解属性默认值
@interface MyAnno2{
int aa() default 100;//给注解属性默认值
String bb();
}
5.4名为value属性的特权
如果只给注解中名称为value的属性赋值,那么“value=http://www.mamicode.com/”可以省略
例如 @MyAnno3(100)等于 @MyAnno3(value=http://www.mamicode.com/100)
5.5注解的属性类型
*八种基本类型(Integer等包装类型不可以)
*Class类型
*枚举类型(enum)
*注解类型
*String类型
*以上任意一种类型的基本数组类型
/**
* 注解属性的使用
* @author ygh
* 2017年1月19日
*/
@MyAnno1(
a=100,
b="hello",
c = Demo1.class,
d=@MyAnno2(a=100,b="ygh"),
e = MyEnum.A,
f={"hello","world"}//如果数组的元素为1,那么{}可以省略,可以写成f="hello"
)
public class Demo3 {
}
@interface MyAnno1{
int a();
String b();
Class c();
MyAnno2 d();
MyEnum e();
String[] f();
}
@interface MyAnno2{
int a();
String b();
}
enum MyEnum{
A,B,C
}
6.注解的目标限定以及保存策略
6.1注解的目标限定:让一个注解只能作用在类上,不能作用在方法上,使用 @Target注解来限定
@MyAnno1
public class Demo4 {
// @MyAnno1//报错,因为限定注解只能作用在类和方法上
private int a;
@MyAnno1
public void fun1(){}
}
@Target(value = http://www.mamicode.com/{ ElementType.TYPE, ElementType.METHOD })
@interface MyAnno1 {
}
6.2注解的保存策略
*源代码文件(SOURCE):注解只在源码中,当编译时就自动忽略了
*字节码文件(CLASS):注解在源码中,当编译时会存在在class字节码文件中,但JVM加载类时,就好忽略
*JVM(RUNTIME):注解在源代码中,当编译时会存在在class字节码文件中,并且在JVM加载类时,会把注解加载到JVM内存中(它是唯一可反射注解!)
@Retention(RetentionPolicy.RUNTIME)//设置注解的保留策略
@interface MyAnno1 {
}
1 package cn.itcast.annotation.demo1; 2 3 @MyAnno1//注解可以放在类(接口和枚举)上 4 public class Demo1 { 5 6 @MyAnno1//注解可以放在构造器 7 public Demo1(){} 8 @MyAnno1//注解可以放在成员变量上面 9 private Integer a; 10 @MyAnno1//注解可以放在方法上 11 public void fun1(@MyAnno1 String str){//注解可以放在方法的形参上 12 @MyAnno1//注解可以放在局部变量上 13 int a[]; 14 15 // @MyAnno1 //不能在执行语句上,不让报错 16 System.out.println("hello"); 17 } 18 } 19 20 /** 21 * 定义注解,所有的注解都是 22 * @author ygh 23 * 2017年1月19日 24 */ 25 @interface MyAnno1{ 26 27 }
1 package cn.itcast.annotation.demo2; 2 3 @MyAnno1(a=100,b="hello")//给注解属性赋值 4 @MyAnno2(bb="hello")//有默认值的注解属性可以不赋值 5 @MyAnno3(100)//如果只给注解中名称为value的属性赋值,那么“value=http://www.mamicode.com/”可以省略 6 public class Demo2 { 7 8 } 9 10 /** 11 * 注解属性的定义 12 * @author ygh 13 * 2017年1月19日 14 */ 15 @interface MyAnno1{ 16 int a(); 17 String b(); 18 } 19 20 @interface MyAnno2{ 21 int aa() default 100;//给注解属性默认值 22 String bb(); 23 } 24 25 @interface MyAnno3{ 26 int value(); 27 String cc() default "ygh"; 28 }
1 /** 2 * 注解属性的使用 3 * @author ygh 4 * 2017年1月19日 5 */ 6 @MyAnno1( 7 a=100, 8 b="hello", 9 c = Demo1.class, 10 d=@MyAnno2(a=100,b="ygh"), 11 e = MyEnum.A, 12 f={"hello","world"}//如果数组的元素为1,那么{}可以省略,可以写成f="hello" 13 ) 14 public class Demo3 { 15 16 } 17 18 @interface MyAnno1{ 19 int a(); 20 String b(); 21 Class c(); 22 MyAnno2 d(); 23 MyEnum e(); 24 String[] f(); 25 } 26 27 @interface MyAnno2{ 28 int a(); 29 String b(); 30 } 31 32 enum MyEnum{ 33 A,B,C 34 }
1 package cn.itcast.annotation.demo4; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @MyAnno1 9 public class Demo4 { 10 11 12 // @MyAnno1//报错,因为限定注解只能作用在类和方法上 13 private int a; 14 @MyAnno1 15 public void fun1(){} 16 } 17 18 @Target(value =http://www.mamicode.com/ { ElementType.TYPE, ElementType.METHOD }) 19 @Retention(RetentionPolicy.RUNTIME)//设置注解的保留策略 20 @interface MyAnno1 { 21 }
@Target注解的源代码
1 /* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 */ 25 26 package java.lang.annotation; 27 28 /** 29 * Indicates the kinds of program element to which an annotation type 30 * is applicable. If a Target meta-annotation is not present on an 31 * annotation type declaration, the declared type may be used on any 32 * program element. If such a meta-annotation is present, the compiler 33 * will enforce the specified usage restriction. 34 * 35 * For example, this meta-annotation indicates that the declared type is 36 * itself a meta-annotation type. It can only be used on annotation type 37 * declarations: 38 * <pre> 39 * @Target(ElementType.ANNOTATION_TYPE) 40 * public @interface MetaAnnotationType { 41 * ... 42 * } 43 * </pre> 44 * This meta-annotation indicates that the declared type is intended solely 45 * for use as a member type in complex annotation type declarations. It 46 * cannot be used to annotate anything directly: 47 * <pre> 48 * @Target({}) 49 * public @interface MemberType { 50 * ... 51 * } 52 * </pre> 53 * It is a compile-time error for a single ElementType constant to 54 * appear more than once in a Target annotation. For example, the 55 * following meta-annotation is illegal: 56 * <pre> 57 * @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD}) 58 * public @interface Bogus { 59 * ... 60 * } 61 * </pre> 62 */ 63 @Documented 64 @Retention(RetentionPolicy.RUNTIME) 65 @Target(ElementType.ANNOTATION_TYPE) 66 public @interface Target { 67 ElementType[] value(); 68 }
@Target中ElementType的源代码
1 /* 2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 */ 25 26 package java.lang.annotation; 27 28 /** 29 * A program element type. The constants of this enumerated type 30 * provide a simple classification of the declared elements in a 31 * Java program. 32 * 33 * <p>These constants are used with the {@link Target} meta-annotation type 34 * to specify where it is legal to use an annotation type. 35 * 36 * @author Joshua Bloch 37 * @since 1.5 38 */ 39 public enum ElementType { 40 /** Class, interface (including annotation type), or enum declaration */ 41 TYPE, 42 43 /** Field declaration (includes enum constants) */ 44 FIELD, 45 46 /** Method declaration */ 47 METHOD, 48 49 /** Parameter declaration */ 50 PARAMETER, 51 52 /** Constructor declaration */ 53 CONSTRUCTOR, 54 55 /** Local variable declaration */ 56 LOCAL_VARIABLE, 57 58 /** Annotation type declaration */ 59 ANNOTATION_TYPE, 60 61 /** Package declaration */ 62 PACKAGE 63 }
@Retention的源代码
1 /* 2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 */ 25 26 package java.lang.annotation; 27 28 /** 29 * Indicates how long annotations with the annotated type are to 30 * be retained. If no Retention annotation is present on 31 * an annotation type declaration, the retention policy defaults to 32 * {@code RetentionPolicy.CLASS}. 33 * 34 * <p>A Retention meta-annotation has effect only if the 35 * meta-annotated type is used directly for annotation. It has no 36 * effect if the meta-annotated type is used as a member type in 37 * another annotation type. 38 * 39 * @author Joshua Bloch 40 * @since 1.5 41 */ 42 @Documented 43 @Retention(RetentionPolicy.RUNTIME) 44 @Target(ElementType.ANNOTATION_TYPE) 45 public @interface Retention { 46 RetentionPolicy value(); 47 }
@Retention中RetentionPolicy 源代码
/* * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package java.lang.annotation; /** * Annotation retention policy. The constants of this enumerated type * describe the various policies for retaining annotations. They are used * in conjunction with the {@link Retention} meta-annotation type to specify * how long annotations are to be retained. * * @author Joshua Bloch * @since 1.5 */ public enum RetentionPolicy { /** * Annotations are to be discarded by the compiler. */ SOURCE, /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior. */ CLASS, /** * Annotations are to be recorded in the class file by the compiler and * retained by the VM at run time, so they may be read reflectively. * * @see java.lang.reflect.AnnotatedElement */ RUNTIME }
Java的注解