美文网首页
201013:Linux查看版本-自定义注解反射调用

201013:Linux查看版本-自定义注解反射调用

作者: 弹钢琴的崽崽 | 来源:发表于2020-10-13 20:28 被阅读0次

    一. Linux查看版本命令

    1. 查看Linux内核版本命令

    1. cat /proc/version
    2. uname -a

    2. 查看Linux系统版本的命令

    1. lsb_release -a

      注意:使用该命令即可列出所有版本信息,这个命令适用于所有的Linux发行版,包括RedHat、SUSE、Debian等发行版。

    2. cat /etc/redhat-release

      注意:该方法只适合Redhat系的Linux

    3. cat /etc/issue

      注意:该命令也适用于所有的Linux发行版

    3. Linux 下面解压.tar.gz 和.gz文件解压的方式

    两种解压方式

    1. tar.gz 使用tar命令进行解压

      tar -zxvf java.tar.gz
      解压到指定的文件夹
      tar -zxvf java.tar.gz  -C /usr/java
      
    2. gz文件的解压 gzip 命令

      gzip -d java.gz
      

    也可使用zcat 命令,然后将标准输出 保存文件

    zcat java.gz > java.java
    

    4. Linux下运行.sh文件的两种方法

    1. 直接./加上文件名.sh,如运行hello.sh为./hello.sh【hello.sh必须有x权限】
    2. 直接sh 加上文件名.sh,如运行hello.sh为sh hello.sh【hello.sh可以没有x权限】

    方法一:绝对路径执行.sh文件

    ./home/test/shell/hello.sh
    
    sh /home/test/shell/hello.sh
    

    方法二:当前目录执行.sh文件

    1. cd到.sh文件所在目录

    2. 给.sh文件添加x执行权限

      chmod u+x hello.sh
      
    3. ./执行.sh文件

      ./hello.sh
      
    4. sh 执行.sh文件

      sh hello.sh
      

    注意事项:用“./”加文件名.sh执行时,必须给.sh文件加x执行权限

    二. 自定义注解

    1. 新建一个annotation,名字为:MyAnnotation.java

    package com.dragon.test.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * Created by gmq on 2015/9/10.
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation
    {
    
        String hello () default "hello";
        String world();
    }
    

    2. 建立一个MyTest.java 来使用上面的annotation

    package com.dragon.test.annotation;
    
    /**
     * Created by gmq on 2015/9/10.
     */
    public class MyTest
    {
    
        @MyAnnotation(hello = "Hello,Beijing",world = "Hello,world")
        public void output() {
            System.out.println("method output is running ");
        }
    }
    

    3. 用反射机制来调用注解中的内容

    package com.dragon.test.annotation;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    
    /**
     * 用反射机制来调用注解中的内容
     * Created by gmq on 2015/9/10.
     */
    public class MyReflection
    {
        public static void main(String[] args) throws Exception
        {
            // 获得要调用的类
            Class<MyTest> myTestClass = MyTest.class;
            // 获得要调用的方法,output是要调用的方法名字,new Class[]{}为所需要的参数。空则不是这种
            Method method = myTestClass.getMethod("output", new Class[]{});
            // 是否有类型为MyAnnotation的注解
            if (method.isAnnotationPresent(MyAnnotation.class))
            {
                // 获得注解
                MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
                // 调用注解的内容
                System.out.println(annotation.hello());
                System.out.println(annotation.world());
            }
            System.out.println("----------------------------------");
            // 获得所有注解。必须是runtime类型的
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations)
            {
                // 遍历所有注解的名字
                System.out.println(annotation.annotationType().getName());
            }
        }
    }
    

    4. 输出:

    Hello,Beijing
    Hello,world
    ----------------------------------
    com.dragon.test.annotation.MyAnnotation
    

    相关文章

      网友评论

          本文标题:201013:Linux查看版本-自定义注解反射调用

          本文链接:https://www.haomeiwen.com/subject/zqrbpktx.html