• Java注解(Java高级)


    思维导图

    image.png

    1、注解概述

    • 什么是注解:什么是注解:Annotation 注解,是一种代码级别的说明。它是 JDK1.5 及以后版本引入的一个特性,与类、接口、枚举是在同一个层次

    • 注解的作用:主要用于取代 XML 和 properties 配置文件

    2、JDK 提供的注解 (了解)

    • @Override:用在方法上,表示这个方法重写了父类的方法,如 toString ()。如果父类没有这个方法,那么就无法编译通过。

      //#2.1 JDK5.0 复写父类方法
      class Parent1_2{
      public void init(){
      }
      }
      class Son1_2 extends Parent1_2{
      @Override
      public void init() {
      }
      }
      
      //#2.2 JDK6.0 实现父接口方法
      interface Parent1_3{
      public void init();
      }
      class Son1_3 implements Parent1_3{
      @Override
      public void init() {
      }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
    • @Deprecated:表示这个方法已经过期,不建议开发者使用。(暗示在将来某个不确定的版本,就有可能会取消掉)

      //#1 方法过期
      class Parent1_1{
      @Deprecated
      public void init(){
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • @SuppressWarnings:表示抑制警告,这个注解的用处是忽略警告信息。

      • deprecation ,忽略过时
      • rawtypes ,忽略类型安全
      • unused , 忽略不使用
      • unchecked ,忽略安全检查
      • null,忽略空
      • all,忽略所有
      //#3 抑制警告
      // serial : 实现序列号接口,但没有生产序列号
      @SuppressWarnings("serial")class Parent1_4 implements java.io.Serializable{
      //null:空指针
      @SuppressWarnings("null")
      public void init(){
           //rawtypes:类型安全,没有使用泛型
           //unused : 不使用
          @SuppressWarnings({ "rawtypes", "unused" })
          List list = new ArrayList();
          String str = null;
          str.toString();
           }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
    • @SafeVarargs:当使用可变数量的参数的时候,而参数的类型又是泛型 T 的话,就会出现警告。

      public class Hero {
          String name;
      
          @SafeVarargs
          public static  T getFirstOne(T... elements) {
              return elements.length > 0 ? elements[0] : null;
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    • @FunctionalInterface:用于约定函数式接口。

      函数式接口概念: 如果接口中只有一个抽象方法(可以包含多个默认方法或多个 static 方法),该接口称为函数式接口。函数式接口其存在的意义,主要是配合 Lambda 表达式 来使用。

      @FunctionalInterface
      public interface AD {
          public void adAttack();
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5

    3、自定义注解

    image.png
    JDK 提供的 5 种元注解
    @Target:用于确定被修饰的自定义注解 使用位置

    ElementType.TYPE 修饰 类、接口
    ElementType.CONSTRUCTOR:修饰构造
    ElementType.METHOD:修饰方法
    ElementType.FIELD:修饰字段
    ElementType.PARAMETER:修饰参数
    ElementType.LOCAL_VARIABLE:修饰局部变量
    ElementType.ANNOTATION_TYPE:修饰注解
    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();
    String password();
    }
    @Retention:用于确定被修饰的自定义注解生命周期

    RetentionPolicy.SOURCE: 被修饰的注解只能在源码中存在,编译成 字节码 class 没有。如:@Override
    RetentionPolicy.CLASS : 被修饰的注解只能存在源码和字节码中,运行时内存中没有。
    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:表示该注解具有继承性 (了解)

    package util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import anno.JDBCConfig;

    public class DBUtilChild extends DBUtil {

    public static Connection getConnection2() throws SQLException, NoSuchMethodException, SecurityException {
        JDBCConfig config = DBUtilChild.class.getAnnotation(JDBCConfig.class);
        String ip = config.ip();
        int port = config.port();
        String database = config.database();
        String encoding = config.encoding();
        String loginName = config.loginName();
        String password = config.password();
    
        String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
        return DriverManager.getConnection(url, loginName, password);
    }
    
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException {
        Connection c = getConnection2();
        System.out.println(c);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    }
    @Documented:使用 javadoc 生成 api 文档时,是否包含此注解 (了解)

    @Repeatable:注解在同一个位置,只能出现一次。使用 @Repeatable,可以在同一个地方使用多次了。

    package util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import anno.JDBCConfig;

    @JDBCConfig(ip = “127.0.0.1”, database = “test”, encoding = “UTF-8”, loginName = “root”, password = “admin”)
    @JDBCConfig(ip = “127.0.0.1”, database = “test”, encoding = “UTF-8”, loginName = “root”, password = “admin”)
    public class DBUtil {
    static {
    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }

    public static Connection getConnection() throws SQLException, NoSuchMethodException, SecurityException {
        JDBCConfig config = DBUtil.class.getAnnotation(JDBCConfig.class);
        System.out.println(config);
    
        String ip = config.ip();
        int port = config.port();
        String database = config.database();
        String encoding = config.encoding();
        String loginName = config.loginName();
        String password = config.password();
    
        String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
        return DriverManager.getConnection(url, loginName, password);
    }
    
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException {
        Connection c = getConnection();
        System.out.println(c);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    }

  • 相关阅读:
    如何将项目(工程/代码)文件上传到gitee?(注意一下,有几个坑)
    常用的辅助网站(持续更新)
    JAVA:实现QuickSelect快速选择算法(附完整源码)
    字节码之 Lambda 表达式底层原理
    数据库 事务
    Unity 从0开始编写一个技能编辑器_01_分析需求
    Vulnhub系列靶机---XXE Lab: 1
    哪个牌子的台灯对孩子的视力好?对孩子视力好的台灯推荐分享
    网络知识——局域网和交换机
    海外媒体宣发套餐如何利用3种方式洞察市场-华媒舍
  • 原文地址:https://blog.csdn.net/m0_54850604/article/details/126235230