注解和maven

本文深入探讨Java注解的原理与应用,包括注解的声明、使用及反射提取,同时解析Maven构建工具的基本概念、配置及生命周期,帮助读者理解两者在现代Java开发中的重要角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java注解和maven

1:java注解

1:1:注解是什么
    1:啥是注解
        代码里的特殊标记,在编译时候会加载,运行时被读取,并进行相应的处理,在不动类
        的逻辑代码的情况下对类的逻辑产生做用,注解也是替代配置文件的有效手段。(对类的补充)
        jdk1.5出来的,以前叫做元数据

作用

        1:注解在不改变原有类的前提下,额外添加补充信息
        2:有效替代配置文件的有效手段(例如@WebServlet(name="",value=""))
        使用xml properties 等配置文件进行配置编写,脱离了类的本身,写起来麻烦
        开发人员在类上不能直观的看到相应的配置
        所以 spring boot极力推崇0配置全部注解的方式

2:技术点

        1:如何申明和添加注解,满足开发要求
        2:编写注解和注解反射获取,
        框架需要编写约束文档,读取配置文件,配置文件做相应的读取操作
    3:基本注解
        @override
        @overloading

用于控制注解的保留期{

                  RetentionPolicy.SOURCE
                  RetentionPolicy.CLASS 默认  实际作用不大
            RetentionPolicy.RUNTIME  反射可以获取
        }
      1:Retention
          保留时间:运行期RetentionPolicy.RUNTIME   源码期,RetentionPolicy.SOURCE 
          RetentionPolicy.CLASS编译期
      2:@Documented
            将类的信息添加到javadoc中
      3:@Target
            作用地址
            类|方法|构造函数|属性等
            作用范围:
             - ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
              - ElementType.CONSTRUCTOR 可以给构造方法进行注解
              - ElementType.FIELD 可以给属性进行注解
              - ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
              - ElementType.METHOD 可以给方法进行注解
              - ElementType.PACKAGE 可以给一个包进行注解
              - ElementType.PARAMETER 可以给一个方法内的参数进行注解
              - ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举
      4:Inherited
          继承性
          继承类使用了注解的类,自动使用这个注解
      5:Repeatable
          可以重复使用这个注解
    自己写的注解为啥可以写在任意位置,系统注解为啥会添加到指定位置
    默认情况下,注解就是脱缰的野马,使用起来没有任何约束
    可以

3:注解的具体属性

    属性类型
        1:基本类型
        2:String类型
        3:类类型
        4:数组类型
        5:以上类型的数组类型
    1:注解的属性如何申明
        注解除了提示作用,大部分情况起到设置作用
        注解本身不可以设置属性
        注解的属性可以设置属性 
        只要是有属性可以共开发人员设置,一般情况下是runtime类型
        注解里面的属性申明和方法的申明类似,方法名就是属性名,返回值就是属性类型
        例如:
        public @interface 注解名称{
          String value() fefault "";//String
          int age() defalut "12";//int 
          User user() 
        }  

