溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用java的注解(用在java類(lèi)的方法上的注解)方法

發(fā)布時(shí)間:2020-09-24 07:30:25 來(lái)源:腳本之家 閱讀:277 作者:zhangbeizhen18 欄目:編程語(yǔ)言

場(chǎng)景:根據(jù)方法上的注解,通過(guò)java反射方式找到需要執(zhí)行的的方法。

1.注解類(lèi)

/**注解作用在方法上*/
@Target({ElementType.METHOD}) 
/**注解的生命周期一直程序運(yùn)行時(shí)都存在VM運(yùn)行期間保留注解,可以通過(guò)反射機(jī)制讀取注解信息*/
@Retention(RetentionPolicy.RUNTIME)
/**注解包含在Javadoc中*/
@Documented 
public @interface Item {
	String value();
}

2.在類(lèi)的方法上使用注解

public class WorkService {
 
	private static Logger logger = LoggerFactory.getLogger(WorkService.class);
 
	@Item("1001")
	public int getItem1001Info(GirlVo girl) {
 
		logger.info("getItem1001Info輸出:");
		logger.info(girl.toString());
		return 1;
	}
	@Item("1002")
	public int getItem1002Info(GirlVo girl) {
 
		logger.info("getItem1002Info輸出:");
		logger.info(girl.toString());
		return 1;
	}
	@Item("1003")
	public int getItem1003Info(GirlVo girl) {
		logger.info("getItem1003Info輸出:");
		logger.info(girl.toString());
		return 1;
	}
	@Item("1004")
	public int getItem1004Info(GirlVo girl) {
		logger.info("getItem1004Info輸出:");
		logger.info(girl.toString());
		return 1;
	}
	@Item("1005")
	public int getItem1005Info(GirlVo girl) {
		logger.info("getItem1005Info輸出:");
		logger.info(girl.toString());
		return 1;
	}
	@Item("1006")
	public int getItem1006Info(GirlVo girl) {
		logger.info("getItem1006Info輸出:");
		logger.info(girl.toString());
		return 1;
	}
}

3.實(shí)體類(lèi),作為參數(shù)傳遞

public class GirlVo implements Serializable {
 
	private static final long serialVersionUID = -8545755852948661858L;
	/**ID號(hào)*/
	private String girlId;
	/**姓名*/
	private String girlName;
	/**年齡*/
	private Long age;
	/**身高*/
	private Double height;
	/**罩杯*/
	private String cupSize;
	public String getGirlId() {
		return girlId;
	}
	public void setGirlId(String girlId) {
		this.girlId = girlId;
	}
	public String getGirlName() {
		return girlName;
	}
	public void setGirlName(String girlName) {
		this.girlName = girlName;
	}
	public long getAge() {
		return age;
	}
	public void setAge(long age) {
		this.age = age;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public String getCupSize() {
		return cupSize;
	}
	public void setCupSize(String cupSize) {
		this.cupSize = cupSize;
	}
 
	@Override
	public String toString() {
		
		return "GirlVo [girlId=" + girlId
				+",girlName=" + girlName
				+",age=" + age
				+",height=" + height
				+",cupSize=" + cupSize
				+"]";
	}
}

4.反射調(diào)用方法注解的類(lèi)

		 List<Method> methods = MethodUtils.getMethodsListWithAnnotation(clazz,Item.class);
		 for (Method method : methods) {
			 for (Annotation annotation : method.getDeclaredAnnotations()) {
				 if (annotation instanceof Item){
					 String code = ((Item) annotation).value();
					 if (Objects.equals(item, code)){
						 try {	
							logger.info("注解號(hào):" + code +" 執(zhí)行開(kāi)始...");
							method.invoke(clazz.newInstance(), girlVo);
							logger.info("注解號(hào):" + code +" 執(zhí)行結(jié)束...");
						} catch (Exception e) {
							e.printStackTrace();
							logger.info("拋異常...");
						}
					 }
				 }
			 }
		 }
 
	}
 
}

5.main方法測(cè)試類(lèi)

public class TestAnnotation {
	
	private static Logger logger = LoggerFactory.getLogger(TestAnnotation.class);
	 
	public static void main(String []args) throws ClassNotFoundException{
		logger.info("測(cè)試開(kāi)始......");
		HashMap<String,Object> map = (HashMap<String,Object>) getData();
		/**遍歷hashMap*/
		for(Entry<String,Object> entry : map.entrySet()){
			String item = entry.getKey();
			GirlVo girlVo = (GirlVo) entry.getValue();
			ReflectCallMethodAnnotation.annotationMethod(item,girlVo);
		}
		
		logger.info("測(cè)試結(jié)束......");
	}
 
 
	public static Map getData() {
 
		HashMap<String ,Object> data = new HashMap();
		 
		GirlVo girlVo1 = new GirlVo();
		girlVo1.setGirlId("1001");
		girlVo1.setGirlName("張一");
		girlVo1.setAge(27);
		girlVo1.setHeight(169);
		girlVo1.setCupSize("F");
		
		GirlVo girlVo2 = new GirlVo();
		girlVo2.setGirlId("1002");
		girlVo2.setGirlName("王二");
		girlVo2.setAge(29);
		girlVo2.setHeight(175);
		girlVo2.setCupSize("B");
		
		GirlVo girlVo3 = new GirlVo();
		girlVo3.setGirlId("1003");
		girlVo3.setGirlName("劉五");
		girlVo3.setAge(24);
		girlVo3.setHeight(160);
		girlVo3.setCupSize("B");
		
		GirlVo girlVo4 = new GirlVo();
		girlVo4.setGirlId("1004");
		girlVo4.setGirlName("陳六");
		girlVo4.setAge(21);
		girlVo4.setHeight(168);
		girlVo4.setCupSize("C");
		
		GirlVo girlVo5 = new GirlVo();
		girlVo5.setGirlId("1005");
		girlVo5.setGirlName("趙七");
		girlVo5.setAge(25);
		girlVo5.setHeight(169);
		girlVo5.setCupSize("C");
		
		GirlVo girlVo6 = new GirlVo();
		girlVo6.setGirlId("1006");
		girlVo6.setGirlName("張八");
		girlVo6.setAge(26);
		girlVo6.setHeight(165);
		girlVo6.setCupSize("A");
		
		data.put("1001", girlVo1);
		data.put("1002", girlVo2);
		data.put("1003", girlVo3);
		data.put("1004", girlVo4);
		data.put("1005", girlVo5);
		data.put("1006", girlVo6);
		
		return data;
	}
 
}

以上所述是小編給大家介紹的使用java的注解(用在java類(lèi)的方法上的注解)方法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI