JAVA语言注解概念使用及Demo讲解
小标 2018-12-26 来源 : 阅读 1019 评论 0

摘要:本文主要向大家介绍了JAVA语言注解概念使用及Demo讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言注解概念使用及Demo讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。


java注解


概念


Java提供了一种原程序中的元素关联任何消息和任何元数据的途径和方法,即注解


java中的常见注解


@Override


@Deprecated


@Suppvisewarnings


第三方注解


Spring @Autowired @Service @Repository Mybatis @InsertProvider @UpdateProvider @Options


元注解


@Target注解 作用域控制


/** Class, interface (including annotation type), or enum declaration */

TYPE,

 

/** Field declaration (includes enum constants) */

FIELD,

 

/** Method declaration */

METHOD,

 

/** Formal parameter declaration */

PARAMETER,

 

/** Constructor declaration */

CONSTRUCTOR,

 

/** Local variable declaration */

LOCAL_VARIABLE,

 

/** Annotation type declaration */

ANNOTATION_TYPE,

 

/** Package declaration */

PACKAGE,

 

/**

 * Type parameter declaration

 *

 * @since 1.8

 */

TYPE_PARAMETER,

 

/**

 * Use of a type

 *

 * @since 1.8

 */

TYPE_USE

   


@Retention注解的生命周期


/**

 * Annotations are to be discarded by the compiler.

 * 注解只在源码中存在,编译成.class就不存在了 

 */

SOURCE,

 

/**

 * Annotations are to be recorded in the class file by the compiler

 * but need not be retained by the VM at run time.  This is the default

 * behavior.

 * 注解在源码和.class文件中都存在 jdk自带的注解都属于编译时注解 

 */

CLASS,

 

/**

 * Annotations are to be recorded in the class file by the compiler and

 * retained by the VM at run time, so they may be read reflectively.

 * 运行阶段还起作用,设置可以影响代码运行逻辑@Autowired

 * @see java.lang.reflect.AnnotatedElement

 */

RUNTIME

   


@Inherited允许子类继承 标识性注解


@Documented生成javaDoc时会包含注解 标识性注解


自定义注解


元注解


使用@interface关键字定义注解


成员:


以无参无异常方式声明 可以用default给成员指定一个默认值


成员类型是受限制的:java基本数据类型 + String Class Annoatation Enumeration


如果注解只有一个成员,则成员名为value(),在使用是可以忽略成员名和赋值好(=)


注解类可以没有成员,没有成员则称为标识注解


@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface TestAnnoation {

    String desc();

    int visted();

    String comment() default "test";

}

   


使用注解


@<注解名>(<成员名1>=<成员值1>,<成员名2>=<成员名2>,…)


@TestAnnoation(desc="ttttest",visted=1)

public class TestDomain {

 

}

   


解析注解


概念:通过反射获取类 函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑


注解应用demo


定义注解


@Target({ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface Column {

    String value();

}

 

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface Table {

    String value();

}

   


获取注解,定义注解要进行的操作


public class SqlUtil {

    public static String getSql(Object o){

        StringBuilder result = new StringBuilder();

        //1.获取class

        Class c = o.getClass();

        //2.获取table名字 即注解的value

        boolean isTableAnnotation = c.isAnnotationPresent(Table.class);

        if(!isTableAnnotation){

            return null;

        }

        Table t = (Table) c.getAnnotation(Table.class);

        String tableName = t.value();

        result.append("select * from ").append(tableName).append(" where 1 = 1");

        //3.获取字段名字

        Field[] declaredFields = c.getDeclaredFields();

        for (Field field : declaredFields) {

            //处理每个字段对应的sql

            //拿到字段名 看是不是column

            boolean isColumnAnnotation = field.isAnnotationPresent(Column.class);

            if(!isColumnAnnotation){

                continue;

            }

            Column colum = field.getAnnotation(Column.class);

            String columnName = colum.value();

            //拿到字段的值

            String fieldName = field.getName();

            String getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);

            Object fieldValue = null;

            try {

                Method method = c.getMethod(getMethodName);

                fieldValue = method.invoke(o);

            } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            //拼装sql

            if(fieldValue == null || (fieldValue instanceof Integer && (Integer)fieldValue == 0)){

                continue;

            }

            result.append(" and ").append(fieldName).append("=");

            if(fieldValue instanceof String){

                result.append("'").append(fieldValue).append("'");

            }else if(fieldValue instanceof Integer){

                result.append(fieldValue);

            }else{

                //TODO 扩展其他类型

            }

 

        }

        return result.toString();

    };

}

   


应用注解,应用操作


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程