欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用java的注解(用在java类的方法上的注解)方法

程序员文章站 2024-02-26 10:32:10
场景:根据方法上的注解,通过java反射方式找到需要执行的的方法。 1.注解类 /**注解作用在方法上*/ @target({elementtype.me...

场景:根据方法上的注解,通过java反射方式找到需要执行的的方法。

1.注解类

/**注解作用在方法上*/
@target({elementtype.method}) 
/**注解的生命周期一直程序运行时都存在vm运行期间保留注解,可以通过反射机制读取注解信息*/
@retention(retentionpolicy.runtime)
/**注解包含在javadoc中*/
@documented 
public @interface item {
	string value();
}

2.在类的方法上使用注解

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.实体类,作为参数传递

public class girlvo implements serializable {
 
	private static final long serialversionuid = -8545755852948661858l;
	/**id号*/
	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.反射调用方法注解的类

		 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("注解号:" + code +" 执行开始...");
							method.invoke(clazz.newinstance(), girlvo);
							logger.info("注解号:" + code +" 执行结束...");
						} catch (exception e) {
							e.printstacktrace();
							logger.info("抛异常...");
						}
					 }
				 }
			 }
		 }
 
	}
 
}

5.main方法测试类

public class testannotation {
	
	private static logger logger = loggerfactory.getlogger(testannotation.class);
	 
	public static void main(string []args) throws classnotfoundexception{
		logger.info("测试开始......");
		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("测试结束......");
	}
 
 
	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类的方法上的注解)方法详解整合,希望对大家有所帮助