服务器之家

服务器之家 > 正文

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

时间:2019-06-20 15:53     来源/作者:zhangbeizhen18

场景:根据方法上的注解,通过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类的方法上的注解)方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

标签:

相关文章

热门资讯

叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍
叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍 2019-06-05
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
淘宝合合卡可以送好友吗 淘宝合合卡快速获得方式
淘宝合合卡可以送好友吗 淘宝合合卡快速获得方式 2019-06-13
叠猫猫可以邀请多少人 叠猫猫大挑战一个队最多多少人
叠猫猫可以邀请多少人 叠猫猫大挑战一个队最多多少人 2019-06-05
淘宝618叠猫猫怎么退出队伍?每天可退出几次队伍
淘宝618叠猫猫怎么退出队伍?每天可退出几次队伍 2019-06-06
返回顶部