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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

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

本文主要向大家介紹了JAVA語(yǔ)言注解概念使用及Demo講解,通過(guò)具體的內(nèi)容向大家展示,希望對(duì)大家學(xué)習(xí)JAVA語(yǔ)言有所幫助。

java注解

概念

Java提供了一種原程序中的元素關(guān)聯(lián)任何消息和任何元數(shù)據(jù)的途徑和方法,即注解

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自帶的注解都屬于編譯時(shí)注解

*/

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.

*?運(yùn)行階段還起作用,設(shè)置可以影響代碼運(yùn)行邏輯@Autowired

*?@see?java.lang.reflect.AnnotatedElement

*/

RUNTIME

@Inherited允許子類繼承?標(biāo)識(shí)性注解

@Documented生成javaDoc時(shí)會(huì)包含注解?標(biāo)識(shí)性注解

自定義注解

元注解

使用@interface關(guān)鍵字定義注解

成員:

以無(wú)參無(wú)異常方式聲明?可以用default給成員指定一個(gè)默認(rèn)值

成員類型是受限制的:java基本數(shù)據(jù)類型?+?String?Class?Annoatation?Enumeration

如果注解只有一個(gè)成員,則成員名為value(),在使用是可以忽略成員名和賦值好(=)

注解類可以沒有成員,沒有成員則稱為標(biāo)識(shí)注解

@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?{

}

解析注解

概念:通過(guò)反射獲取類?函數(shù)或成員上的運(yùn)行時(shí)注解信息,從而實(shí)現(xiàn)動(dòng)態(tài)控制程序運(yùn)行的邏輯

注解應(yīng)用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();

}

獲取注解,定義注解要進(jìn)行的操作

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)?{

//處理每個(gè)字段對(duì)應(yīng)的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?擴(kuò)展其他類型

}

}

return?result.toString();

};

}

應(yīng)用注解,應(yīng)用操作

本文由職坐標(biāo)整理并發(fā)布,希望對(duì)同學(xué)們有所幫助。了解更多詳情請(qǐng)關(guān)注編程語(yǔ)言JAVA頻道!

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。