java高級用法之注解和反射講義
前言
反射和注解在java中偏高級用法,一般在各種框架中被廣泛應(yīng)用,文章簡單介紹下反射和注解的用法,希望對你的工作學(xué)習(xí)有一定幫助
java注解
什么是注解
Java 注解也就是Annotation是從 Java5 開始引入的新技術(shù)
Annotation的作用:
- 不是程序本身,可以對程序作出解釋
- 可以被其他程序(編譯器等)讀取
Annotation的格式:
- 注解以@注釋名在代碼中存在的,可以添加一些數(shù)值,例如SuppressWarnings(value=”unchecked”)
Annotation在里使用?
- 可以附加在package,class、method,filed等上面,相當(dāng)與給他們添加了額外的輔助信息,我們可以通過反射機(jī)制編程實(shí)現(xiàn)對這些元數(shù)據(jù)的訪問
元注解
元注解的作用就是負(fù)責(zé)注解其他注解,java定義了4個(gè)標(biāo)準(zhǔn)的meta-annotation類型,被用來提供對其他annotation類型作說明
這些類型和它們所支持的類在java.lang.annotation包中可以找到(@Target,@Retention,@Documented,@Inherited)
- @Target:用于描述使用范圍(注解在什么地方使用)
- @Retetion:表示需要在什么級別保證該注釋信息,用于描述注解的生命周期(source<class<runtime)
- @Document:英文意思是文檔。它的作用是能夠?qū)⒆⒔庵械脑匕?Javadoc 中去。
- @Inherited:注解了的注解修飾了一個(gè)父類,如果他的子類沒有被其他注解修飾,則它的子類也繼承了父類的注解
自定義注解
使用@interface自定義注解時(shí),自動(dòng)繼承了java.lang.annotation.Annotation接口
public class Test03 {
//注解可以顯示賦值,如果沒有默認(rèn)值,一定要給注解賦值
@Myannotation2(name = "aj",schloos = {"機(jī)電學(xué)院"})
public void test(){
}
@MyAnnotation3("")
public void test2(){
}
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface Myannotation2{
// 注解的參數(shù),參數(shù)類型+參數(shù)名
String name() default "";
int age() default 0;
//如果默認(rèn)值為-1 代表不存在
int id() default -1;
String[] schloos() ;
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
}
給代碼加注解其實(shí)就是這么多,關(guān)鍵還是我們?nèi)绾稳プx取注解,這就需要用到反射,下面重點(diǎn)介紹java反射
java反射
反射是java被視為動(dòng)態(tài)語言的關(guān)鍵,反射機(jī)制允許程序在執(zhí)行期借助Reflection API取得任何類的內(nèi)部信息,并能直接操作任意對象內(nèi)部熟悉及方法
Class c = Class.forName("java.lang.String")
加載完類之后,在堆內(nèi)存的方法區(qū)就產(chǎn)生了一個(gè)Class類型的對象(一個(gè)類只有一個(gè)Class對象),這個(gè)對象就包含了完整的類的結(jié)構(gòu)信息。我們可以通過這個(gè)對象看到類的結(jié)構(gòu)。這個(gè)對象就像一面鏡子,透過這個(gè)鏡子看到類的結(jié)構(gòu),所以我們稱之為:反射