2:注解的属性如何赋值

    案例:
        @Documented
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface GrandFatherAnntation {
            String name();//String 
            int age() default 80;//int 
            GrandMothor nainai();//Annotation
        }
        @Target({ElementType.METHOD,ElementType.TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        public @interface GrandMothor {
            String name();
            int age() default 80;
            sons [] sunzis();
        }
        @Target({ElementType.METHOD,ElementType.TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        public @interface sons {
            String name();
            int age();
            Gf []  nvpenyou();
        }
        @Target({ElementType.METHOD,ElementType.TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        public @interface Gf {
            String name();
            int age() default 18;
        }
        //调用赋值    key=value形式
        @GrandFatherAnntation(name = "hfh",age =23, 
          nainai = @GrandMothor(name = "f",age = 23,
          sunzis = @sons(name = "fkj",age = 23,
          nvpenyou = @Gf(name = "hjk",age = 34))))
          public class UserAnnotation {
          }

4:注解在开发中属性的提取

      @Retention(RetentionPolicy.RUNTIME)
       @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
       @Documented
       public @interface UserInfo {
           String value() default "";
           int age() default 1;
       }
### 利用反射获取属性、方法、构造方法上的注解属性
```//获取类上面的注解属性
       Class<UserAnnotation> ua1 = UserAnnotation.class;
       UserInfo annotation = ua1.getAnnotation(UserInfo.class);
       int age = annotation.age();
       String value = annotation.value();
       System.out.println(age+"   "+value);
       @UserInfo(value = "珍君0",age = 0)
       public class UserAnnotation {
           //属性
           @UserInfo(value = "珍君1",age = 1)
           private String zhen;
           //方法上面
           @UserInfo(value = "珍君2",age = 12)
           public void eat(){
           }
           //构造方法上面
           @UserInfo(value = "珍君3",age = 111)
           public UserAnnotation(String zhen) {
               this.zhen = zhen;
           }
       }

//属性上面的注解属性

          Field zhen = ua1.getDeclaredField("zhen");
          UserInfo annotation1 = zhen.getAnnotation(UserInfo.class);
          int age1 = annotation1.age();
          String value1 = annotation1.value();
          System.out.println(age1+"     "+value1);
        //获取方法上面的而属性
          Method eat = ua1.getMethod("eat");
          UserInfo annotation2 = eat.getAnnotation(UserInfo.class);
          int age2 = annotation2.age();
          String value2 = annotation2.value();
          System.out.println(age2+"    "+ value2);
        //构造方法上面的属性
          Constructor<UserAnnotation> constructor = ua1.getConstructor(String.class);
          UserInfo annotation3 = constructor.getAnnotation(UserInfo.class);
          int age3 = annotation3.age();
          String value3 = annotation3.value();
          System.out.println(age3+"    "+value3);
         
### 案例二
         案例:Class User
         @Param("张三");
            privat String name;
            @Param("11");
            private int age;
            @MethodParam(name="张三",age="11");
            public void setData(String name,int age){
             this.name = name;
             thts.age  = age;
            }
            getter/setter方法
            toString方法
         编写两个注解!一个@Param 另一个@MethodParam
         注解target和属性值如上!
         编写一个注解解析类!
         要求使用Param注解可以给属性赋值!
         或者通过MethodParam注解可以通过setData方法进行属性赋值!
         最后输出User对象,直接可以获取注解添加的内容!
   
### 注解      
@Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.METHOD)
          public @interface MethodParam {
              String name();
              String age();
          }
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.FIELD)
          public @interface Param {
              String value();
          }
          public class User {
          @Param("张三")
          private String name;
          @Param("11")
          private int age;
          @MethodParam(name = "张三",age = "12")
          public void setData(String name,int age){
              this.age=age;
              this.name=name;
          }
          public User() {
          }
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public int getAge() {
              return age;
          }
          public void setAge(int age) {
              this.age = age;
          }
          @Override
          public String toString() {
              return "User{" +
                      "name='" + name + '\'' +
                      ", age=" + age +
                      '}';
          }
      }
      
Class<User> userClass = User.class;
        Field name = userClass.getDeclaredField("name");
        Field age = userClass.getDeclaredField("age");
        name.setAccessible(true);
        age.setAccessible(true);
        Param annotation = name.getAnnotation(Param.class);
        Param annotation1 = age.getAnnotation(Param.class);
        User user = userClass.newInstance();
        user.setData(annotation.value(), Integer.parseInt(annotation1.value()));
        System.out.println(user.toString());
        //method
        Method setData = userClass.getMethod("setData", String.class, int.class);
        MethodParam annotation2 = setData.getAnnotation(MethodParam.class);
        String age1 = annotation2.age();
        String name1 = annotation2.name();
        System.out.println(age1+"     "+name1);

2:maven

1:mavan是什么

1:maven:"专家|内行" 是一个基于对象模型的用来构建和依赖管理java项目的一个工具
2:构建:java代码能运行起来会经历:清理->编译->测试->报告->打包->部署
3:状态:源码->编译->运行时

2:约定:约定优于配置----maven有自己的配置原则,有自己的目录结构,因此更加适合协同开发团队开发。

3:结构
project_name
----src
-------main
----------java
-------------com
-----------------qf
--------------------Hello.java
resources
webapp (web项目)
-------test
----------java
-------------com
-----------------qf
-------------------test
----------------------HelloTest.java
----------resources
-----pom.xml

4:配置

1:安装:-->下载-->解压(指定位置)
2:配置:1:环境:右键我的电脑(或者计算机)-->属性-->高级系统设置-->高级环境变量--.系统变量-->
            新建MAVEN_HOME-->值为maven的安装目录
         2:把%MAVEN_HOME%\bin;追加到Path变量的值后面
         3.检验是否成功:cmd-> mvn -v  或 mvn -version
         4.修改配置文件:maven安装目录下conf目录中settings.xml
                1:<!--第一个位置在根节点下添加本地仓库位置  用来存储下载的jar包-->
                  <localRepository>自己指的位置</localRepository>
                2:远程仓库,当依赖中引入jar时没有必要去国外仓库下载jar提高下载速度
                   <!--第二个位置,在mirrors节点下添加 中央仓库镜像-->
                   <mirror>
                          <id>alimaven</id>
                          <name>aliyun maven</name>
                          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                          <mirrorOf>central</mirrorOf>       
                   </mirror> 
                3:修改jdk编译版本
                <!--第三个位置,在profiles节点下添加 jdk版本全局配置,因为默认为1.5-->
                <profile>  
                        <id>jdk18</id>  
                        <activation>  
                            <activeByDefault>true</activeByDefault>  
                            <jdk>1.8</jdk>  
                        </activation>  
                        <properties>  
                            <maven.compiler.source>1.8</maven.compiler.source>  
                            <maven.compiler.target>1.8</maven.compiler.target>  
                            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
                        </properties>   
                 </profile>

5:寻找jar包过程

                  现在本地仓库找 ----> 如果本地仓库里面没有---->如果没有会去远程仓库寻找
                  ---->如果没有会去中央仓库寻找   途中如果前面过程找到了就直接返回,下载,不会再
                  去更远的仓库--->打完收功

5:集成环境关联maven(idea)

  1:  file--->other settings--->setting for new projects--->maven--->maven home dir...选择自己安装的位置
       usersettins file config setting.xml文件位置
       local repository :自己配置本地仓库的位置
  2:--->importing ---->import maven projects automatically
     --->sources   Documentation
  3:file ---> new project ---> maven  archetype 根据自己建的项目类型选择   填写gav 
    g:GroupId:公司域名倒置
    a:ArtifactId:项目名|模块名
    v:Versoion:版本    --->下一步...

6:生命周期

验证   编译   测试  打包  安装  部署    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值