java自定義注解接口實(shí)現(xiàn)方案
更新時(shí)間:2012年11月25日 18:36:08 作者:
java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運(yùn)行時(shí)進(jìn)行解析和使用,起到說(shuō)明、配置的功能,本文將詳細(xì)介紹,此功能的實(shí)現(xiàn)方法
java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運(yùn)行時(shí)進(jìn)行解析和使用,起到說(shuō)明、配置的功能。
注解不會(huì)也不能影響代碼的實(shí)際邏輯,僅僅起到輔助性的作用。包含在 java.lang.annotation 包中。
1、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四種。
1.1、@Retention: 定義注解的保留策略
Java代碼
@Retention(RetentionPolicy.SOURCE) //注解僅存在于源碼中,在class字節(jié)碼文件中不包含
@Retention(RetentionPolicy.CLASS) //默認(rèn)的保留策略,注解會(huì)在class字節(jié)碼文件中存在,但運(yùn)行時(shí)無(wú)法獲得,
@Retention(RetentionPolicy.RUNTIME)//注解會(huì)在class字節(jié)碼文件中存在,在運(yùn)行時(shí)可以通過(guò)反射獲取到
1.2、@Target:定義注解的作用目標(biāo)
Java代碼
@Target(ElementType.TYPE) //接口、類(lèi)、枚舉、注解
@Target(ElementType.FIELD) //字段、枚舉的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法參數(shù)
@Target(ElementType.CONSTRUCTOR) //構(gòu)造函數(shù)
@Target(ElementType.LOCAL_VARIABLE)//局部變量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
elementType 可以有多個(gè),一個(gè)注解可以為類(lèi)的,方法的,字段的等等
1.3、@Document:說(shuō)明該注解將被包含在javadoc中
1.4、@Inherited:說(shuō)明子類(lèi)可以繼承父類(lèi)中的該注解
下面是自定義注解的一個(gè)例子
2、注解的自定義
Java代碼
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelloWorld {
public String name() default "";
}
3、注解的使用,測(cè)試類(lèi)
Java代碼
public class SayHello {
@HelloWorld(name = " 小明 ")
public void sayHello(String name) {
System.out.println(name + "say hello world!");
}//www.heatpress123.net
}
4、解析注解
java的反射機(jī)制可以幫助,得到注解,代碼如下:
Java代碼
public class AnnTest {
public void parseMethod(Class<?> clazz) {
Object obj;
try {
// 通過(guò)默認(rèn)構(gòu)造方法創(chuàng)建一個(gè)新的對(duì)象
obj = clazz.getConstructor(new Class[] {}).newInstance(
new Object[] {});
for (Method method : clazz.getDeclaredMethods()) {
HelloWorld say = method.getAnnotation(HelloWorld.class);
String name = "";
if (say != null) {
name = say.name();
System.out.println(name);
method.invoke(obj, name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AnnTest t = new AnnTest();
t.parseMethod(SayHello.class);
}
}
注解不會(huì)也不能影響代碼的實(shí)際邏輯,僅僅起到輔助性的作用。包含在 java.lang.annotation 包中。
1、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四種。
1.1、@Retention: 定義注解的保留策略
Java代碼
復(fù)制代碼 代碼如下:
@Retention(RetentionPolicy.SOURCE) //注解僅存在于源碼中,在class字節(jié)碼文件中不包含
@Retention(RetentionPolicy.CLASS) //默認(rèn)的保留策略,注解會(huì)在class字節(jié)碼文件中存在,但運(yùn)行時(shí)無(wú)法獲得,
@Retention(RetentionPolicy.RUNTIME)//注解會(huì)在class字節(jié)碼文件中存在,在運(yùn)行時(shí)可以通過(guò)反射獲取到
1.2、@Target:定義注解的作用目標(biāo)
Java代碼
復(fù)制代碼 代碼如下:
@Target(ElementType.TYPE) //接口、類(lèi)、枚舉、注解
@Target(ElementType.FIELD) //字段、枚舉的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法參數(shù)
@Target(ElementType.CONSTRUCTOR) //構(gòu)造函數(shù)
@Target(ElementType.LOCAL_VARIABLE)//局部變量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
elementType 可以有多個(gè),一個(gè)注解可以為類(lèi)的,方法的,字段的等等
1.3、@Document:說(shuō)明該注解將被包含在javadoc中
1.4、@Inherited:說(shuō)明子類(lèi)可以繼承父類(lèi)中的該注解
下面是自定義注解的一個(gè)例子
2、注解的自定義
Java代碼
復(fù)制代碼 代碼如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelloWorld {
public String name() default "";
}
3、注解的使用,測(cè)試類(lèi)
Java代碼
復(fù)制代碼 代碼如下:
public class SayHello {
@HelloWorld(name = " 小明 ")
public void sayHello(String name) {
System.out.println(name + "say hello world!");
}//www.heatpress123.net
}
4、解析注解
java的反射機(jī)制可以幫助,得到注解,代碼如下:
Java代碼
復(fù)制代碼 代碼如下:
public class AnnTest {
public void parseMethod(Class<?> clazz) {
Object obj;
try {
// 通過(guò)默認(rèn)構(gòu)造方法創(chuàng)建一個(gè)新的對(duì)象
obj = clazz.getConstructor(new Class[] {}).newInstance(
new Object[] {});
for (Method method : clazz.getDeclaredMethods()) {
HelloWorld say = method.getAnnotation(HelloWorld.class);
String name = "";
if (say != null) {
name = say.name();
System.out.println(name);
method.invoke(obj, name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AnnTest t = new AnnTest();
t.parseMethod(SayHello.class);
}
}
相關(guān)文章
Java網(wǎng)絡(luò)編程實(shí)例——簡(jiǎn)單模擬在線聊天
學(xué)了java網(wǎng)絡(luò),也是該做個(gè)小案例來(lái)鞏固一下了。本次案例將使用UDP和多線程模擬即時(shí)聊天,簡(jiǎn)單練練手。2021-05-05Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法
這篇文章主要介紹了Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Mybatis批量修改時(shí)出現(xiàn)報(bào)錯(cuò)問(wèn)題解決方案
這篇文章主要介紹了Mybatis批量修改時(shí)出現(xiàn)報(bào)錯(cuò)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Spring中Controller和RestController的區(qū)別詳解
這篇文章主要介紹了Spring中Controller和RestController的區(qū)別詳解,@Controller是標(biāo)識(shí)一個(gè)Spring類(lèi)是Spring MVC controller處理器,@Controller類(lèi)中的方法可以直接通過(guò)返回String跳轉(zhuǎn)到j(luò)sp、ftl、html等模版頁(yè)面,需要的朋友可以參考下2023-09-09Java實(shí)現(xiàn)線性表的鏈?zhǔn)酱鎯?chǔ)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)線性表的鏈?zhǔn)酱鎯?chǔ),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Spring中@Autowired與@Resource的區(qū)別詳析
@Autowired與@Resource都可以用來(lái)裝配bean,都可以寫(xiě)在字段上,或?qū)懺趕etter方法上,下面這篇文章主要給大家介紹了關(guān)于Spring中@Autowired與@Resource區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-10-10Mybatis-Plus實(shí)現(xiàn)SQL攔截器的示例
這篇文章主要介紹了Mybatis-Plus實(shí)現(xiàn)一個(gè)SQL攔截器,通過(guò)使用SQL攔截器,開(kāi)發(fā)人員可以在執(zhí)行SQL語(yǔ)句之前或之后對(duì)其進(jìn)行修改或記錄,從而更好地控制和優(yōu)化數(shù)據(jù)庫(kù)操作,對(duì)Mybatis-Plus?SQL攔截器相關(guān)知識(shí)感興趣的朋友一起看看吧2023-05-05