日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 注解 demo_JAVA语言注解概念使用及Demo讲解

發布時間:2025/3/21 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 注解 demo_JAVA语言注解概念使用及Demo讲解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要向大家介紹了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";

}

使用注解

@(=,=,…)

@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頻道!

總結

以上是生活随笔為你收集整理的java 注解 demo_JAVA语言注解概念使用及Demo讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。