【對線面試官】今天來聊聊Java注解



public?void?send(String?userName)?{
??try?{
????//?qps?上報(bào)
????qps(params);
????long?startTime?=?System.currentTimeMillis();
????//?構(gòu)建上下文(模擬業(yè)務(wù)代碼)
????ProcessContext?processContext?=?new?ProcessContext();
????UserModel?userModel?=?new?UserModel();
????userModel.setAge("22");
????userModel.setName(userName);
????//...
????//?rt?上報(bào)
????long?endTime?=?System.currentTimeMillis();
????rt(endTime?-?startTime);
??}?catch?(Exception?e)?{
????
????//?出錯(cuò)上報(bào)
????error(params);
??}
}





@Around("@annotation(com.sanwai.service.openapi.monitor.Monitor)")
public?Object?antispan(ProceedingJoinPoint?pjp)?throws?Throwable?{
????String?functionName?=?pjp.getSignature().getName();
????Map?tags?=?new?HashMap<>();
????logger.info(functionName);
????tags.put("functionName",?functionName);
????tags.put("flag",?"done");
????monitor.sum(functionName,?"start",?1);
????//方法執(zhí)行開始時(shí)間
????long?startTime?=?System.currentTimeMillis();
????Object?o?=?null;
????try?{
????????o?=?pjp.proceed();
????}?catch?(Exception?e)?{
????????//方法執(zhí)行結(jié)束時(shí)間
????????long?endTime?=?System.currentTimeMillis();
????????tags.put("flag",?"fail");
????????monitor.avg("rt",?tags,?endTime?-?startTime);
????????monitor.sum(functionName,?"fail",?1);
????????throw?e;
????}
????//方法執(zhí)行結(jié)束時(shí)間
????long?endTime?=?System.currentTimeMillis();
????monitor.avg("rt",?tags,?endTime?-?startTime);
????if?(null?!=?o)?{
????????monitor.sum(functionName,?"done",?1);
????}
????return?o;
}



文章以純面試的角度去講解,所以有很多的細(xì)節(jié)是未鋪墊的。
比如說反射、.java文件到j(luò)vm的過程、AOP是什么等等等基礎(chǔ)...這些在【Java3y】都有過詳細(xì)的基本教程甚至電子書,我就不再詳述了。
注解可以把它當(dāng)做是配置的載體,可能在運(yùn)行時(shí)、可能在編譯過程中解析注解,實(shí)現(xiàn)些方便好用的功能。
添加我的微信【sanwaiyihao】進(jìn)一步交流和學(xué)習(xí)
評論
圖片
表情