Class類
對于每個(gè)類而言,JRE都為其保留一個(gè)不變的Class類型的對象,一個(gè)Class對象包含了特定某個(gè)結(jié)構(gòu)的有關(guān)信息。
- Class本身也是一個(gè)類
- Class對象只能由系統(tǒng)建立對象
- 一個(gè)加載的類在jvm中只會(huì)有一個(gè)CLass實(shí)例
- 一個(gè)Class對象對應(yīng)的是一個(gè)加載到j(luò)vm中的一個(gè).class文件
- 每個(gè)類的實(shí)例都會(huì)記得自己是由哪個(gè)Class實(shí)例生成的
- 通過Class可以完整的得到一個(gè)類中的所有被加載的結(jié)構(gòu)
- Class類是Reflection的根源,針對任何你想動(dòng)態(tài)加載、運(yùn)行的類,唯有先獲得相應(yīng)的Class對象
Class類的常用方法
反射獲取對象
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println("這個(gè)人是"+person.name);
//通過對象獲取
Class c1 = person.getClass();
System.out.println(c1.hashCode());
//通過forname獲取
Class c2 = Class.forName("reflection.Student");
System.out.println(c2.hashCode());
//通過類名獲取
Class c3 = Student.class;
System.out.println(c3.hashCode());
//獲得父類類型
Class c4 = c1.getSuperclass();
System.out.println(c4);
}
}
@Data
class Person{
public String name;
public int age;
}
class Student extends Person{
public Student(){
this.name = "學(xué)生";
}
}
class Teacher extends Person{
public Teacher(){
this.name = "老師";
}
}
反射操作方法、屬性
public class Test03 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class.forName("reflection.Student");
Student student = (Student) c1.newInstance();
System.out.println(student.getName());
// 通過反射操作方法
Method setName = c1.getDeclaredMethod("setName", String.class);
setName.invoke(student, "zhangshan");
System.out.println(student.getName());
Student student1 = (Student) c1.newInstance();
Field name = c1.getDeclaredField("name");
//反射不能直接操作私有屬性,需要手動(dòng)關(guān)掉程序的安全檢測,setAccessible(true)
name.setAccessible(true);
name.set(student1,"lisi");
System.out.println(student1.getName());
}
}
性能檢測
public class Test04 {
public static void test01(){
User user = new User();
long startTime = System.currentTimeMillis();
for (int i = 0; i <1000000000 ; i++) {
user.getName();
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime +"ms");
}
public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User user = new User();
long startTime = System.currentTimeMillis();
Class c1 = user.getClass();
Method getName = c1.getDeclaredMethod("getName", null);
for (int i = 0; i <1000000000 ; i++) {
getName.invoke(user, null);
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime +"ms");
}
public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User user = new User();
long startTime = System.currentTimeMillis();
Class c1 = user.getClass();
Method getName = c1.getDeclaredMethod("getName", null);
getName.setAccessible(true);
for (int i = 0; i <1000000000 ; i++) {
getName.invoke(user, null);
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime +"ms");
}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
test01();
test02();
test03();
}
}

反射操作注解
public class Test05 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> c1 = Class.forName("reflection.Customer");
// 通過反射獲取注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation:annotations){
System.out.println(annotation);
}
// 獲取注解的值
TableAnnotation annotation = c1.getAnnotation(TableAnnotation.class);
System.out.println(annotation.value());
//獲取類指定注解
Field id = c1.getDeclaredField("id");
FiledAnnotation annotation1 = id.getAnnotation(FiledAnnotation.class);
System.out.println(annotation1.columnName());
System.out.println(annotation1.length());
System.out.println(annotation1.type());
}
}
//類注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableAnnotation{
String value();
}
@Data
@TableAnnotation("db_customer")
class Customer {
@FiledAnnotation(columnName="id",type = "Long",length =10)
private Long id;
@FiledAnnotation(columnName="age",type = "int",length =10)
private int age;
@FiledAnnotation(columnName="name",type = "String",length =10)
private String name;
}
//方法注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FiledAnnotation{
String columnName();
String type();
int length();
}
總結(jié)
到此這篇關(guān)于java高級用法之注解和反射的文章就介紹到這了,更多相關(guān)java注解和反射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對Java字符串與整形、浮點(diǎn)類型之間的相互轉(zhuǎn)換方法總結(jié)
今天小編就為大家分享一篇對Java字符串與整形、浮點(diǎn)類型之間的相互轉(zhuǎn)換方法總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制(踩坑經(jīng)驗(yàn))
這篇文章主要介紹了springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制,本文給大家分享小編實(shí)際開發(fā)中的一點(diǎn)踩坑經(jīng)驗(yàn),內(nèi)容簡單易懂,需要的朋友可以參考下2020-07-07
堆排序?qū)嵗?Java數(shù)組實(shí)現(xiàn))
下面小編就為大家分享一篇使用Java數(shù)組實(shí)現(xiàn)堆排序的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Spring Boot 2和Redis例子實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Boot2發(fā)布與調(diào)用REST服務(wù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Spring中@RestController和@Controller的使用及區(qū)別
這篇文章主要介紹了Spring中@RestController和@Controller的使用及區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03

