java 注解 demo_JAVA语言注解概念使用及Demo讲解
本文主要向大家介紹了JAVA語言注解概念使用及Demo講解,通過具體的內(nèi)容向大家展示,希望對大家學(xué)習(xí)JAVA語言有所幫助。
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自帶的注解都屬于編譯時注解
*/
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.
*?運行階段還起作用,設(shè)置可以影響代碼運行邏輯@Autowired
*?@see?java.lang.reflect.AnnotatedElement
*/
RUNTIME
@Inherited允許子類繼承?標(biāo)識性注解
@Documented生成javaDoc時會包含注解?標(biāo)識性注解
自定義注解
元注解
使用@interface關(guān)鍵字定義注解
成員:
以無參無異常方式聲明?可以用default給成員指定一個默認(rèn)值
成員類型是受限制的:java基本數(shù)據(jù)類型?+?String?Class?Annoatation?Enumeration
如果注解只有一個成員,則成員名為value(),在使用是可以忽略成員名和賦值好(=)
注解類可以沒有成員,沒有成員則稱為標(biāo)識注解
@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?{
}
解析注解
概念:通過反射獲取類?函數(shù)或成員上的運行時注解信息,從而實現(xiàn)動態(tài)控制程序運行的邏輯
注解應(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)?{
//處理每個字段對應(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?擴展其他類型
}
}
return?result.toString();
};
}
應(yīng)用注解,應(yīng)用操作
本文由職坐標(biāo)整理并發(fā)布,希望對同學(xué)們有所幫助。了解更多詳情請關(guān)注編程語言JAVA頻道!
總結(jié)
以上是生活随笔為你收集整理的java 注解 demo_JAVA语言注解概念使用及Demo讲解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java enum 定义属性_java
- 下一篇: 面试java回答优缺点_阿里Java开发