java注解相关

it2022-05-29  78

1.怎么创建一个注解?

注释是一种接口形式,其中关键字接口以@开头,其主体包含与方法非常相似的注释类型元素声明,如下所示:

a、创建注解

import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Cks { String value() default "This is an element"; int[] type(); }

b、使用注解

import com.ck.admin_fiction.demo.Cks; @Cks(value = "hjh",type = {1,2,3}) public class Test { }

注意:为数组元素提供多个值时,必须将它们括在括号中。并且其可选地,只要它们是编译器的常量表达式,就可以提供默认值。

2.可以从注解方法声明返回哪些对象类型?

返回类型必须是基本类型,String,Class,Enum或以前类型之一的数组。否则,编译器将抛出错误。

eg:成功的栗子

enum Complexity { LOW, HIGH } public @interface ComplexAnnotation { Class<? extends Object> value(); int[] types(); Complexity complexity(); }

由于_Object_不是有效的返回类型,下一个示例将无法编译:

public @interface FailingAnnotation { Object complexity();

3. 哪些程序元素可以注解?

注释可以应用于整个源代码的多个位置。它们可以应用于类,构造函数和字段的声明。

eg:举个栗子

@SimpleAnnotation //类 public class Apply { @SimpleAnnotation //属性 private String aField; @SimpleAnnotation //方法 public Apply() { // ...可以用在局部变量,包,其他注释类型 }

4. java元注解有哪些?

java中有四个元注解,@Target,@Retention,@Documented,@Inherited

元注解描述@Target表示该注解可以用于什么地方。可能的ElementType参数包括:CONSTRUCTOR:构造器的生命FIELD:域声明(包括enum实例)LOCAL_VARIABLE:局部变量声明METHOD:方法声明PACKAGE:包声明PARAMETER:参数声明TYPE:类、接口(包括注解类型)和enum声明ANNOTATION_TYPE:注解声明(与TYPE的区别?,专门用在注解上的TYPE)TYPE_PARAMETER:Java8TYPE_USE:Java8@Retention表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:SOURCE:注解将在编译器丢弃CLASS:注解在class文件中可用,但会被VM丢弃RUNTIME:VM将在运行期也保留注解,因此可以通过反射机制读取注解的信息@Documented将此注解包含在Javadoc中@Inherited允许子类继承父类中的注解

详细介绍如下:

a.@Target注解

ElementType 这个枚举类型的常量提供了一个简单的分类:注释可能出现在Java程序中的语法位置(这些常量与元注释类型(@Target)一起指定在何处写入注释的合法位置)

ElementType的类型如下 类型小写描述TYPEtype类, 接口 (包括注释类型), 或 枚举 声明FIELDfield字段声明(包含枚举常量)METHODmethod方法声明PARAMETERparameter正式的参数声明CONSTRUCTORconstructor构造函数声明LOCAL_VARIABLElocal_variable局部变量声明ANNOTATION_TYPEannotation_type注释类型声明PACKAGEpackage包声明TYPE_PARAMETERtype_papameter类型参数声明TYPE_USEtype_use使用类型声明 ElementType具体源码如下 public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ TYPE, /** Field declaration (includes enum constants) */ FIELD, /** Method declaration */ METHOD, /** Formal parameter declaration */ PARAMETER, /** Constructor declaration */ CONSTRUCTOR, /** Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE, /** * Type parameter declaration * * @since 1.8 */ TYPE_PARAMETER, /** * Use of a type * * @since 1.8 */ TYPE_USE }

b. @Retention注解

RetentionPolicy这个枚举类型的常量描述保留注释的各种策略,它们与元注释(@Retention)一起指定注释要保留多长时间

RetentionPolicy的类型如下: 类型小写描述SOURCEsource注释只在源码级别保留,编译时被忽略CLASSclass注释将被编译器在类文件中记录,但在运行时不需要JVM保留,这是默认行为RUNTIMEruntime注释将类编辑器记录到类文件中,在运行是保留JVM,因此可以反读 RetentionPolicy的源码如下: 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 }

c.@Documented注解

Documented注解表明这个注释是由 javadoc记录的,在默认情况下也有类似的记录工具。 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分。

package java.lang.annotation; /** * Indicates that annotations with a type are to be documented by javadoc * and similar tools by default. This type should be used to annotate the * declarations of types whose annotations affect the use of annotated * elements by their clients. If a type declaration is annotated with * Documented, its annotations become part of the public API * of the annotated elements. * * @author Joshua Bloch * @since 1.5 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Documented { }

d.@Inherited注解

允许子类继承父类中的注解

5. 什么是重复注解?

a.重复注解

即允许在同一申明类型(类,属性,或方法)前多次使用同一个类型注解

在java8 以前,同一个程序元素前最多只能有一个相同类型的注解;如果需要在同一个元素前使用多个相同类型的注解,则必须使用注解“容器”

java8之前使用方式:

public @interface Authority {      String role(); } //@Authorities注解作为可以存储多个@Authority注解的容器 public @interface Authorities {         Authority[] value(); } public class RepeatAnnotationUseOldVersion {     @Authorities({@Authority(role="admin"),@Authority(role="manager")})     public void doSomeThing(){     } }

java8新增了重复注解,其使用方式为:

@Repeatable(Authorities.class) public @interface Authority {      String role(); } public @interface Authorities {     Authority[] value(); } public class RepeatAnnotationUseNewVersion {     @Authority(role="admin")     @Authority(role="manager")     public void doSomeThing(){ } }

6.描述5个有用的注解。

类型描述@Controller该注释声明Controller层,一般在spring里面使用@RequestMapping该注释在方法前或者类前,表示请求的路径@Autowired该注释表示自动装配,默认按照类型装配@Service用在Service层的代码@Repository该注解对应数据访问层的Bean@Component该注解不确定是哪一层的时候使用该注解

7.是否可以扩展注释?

注释。注释总是扩展_java.lang.annotation.Annotation,_如Java语言规范中所述。

如果我们尝试在注释声明中使用_extends_子句,我们将得到一个编译错误:

public @interface AnAnnotation extends OtherAnnotation { // Compilation error}

reference:https://blog.csdn.net/qq_37939251/article/details/83215703

转载于:https://www.cnblogs.com/tidetrace/p/10845345.html


最新回复(0)