what
1.注解:Java中的元數(shù)據(jù)
元數(shù)據(jù)肥败?:描述數(shù)據(jù)的數(shù)據(jù)莱没,更通俗一點(diǎn)胁编,就是描述代碼間關(guān)系,或者代碼與其他資源(例如數(shù)據(jù)庫表)之間內(nèi)在聯(lián)系的數(shù)據(jù)专酗。eg:.xml配置文件
2.注解相當(dāng)于一種標(biāo)記睹逃,在程序中加了注解就等于為程序打上了某種標(biāo)記,里面的屬性就是鍵值對(duì)祷肯。程序可以利用java的反射機(jī)制來了解類及各種元素上有無何種標(biāo)記沉填,針對(duì)不同的標(biāo)記,就去做相應(yīng)的事件佑笋。標(biāo)記可以加在包翼闹,類,字段允青,方法橄碾,方法的參數(shù)以及局部變量上卵沉。JDK定義成@interface
3.java的注解本質(zhì)上是一個(gè)接口,而且是繼承了接口Annotation的接口法牲,見JDK文檔
/**
* 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 {
}
why
本質(zhì)注解和.xml文件實(shí)現(xiàn)效果是一致的:
.xml:集中式的元數(shù)據(jù)史汗,與源代碼無綁定
@interface(注解):分散式的元數(shù)據(jù),與源代碼緊綁定
如何選擇拒垃?
1.約定大于一切
2.用配置還是走XML吧停撞,比如事務(wù)配置,比如數(shù)據(jù)庫連接池等等
3.注解缺乏靈活性悼瓮,在實(shí)現(xiàn)復(fù)雜的邏輯上戈毒,沒有XML來的更加強(qiáng)大;注解就是要么用横堡,要么不用埋市,不可用功能的一部分
4.注解簡(jiǎn)單,但是不容易理解
...
how
xml:
<?xml version="1.0" encoding="UTF-8"?>
<SCHOOL>
<name>藍(lán)胖子</name>
<address>日本東京</address>
<food name="banana">
<num>2<num>
</food>
<food name="apple">
<num>3</num>
</food>
</SCHOOL>
注解:
package com.test.zy.xml;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Student {
private String studentNum;
private String studentName;
private String studentGrade;
private int age;
@XmlElement(name="num")
public String getStudentNum() {
return studentNum;
}
public void setStudentNum(String studentNum) {
this.studentNum = studentNum;
}
@XmlElement(name="name")
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@XmlAttribute(name="grade")
public String getStudentGrade() {
return studentGrade;
}
public void setStudentGrade(String studentGrade) {
this.studentGrade = studentGrade;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [studentNum=" + studentNum + ", studentName="
+ studentName + ", studentGrade=" + studentGrade + ", age="
+ age + "]";
}
}
注解生命周期
一個(gè)注解可以有三個(gè)生命周期命贴,它默認(rèn)的生命周期是保留在一個(gè)CLASS文件道宅,
但它也可以由一個(gè)@Retetion的元注解指定它的生命周期。
a.java源文件
當(dāng)在一個(gè)注解類前定義了一個(gè)@Retetion(RetentionPolicy.SOURCE)的注解胸蛛,那么說明該注解只保留在一個(gè)源文件當(dāng)中污茵,當(dāng)編譯器將源文件編譯成class文件時(shí),它不會(huì)將源文件中定義的注解保留在class文件中葬项。
b. class文件中
當(dāng)在一個(gè)注解類前定義了一個(gè)@Retetion(RetentionPolicy.CLASS)的注解泞当,那么說明該注解只保留在一個(gè)class文件當(dāng)中,當(dāng)加載class文件到內(nèi)存時(shí)民珍,虛擬機(jī)會(huì)將注解去掉襟士,從而在程序中不能訪問。
c. 程序運(yùn)行期間
當(dāng)在一個(gè)注解類前定義了一個(gè)@Retetion(RetentionPolicy.RUNTIME)的注解穷缤,那么說明該注解在程序運(yùn)行期間都會(huì)存在內(nèi)存當(dāng)中敌蜂。此時(shí)箩兽,我們可以通過反射來獲得定義在某個(gè)類上的所有注解津肛。