栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java自定义注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java自定义注解

1.java文件叫Annotation ,用@interface表示

2.原注解,@interface上面按需要注解上一些东西,包括@Retention,@Target,@document,@Inherited四种

3.注解的保留策略:

@Retention(RetenttionPolicy.Source) //注解仅存在于源码中,在class字节码中不包含

@Retention(RetentionPolicy.CLASS)  //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

@Retention(Retention.RUNTIME) //注解会在class文件中存在,在运行时通过反射获取到

4.注解的作用目标:

@Target(ElementType.TYPE)//接口,类,枚举,注解

@Target(Element Type.FIELD)//字段枚举的常量

@Target(ElementType.METHOD)//方法

@Target(ElementType.PARAMETER) //方法参数

@Target(ElementType.CONSTRUCTOR) //构造函数

@Target(Element Type.LOCAL_VARIABLE) //局部变量

@Target(ElementType.ANNOTATION_TYPE) //注解

@Target(ElementType.PACKAGE) //包

5.注解包含在javadoc中

@documented

6.注解可以被继承

@Inherited

7.注解解析器:用来自定义注解

package org.javaboy.vhr.explain;

import java.lang.annotation.*;


@documented
@Inherited
@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Init {
    public String value() default "123";
}
#建立user类
package org.javaboy.vhr.test;

import org.javaboy.vhr.explain.Init;
import org.javaboy.vhr.explain.Vadiate;


public class User {
    @Vadiate(min = 1,max = 10)
    private String name;
    private String age;

    public String getName() {
        return name;
    }
   @Init(value = "xushuai")
    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }
  @Init(value = "123")
    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + ''' +
                ", age='" + age + ''' +
                '}';
    }

    public static void main(String[] args) {
        User user = UserFactory.create();


        System.out.println(user.getName());
        System.out.println(user.getAge());
    }
}
package org.javaboy.vhr.test;

import org.javaboy.vhr.explain.Init;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class UserFactory {
    public static  User create() {
        User user = new User();
        //通过反射获取user类中的所有方法
        Method[] methods = User.class.getDeclaredMethods();
        try {
            for (Method method : methods) {
                //如果有注解就吧注解里的数据复制到user对象里
                if (method.isAnnotationPresent(Init.class)) {
                    Init annotation = method.getAnnotation(Init.class);
                    method.invoke(user, annotation.value());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
        return user;
    }
}

结果如下:

8.自定义注解进行校验:

package org.javaboy.vhr.test;
import org.javaboy.vhr.explain.Vadiate;


import java.lang.reflect.Field;


public class UserCheck {
    public static boolean check(User user){
        if (user == null){
            System.out.println("!!!user对象不能为空");
            return false;
        }
        Field[] declaredFields = User.class.getDeclaredFields();

            if (declaredFields != null && declaredFields.length>0){
                for (Field declaredField : declaredFields) {
                    //如果有注解,就校验
                    if (declaredField.isAnnotationPresent(Vadiate.class)){
                        Vadiate annotation = declaredField.getAnnotation(Vadiate.class);
                        System.out.println(declaredField.getName()+"adasd");
                        if (declaredField.getName().equals("name")){
                            System.out.println(user.getName().length());
                        if(user.getName().length()> annotation.max()|| user.getName().length()

结果如下:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号