常见注解解析

基本的内置注解:

1.@Override 重写父类的方法

2.@Deprecated 表示方法已过期

3.@SuppressWarnings 忽略控制台警告信息

. @SuppressWarnings({ "rawtypes", "unused" })常用写法

1.deprecation:使用了不赞成使用的类或方法时的警告(使用@Deprecated使得编译器 产生的警告);

2.unchecked:执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型; 关闭编译器警告

3.fallthrough:当 Switch 程序块直接通往下一种情况而没有 Break 时的警告;

4.path:在类路径、源文件路径等中有不存在的路径时的警告;

5.serial:当在可序列化的类上缺少 serialVersionUID 定义时的警告;

6.finally:任何 finally 子句不能正常完成时的警告;

7.rawtypes 泛型类型未指明

8.unused 引用定义了,但是没有被使用

9.all:关于以上所有情况的警告。

4.@FunctionalInterface 用于约定函数接口。

某接口包含一个抽象方法(包含多个默认方法或STATIC方法),该接口称为函数式接口,主要是供Lamba表达式使用

元数据:为其他数据提供信息的数据.

元注解:用于注解自定义注解的注解

常用的元注解有四个:

1.@Target

2.@Retention

3.@Inherited

4.@Documented

下面详细进行解答:

  

@Target :可以放置在方法和类型上,但不能放在属性等其他位置.

可选位置如下:

  1. ElementType.TYPE:能修饰类
  2. ElementType.FIELD:能修饰成员变量
  3. ElementType.METHOD:能修饰方法
  4. ElementType.PARAMETER:能修饰参数
  5. ElementType.CONSTRUCTOR:能修饰构造器
  6. ElementType.LOCAL_VARIABLE:能修饰局部变量
  7. ElementType.ANNOTATION_TYPE:能修饰注解
  8. ElementType.PACKAGE:能修饰包

示例代码:

