註解使用

現在註解的使用是很多框架都喜歡使用的,首先了解一下元註解


元註解(meta-annotation):

元註解的作用就是負責註解其他註解。Java5.0定義了4個標準的meta-annotation類型,它們被用來提供對其它 annotation類型作說明。Java5.0定義的元註解:

1.@Target,2.@Retention,3.@Documented,4.@Inherited

這些類型和它們所支持的類在java.lang.annotation包中可以找到。下面我們看一下每個元註解的作用和相應分參數的使用說明。

@Target:

@Target說明了Annotation所修飾的對象範圍:Annotation可被用於 packages、types(類、接口、枚舉、Annotation類型)、類型成員(方法、構造方法、成員變量、枚舉值)、方法參數和本地變量(如循環變量、catch參數)。在Annotation類型的聲明中使用了target可更加明晰其修飾的目標。

作用:用於描述註解的使用範圍(即:被描述的註解可以用在什麼地方)

取值(ElementType)有:

1.CONSTRUCTOR:用於描述構造器

2.FIELD:用於描述域

3.LOCAL_VARIABLE:用於描述局部變量

4.METHOD:用於描述方法

5.PACKAGE:用於描述包

6.PARAMETER:用於描述參數

7.TYPE:用於描述類、接口(包括註解類型) 或enum聲明

@Retention:

@Retention定義了該Annotation被保留的時間長短:某些Annotation僅出現在源代碼中,而被編譯器丟棄;而另一些卻被編譯在class文件中;編譯在class文件中的Annotation可能會被虛擬機忽略,而另一些在class被裝載時將被讀取(請注意並不影響class的執行,因爲Annotation與class在使用上是被分離的)。使用這個meta-Annotation可以對 Annotation的“生命週期”限制。

作用:表示需要在什麼級別保存該註釋信息,用於描述註解的生命週期(即:被描述的註解在什麼範圍內有效)

取值(RetentionPoicy)有:

1.SOURCE:在源文件中有效(即源文件保留)

2.CLASS:在class文件中有效(即class保留)

3.RUNTIME:在運行時有效(即運行時保留)

Retention meta-annotation類型有唯一的value作爲成員,它的取值來自java.lang.annotation.RetentionPolicy的枚舉類型值。

@Documented:

@Documented用於描述其它類型的annotation應該被作爲被標註的程序成員的公共API,因此可以被例如javadoc此類的工具文檔化。Documented是一個標記註解,沒有成員。

@Inherited:

@Inherited 元註解是一個標記註解,@Inherited闡述了某個被標註的類型是被繼承的。如果一個使用了@Inherited修飾的annotation類型被用於一個class,則這個annotation將被用於該class的子類。

注意:@Inherited annotation類型是被標註過的class的子類所繼承。類並不從它所實現的接口繼承annotation,方法並不從它所重載的方法繼承annotation。

當@Inherited annotation類型標註的annotation的Retention是RetentionPolicy.RUNTIME,則反射API增強了這種繼承性。如果我們使用java.lang.reflect去查詢一個@Inherited annotation類型的annotation時,反射代碼檢查將展開工作:檢查class和其父類,直到發現指定的annotation類型被發現,或者到達類繼承結構的頂層。



下面是自己寫的一個例子


首先寫一個註解@interface

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 *
 * @auther QiaoZhenwu
 * @date 2017年7月11日 上午10:31:33
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface Service {

//	public Class<?> getCls();//代表註釋位置是否要加屬性信息  例:@Service(getCls = User.class)	private User user;
}


然後寫一個註解的執行器

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.alibaba.fastjson.JSON;

/**
 *
 * @auther QiaoZhenwu
 * @date 2017年7月11日 上午10:41:10
 */
public class ServiceActuator {

	public static void getAllBean(Object obj){
		Class<? extends Object> cls = obj.getClass();
		
		// 得到自定義的屬性
		Field[] fields = cls.getDeclaredFields();
		try {
			for (Field f : fields) {
				// 循環判斷是否有UploadFile的自動注入field
				Service uf = f.getAnnotation(Service.class);
				if (uf != null) {
					// 假如此屬性被註解爲UploadFile
					// 得到需要注入的實例class
					System.out.println(f.getType().getName());
					
//					Class c = Class.forName(f.getType().getName());
//					Object o = c.newInstance();
//					Field[] fs = c.getDeclaredFields();
//					for(Field fi : fs){
//						String fiName = fi.getName();
//						String setMethod = "set" + fiName.substring(0, 1).toUpperCase() + fiName.substring(1);
//						Method m = c.getDeclaredMethod(setMethod, new Class[]{fi.getType()});
//						m.invoke(o, fiName + "-java");//調用set方法,並設置值
	            	  //獲取屬性
	            	   PropertyDescriptor pd = new PropertyDescriptor(f.getName(), cls);
	                   Method getMethod = pd.getReadMethod();// 獲得get方法
	                   Object ob = getMethod.invoke(obj);// 執行get方法返回一個Object
	                   System.out.println(f.getName() + ":" + f.getType().getName() + " = " + JSON.toJSONString(ob));
	                   
	                   Class c = f.getType();
	                   Field[] fs = c.getDeclaredFields();
	                   //判斷屬性是否含有屬性
	                   if(fs.length > 0){
	                	   for(Field fi : fs){
		                	   PropertyDescriptor pd1 = new PropertyDescriptor(fi.getName(), c);
		                	   Method getMethod1 = pd1.getReadMethod();// 獲得get方法
		                	   Object ob2 = getMethod1.invoke(ob);// 執行get方法返回一個Object
		                	   System.out.println(fi.getName() + ":" + fi.getType().getName() + " = " + ob2);
		                   }
	                   }
//					}
				}
			}
		}catch(Exception e){
			
		}
	}


再寫兩個測試的entity

/**
 *
 * @auther QiaoZhenwu
 * @date 2017年7月11日 上午11:36:46
 */
public class Image {

	private String name;
	
	private String path;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
}
/**
 *
 * @auther QiaoZhenwu
 * @date 2017年7月11日 上午10:55:18
 */
public class User {

	private String name;
	
	private String age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
	
}


測試類

/**
 *
 * @auther QiaoZhenwu
 * @date 2017年7月11日 上午10:54:18
 */
public class TestMain {

	@Service
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Service
	private Image p_w_picpath;

	public Image getImage() {
		return p_w_picpath;
	}

	public void setImage(Image p_w_picpath) {
		this.p_w_picpath = p_w_picpath;
	}

	public static void main(String[] args) {
		TestMain t = new TestMain();
		User u = new User();
		u.setAge("age=18");
		u.setName("name=xw");
		t.setUser(u);
		Image i = new Image();
		i.setName("name=p_w_picpath.jpg");
		i.setPath("test.p_w_picpath.jpg");
		t.setImage(i);
		ServiceActuator.getAllBean(t);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章