注解概念
目前成都創(chuàng)新互聯(lián)公司已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、青岡網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
注解是Java SE 5.0版本開(kāi)始引入的概念,它是對(duì)java源代碼的說(shuō)明,是一種元數(shù)據(jù)(描述數(shù)據(jù)的數(shù)據(jù))。
注解和注釋的不同
注解分類(lèi)
注解以@開(kāi)頭,我們會(huì)在應(yīng)用程序中見(jiàn)到各種各樣的注解,比如 @Autowired,@Service,@Controller,@Override,@Test,@Value 等等,按照來(lái)源劃分,可以分為 JDK的注解, 第三方的注解, 自定義注解。
JDK注解
JAVA 內(nèi)置注解
第三方注解(各種框架注解)
請(qǐng)自行百度各個(gè)框架的注解詳解
自定義注解
使用元注解自己定義的注解
JDK中有一些元注解,主要有@Target,@Retention,@Document,@Inherited用來(lái)修飾注解。
表明該注解可以應(yīng)用的java元素類(lèi)型
表明該注解的生命周期
表明該注解標(biāo)記的元素可以被Javadoc 或類(lèi)似的工具文檔化
表明使用了@Inherited注解的注解,所標(biāo)記的類(lèi)的子類(lèi)也會(huì)擁有這個(gè)注解
注解格式
/**
* 修飾符
@interface 注解名 {
* 注解元素的聲明1
* 注解元素的聲明2
* }
* 修飾符:訪問(wèn)修飾符必須為public,不寫(xiě)默認(rèn)為pubic;
* 關(guān)鍵字:必須為
@interface;
* 注解名: 注解名稱(chēng)為自定義注解的名稱(chēng),使用時(shí)還會(huì)用到;
* 注解類(lèi)型元素:注解類(lèi)型元素是注解中內(nèi)容,可以理解成自定義接口的實(shí)現(xiàn)部分;
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public
@interface MyTestAnnotation {
/**
* 注解的元素聲明的兩種形式
* type elementName();
* type elementName() default value;
*/
String value()
default
"test";
}
注解樣例
接下來(lái)我們以Spring中的 @Service 注解為例
@Target({ElementType
.TYPE})
// ElementType.TYPE 代表在注解上使用
@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表運(yùn)行時(shí)使用,可以通過(guò)反射獲取到
@Documented//包含在JavaDoc中
@Component//允許通過(guò)包掃描的方式自動(dòng)檢測(cè)
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
@Annotation
JDK1.5有的,在rt.jar包下 java.lang.annotation包下,所有的注解默認(rèn)繼承了Annotation接口,但是它本身不能定義注解。
package java.lang.annotation;
/**
* 所有的注解默認(rèn)繼承了Annotation接口,但是它本身不能定義注解。
* The common interface extended by all annotation types. Note that an
* interface that manually extends this one does <i>not</i> define
* an annotation type. Also note that this interface does not itself
* define an annotation type.
*
* More information about annotation types can be found in section 9.6 of
* <cite>The Java? Language Specification</cite>.
*
* The {
@link java.lang.reflect.AnnotatedElement} interface discusses
* compatibility concerns when evolving an annotation type from being
* non-repeatable to being repeatable.
*
*
@author Josh Bloch
*
@since 1.5
*/
public
interface
Annotation{
.
.
.
}
第一步-定義自定義注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public
@interface MyTestAnnotation {
String value()
default
"test";
}
第二步-配置注解
@Data
@Builder
@MyTestAnnotation
public class MyBean {
private
String
name;
private
int
age;
}
第三步-利用反射解析注解
public
class
MyTest {
//isAnnotationPresent:判斷當(dāng)前元素是否被指定注解修飾
//getAnnotation:返回指定的注解
//getAnnotations:返回所有的注解
public static void main(String[] args) {
try {
//獲取MyBean的Class對(duì)象
MyBean myBean = MyBean.builder().build();
Class clazz = myBean.getClass();
//判斷myBean對(duì)象上是否有MyTestAnnotation注解
if (clazz.isAnnotationPresent(MyTestAnnotation.class)) {
System.out.println("MyBean類(lèi)上配置了MyTestAnnotation注解!");
//獲取該對(duì)象上MyTestAnnotation類(lèi)型的注解
MyTestAnnotation myTestAnnotation = (MyTestAnnotation) clazz.getAnnotation(MyTestAnnotation.class);
System.out.println(myTestAnnotation.value());
} else {
System.out.println("MyBean類(lèi)上沒(méi)有配置MyTestAnnotation注解!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
執(zhí)行main方法,運(yùn)行結(jié)果:
Connected to the target VM, address:
'127.0.0.1:62125', transport:
'socket'
MyBean類(lèi)上配置了MyTestAnnotation注解!
test
Disconnected
from the target VM, address:
'127.0.0.1:62125', transport:
'socket'
本文標(biāo)題:Java教程:JAVA自定義注解
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article0/gigjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、商城網(wǎng)站、定制開(kāi)發(fā)、網(wǎng)站營(yíng)銷(xiāo)、軟件開(kāi)發(fā)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)