package anno;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({METHOD,TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface JDBCConfig {
     String ip(); 
     int port() default 3306; 
     String database(); 
     String encoding(); 
     String loginName(); 
 }

 

@Retention 表示生命周期

可选值:

RetentionPolicy.SOURCE: 注解只在源代码中存在,编译成class之后,就没了。 @Override 就是这种注解。

RetentionPolicy.CLASS: 注解在java文件编程成.class文件后,依然存在,但是运行起来后就没了。@Retention的默认值,即当没有显式指定@Retention的时候,就会是这种类型。

RetentionPolicy.RUNTIME: 注解在运行起来之后依然存在,程序可以通过反射获取这些信息。

示例代码:

    

package anno;
 
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
 
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target({METHOD,TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
 
public @interface JDBCConfig {
     String ip();
     int port() default 3306;
     String database();
     String encoding();
     String loginName();
     String password();
}

 

@Inherited 表示该注解具有继承性.

示例代码:

@Target(ElementType.TYPE)  
@Retention(RetentionPolicy.RUNTIME)  
@Inherited  
public @interface ATable {  
  
    public String name() default "";  
}  
  
@Target(ElementType.TYPE)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface BTable {  
    public String name() default "";  
}  
  
  
@ATable  
public class Super {  
    private int superx;  
    public int supery;  
    public Super() {  
    }  
    private int superX(){    
        return 0;    
    }    
    public int superY(){    
        return 0;    
    }    
      
}  
  
  
@BTable  
public class Sub extends Super{  
    private int subx;  
    public int suby;  
    private Sub()  
    {    
    }    
    public Sub(int i){    
    }    
    private int subX(){    
        return 0;    
    }    
    public int subY(){    
        return 0;    
    }    
}  
  
  
  
public class TestMain {  
    public static void main(String[] args) {  
          
        Class<Sub> clazz = Sub.class;  
          
        System.out.println("============================Field===========================");    
        System.out.println(Arrays.toString(clazz.getFields()));  
        System.out.println(Arrays.toString(clazz.getDeclaredFields()));  //all + 自身    
        System.out.println("============================Method===========================");  
        System.out.println(Arrays.toString(clazz.getMethods()));   //public + 继承    
        //all + 自身    
        System.out.println(Arrays.toString(clazz.getDeclaredMethods()));  
        System.out.println("============================Constructor===========================");    
        System.out.println(Arrays.toString(clazz.getConstructors()));    
                System.out.println(Arrays.toString(clazz.getDeclaredConstructors()));    
        System.out.println("============================AnnotatedElement===========================");    
        //注解DBTable2是否存在于元素上    
        System.out.println(clazz.isAnnotationPresent(BTable.class));    
        //如果存在该元素的指定类型的注释DBTable2,则返回这些注释,否则返回 null。    
        System.out.println(clazz.getAnnotation(BTable.class));    
        //继承    
        System.out.println(Arrays.toString(clazz.getAnnotations()));    
        System.out.println(Arrays.toString(clazz.getDeclaredAnnotations()));  自身    
    }  
}  

 

@Documented:API文档专用注解

特别的:使用eclipse把项目中的.java文件导成API文档步骤:

1. 选中项目

2. 点开菜单File

3. 点击Export

4. 点开java->javadoc->点next

5. 点finish

 

新增的标签:

@Repeatable

@Repeatable修饰的时候,注解在同一个位置,只能出现一次

示例代码:

package annotation;
import static java.lang.annotation.ElementType.METHOD;

import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class FindFiles {
    @Target( METHOD)
    @Retention( RetentionPolicy.RUNTIME )
    public @interface FileTypes {
        FileType[] value();
    }

    @Target(  METHOD )
    @Retention( RetentionPolicy.RUNTIME )
    @Repeatable( FileTypes.class )
    public @interface FileType {
        String value();
    };

    @FileType( ".java" )
    @FileType( ".html" )
    @FileType( ".css" )
    @FileType( ".js" )
    public void work(){
    	
    	try {
    		FileType[] fileTypes= this.getClass().getMethod("work").getAnnotationsByType(FileType.class);
    		System.out.println("将从如下后缀名的文件中查找文件内容");
    		for (FileType fileType : fileTypes) {
				System.out.println(fileType.value());
			}
    		System.out.println("查找过程略。。。");
		} catch (NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public static void main(String[] args) {
    	new FindFiles().work();
    }
}

 

代码说明:

为了紧凑起见,把注解作为内部类的形式放在一个文件里。

1. 注解FileTypes,其value()返回一个FileType数组

2. 注解FileType,其@Repeatable的值采用FileTypes

3. 运用注解:在work方法上重复使用多次@FileType注解 

4. 解析注解: 在work方法内,通过反射获取到本方法上的FileType类型的注解数组,然后遍历本数组

**********************************************************************************************************************

注解分类:

1.按照注解的作用域@rentention分:

RententionPolicy.SOURCE:java源文件上的注解

RententionPolicy.CLASS:Class类文件上的注解.

RententionPolicy.RUNTIME: 运行时的注解.

 

2.按照注解源来分:

内置注解 :@override

@Deprecated

第三方注解:@Hibernate

@Strruts

自定义注解: 仿hibernate的自定义注解.

例:

public @interface MyAnno(){ }

反编译结果:

public interface MyAnno extends java.lang.annotation.Annotation{}

 

注解本质:注解本质就是接口,该接口默认继承Annotation接口.

属性:

(接口里面可以定义的成员方法和属性)

1.属性的返回值类型:

基本数据类型

Spring

注解类型

以上类型的数组

2.定义的属性 使用的时候要赋值.

 

1.定义属性是用default关键字给属性默认值,使用时不用赋值.

2.如果只有一个属性需要赋值,属性名字叫value,可以直接复制

3.数组赋值用{},如果数组中只有一个,{}可以省略..

注解作用:编写文档:通过代码里标识的注解生成文档【生成DOC文档】

代码分析:通过代码里标识的注解对代码进行分析

编译检查:通过代码里的标识,让编译器能够实现基本的编译检查功能

 

到此基本的注解讲解完毕..

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值