今天面試官考我Java注解...




public void send(String userName) {
try {
// qps 上報
qps(params);
long startTime = System.currentTimeMillis();
// 構建背景關系(模擬業務代碼)
ProcessContext processContext = new ProcessContext();
UserModel userModel = new UserModel();
userModel.setAge("22");
userModel.setName(userName);
//...
// rt 上報
long endTime = System.currentTimeMillis();
rt(endTime - startTime);
} catch (Exception e) {
// 出錯上報
error(params);
}
}





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




文章以純面試的角度去講解,所以有很多的細節是未鋪墊的,
比如說反射、.java檔案到jvm的程序、AOP是什么等等等...這些在【Java3y】都有過詳細的基本教程甚至電子書,我就不再詳述了,
注解可以把它當做是配置的載體,可能在運行時、可能在編譯程序中決議注解,實作些方便好用的功能,
歡迎關注我的微信公眾號【面試造火箭】來聊聊Java面試

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/239876.html
標籤:Java
