พื้นหลังความต้องการ
ในโครงการล่าสุดเมื่อโครงการเสร็จสิ้นโดยทั่วไปลูกค้าเสนอให้บันทึกบันทึกการดำเนินธุรกิจทั้งหมดลงในฐานข้อมูลและแยกข้อมูลธุรกิจที่สำคัญบางอย่าง (เช่นหมายเลขใบสั่งทำธุรกรรม) ในบันทึก
เพื่อให้แน่ใจว่าระยะเวลาการก่อสร้างหลังจากตรวจสอบข้อมูลฉันตัดสินใจใช้ AOP + คำอธิบายประกอบแบบกำหนดเองเพื่อทำตามข้อกำหนดนี้
การตระเตรียม
แพ็คเกจ JAR ที่จำเป็นต้องขึ้นอยู่กับคำอธิบายประกอบที่กำหนดเองรวมถึง APPERTJRT-XXX.JAR, APPERITURJEAVER-XXX.JAR และ XXX แสดงถึงหมายเลขเวอร์ชัน
คำอธิบายประกอบที่กำหนดเอง
แพ็คเกจบันทึกแยกต่างหากถูกสร้างขึ้นภายใต้โครงการเพื่อจัดเก็บเนื้อหาที่เกี่ยวข้องกับบันทึก
**. Common.log.annotation // สถานที่จัดเก็บคำอธิบายประกอบที่กำหนดเอง **. Common.log.aop // สถานที่จัดเก็บเครื่องมือ AOP
สร้างคลาสคำอธิบายประกอบที่กำหนดเองใหม่ภายใต้แพ็คเกจคำอธิบายประกอบ:
แพ็คเกจ **. Common.log.annotation; นำเข้า java.lang.annotation.elementtype; นำเข้า java.lang.annotation.retention; นำเข้า java.lang.annotation.retentionpolicy; นำเข้า java.lang.lang.lang.lang. @Interface xxxoperatelog { / *** คำอธิบายประเภทการทำงาน* @return* / สตริง operatetypedesc () ค่าเริ่มต้น ""; / *** ประเภทการทำงาน* @return*/ Long operatetype () ค่าเริ่มต้น -1; / *** การเข้ารหัสโมดูล* @return*/ String Mousecode () ค่าเริ่มต้น "M30"; / *** ชื่อโมดูล* @return*/ สตริง mousename () ค่าเริ่มต้น "โมดูล xx"; / *** ประเภทธุรกิจ* @return*/ สตริง busstype () ค่าเริ่มต้น ""; / *** คำอธิบายประเภทธุรกิจ* @return*/ String busstypedesc () ค่าเริ่มต้น "";}สร้าง xxxoperatelogaop ใหม่ภายใต้แพ็คเกจ AOP
แพ็คเกจ **. Common.log.aop; Import **; // omit @Aspect @ComponentPublic Class XXXOperAtelogaop {@autoWired SystemlogService SystemlogService; คำขอ httpservletrequest = null; logger logger = loggerFactory.getLogger (xxxoperatelogaop.class); Threadlocal <long> time = new ThreadLocal <long> (); // id ที่ไม่ซ้ำกันใช้ในการสร้างบันทึกการทำงานที่ใช้เรียกสาธารณะคงที่สำหรับการตรวจสอบกระบวนการทางธุรกิจบันทึก threadlocal <string> tag = new ThreadLocal <String> (); // ประกาศจุดเริ่มต้นของ AOP วิธีการใด ๆ ที่ใช้ xxxoperatelog ถูกสกัดกั้น @pointcut (" @annotation (**. Common.log.annotation.xxxoperatelog)") public void log () {system.out.println ("ฉันเป็นจุดเริ่มต้น"); } / *** ตัดในทุกสถานที่ที่ทำเครื่องหมาย @log* @param joinpoint* / @before ("log ()") โมฆะสาธารณะ beforeexec (joinpoint joinpoint) {time.set (system.currentTimeMillis ()); ข้อมูล (JoinPoint); // ตั้งค่าหมายเลขประจำตัวที่ไม่ซ้ำกันของบันทึกบันทึกบันทึก (uuid.randomuuid (). toString ()); request = ((servletrequestattributes) requestcontextholder.getRequestatTributes ()). getRequest (); } @after ("log ()") โมฆะสาธารณะ afterExec (JoinPoint JoinPoint) {MethodSignature MS = (MethodSignature) JoinPoint.getSignature (); วิธีการ = ms.getMethod (); logger.debug ("tag as" + tag.get () + "method" + method.getName () + "เรียกใช้การบริโภค" + (system.currentTimeLis () - time.get ()) + "MS"); } // ในกระบวนการดำเนินการวิธีการเป้าหมายวิธีนี้จะถูกดำเนินการและการบันทึกสามารถนำไปใช้ที่นี่ @around ("log ()") วัตถุสาธารณะรอบ ๆ (ดำเนินการ joinpoint pjp) โยน {object ret = pjp.proceed (); ลอง {object [] orgs = pjp.getargs (); Systemlog Valuereturn = null; สำหรับ (int i = 0; i <orgs.length; i ++) {ถ้า (orgs [i] instanceof systemlog) {valuereturn = (systemlog) orgs [i]; }} if (valuereturn == null) {valuereturn = new Systemlog (); } if (valuereturn! = null && คำขอ! = null) {methodSignature ms = (methodSignature) pjp.getSignature (); วิธีการ = ms.getMethod (); // รับข้อมูลบันทึกการดำเนินการของคำอธิบายประกอบ xxxoperatelog log = method.getAnnotation (xxxoperatelog.class); String BusinessType = log.busStype (); String BusinessDesc = log.busstypedesc (); hashmap requestmap = servletutils.getparameterstohashmap (คำขอ); // ค้นหาประเภทธุรกิจจากพารามิเตอร์ถ้า (BusinessType.equals ("")) {Object ObjbusinessType = requestMap.get ("Business_Type"); BusinessType = objbusinessType == null? "": objbusinesstype.toString (); } // พบประเภทธุรกิจจากแบบฟอร์มแอปพลิเคชันสำหรับวัตถุผลการดำเนินการใช้งาน = request.getAttribute ("ใช้"); if (ใช้! = null) {jsonObject obj = jsonfactory.tojsonabstractentity (ใช้); if (obj! = null) {valuereturn.setotherdesc ("ใช้หมายเลข:"+obj.getString ("appl_no")); if (BusinessType.equals ("")) {BusinessType = obj.getString ("Business_type"); }}} // การค้นหาประเภทธุรกิจจากพารามิเตอร์กระบวนการดำเนินการของวิธีการ (โดยปกติจะตั้งค่าด้วยตนเอง) ถ้า (BusinessType.equals ("")) {BusinessType = (String) Request.getAttribute ("Business_type"); BusinessType = BusinessType == null? "": BusinessType; } if (! BusinessType.equals ("") && businessDesc.equals ("")) {businessDesc = xxxsysconstant.business_type.getName (BusinessType); } valuereturn.setBusStype (xxxsysconstant.business_type.getNumber (BusinessType)); Valuereturn.setBusStypedesc (BusinessDesc); Valuereturn.setMoudleCode (log.moudleCode ()); Valuereturn.setMoudLename (log.moudlename ()); Valuereturn.setoperateresult (xxxsysconstant.yesorno.yes); Valuereturn.setOperAtetype (log.operatetype ()); valuereturn.setInputUserId (((userContext) webUtils.getSessionAttribute (คำขอ, "xxxuserContext")). getSysuser (). getId ()); Valuereturn.setOperAtetypedesc (log.operatetypedesc ()); Valuereturn.setRequestip (getRemoteHost (คำขอ)); Valuereturn.setRequesturl (request.getRequesturi ()); Valuereturn.setServerip (request.getLocaladdr ()); Valuereturn.setuids (tag.get ()); // บันทึกการดำเนินการ log systemlogservice.savesystemlog (valuereturn); } else {logger.info ("อย่าบันทึกข้อมูลบันทึก"); } // บันทึกผลการดำเนินการ} catch (Exception e) {E.printStackTrace (); } return return; } // บันทึกบันทึกข้อยกเว้น @afterthrowing (pointcut = "log ()", การขว้าง = "e") โมฆะสาธารณะ doafterthrowing (joinpoint joinpoint, throwable e) {ลอง {info (joinpoint); Object [] orgs = joinpoint.getargs (); Systemlog Valuereturn = null; สำหรับ (int i = 0; i <orgs.length; i ++) {ถ้า (orgs [i] instanceof systemlog) {valuereturn = (systemlog) orgs [i]; }} if (valuereturn == null) {valuereturn = new Systemlog (); } if (valuereturn! = null && คำขอ! = null) {methodSignature ms = (methodSignature) joinpoint.getSignature (); วิธีการ = ms.getMethod (); xxxoperatelog log = method.getannotation (xxxoperatelog.class); String BusinessType = log.busStype (); String BusinessDesc = log.busstypedesc (); if (BusinessType.equals ("")) {Object objbusinessType = servletutils.getParameterStoHashMap (คำขอ) .get ("Business_type"); BusinessType = objbusinessType == null? "": objbusinesstype.toString (); BusinessDesc = xxxsysconstant.business_type.getName (BusinessType); } valuereturn.setBusStype (xxxsysconstant.business_type.getNumber (BusinessType)); Valuereturn.setBusStypedesc (BusinessDesc); Valuereturn.setMoudleCode (log.moudleCode ()); Valuereturn.setMoudLename (log.moudlename ()); Valuereturn.setOperAtetype (log.operatetype ()); Valuereturn.setOperAtetypedesc (log.operatetypedesc ()); valuereturn.setInputUserId (((userContext) webUtils.getSessionAttribute (คำขอ, "xxxuserContext")). getSysuser (). getId ()); Valuereturn.setoperateresult (xxxsysconstant.yesorno.no); string errmes = e.getMessage (); ถ้า (errmes! = null && errmes.length ()> 800) {errmes = errmes.substring (0, 800); } valuereturn.seterRorMessage (errmes); Valuereturn.setRequestip (getRemoteHost (คำขอ)); Valuereturn.setRequesturl (request.getRequesturi ()); Valuereturn.setServerip (request.getLocaladdr ()); Valuereturn.setuids (tag.get ()); SystemLogService.Savesystemlog (Valuereturn); } else {logger.info ("ไม่มีการบันทึกข้อมูลบันทึก"); }} catch (Exception e1) {e1.printstackTrace (); }} ข้อมูลโมฆะส่วนตัว (JoinPoint JoinPoint) { logger.debug ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ OS = joinpoint.getArgs (); logger.debug ("sourcelocation:/t" + joinpoint.getSourceLocation ()); logger.debug ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - request.getheader ("proxy-client-ip"); ip = request.getRemoteaddr ();} return ip.equals ("0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 1")?แก้ไขไฟล์การกำหนดค่า Spring-MVC.XML และเพิ่มการกำหนดค่าต่อไปนี้
<!-เปิดการสกัดกั้น AOP-> <AOP: APISICESJ-AUTOPROXY PROXY-TARGET-CLASS = "True" /> <MVC: คำอธิบายประกอบที่ขับเคลื่อนด้วย /> <!-กำหนดขอบเขตของคำอธิบายฤดูใบไม้ผลิถั่ว-> <บริบท: ส่วนประกอบ expression = "org.springframework.stereotype.controller"/> </บริบท: Component-Scan>
ควรสังเกตว่าการกำหนดค่าข้างต้นจะต้องวางไว้ในไฟล์ XML เดียวกันไม่ว่าจะเป็น Spring-MVC.XML หรือ Spring-context.xml มิฉะนั้นอาจไม่มีผลและยังไม่พบเหตุผล
การใช้คำอธิบายประกอบ
@xxxoperatelog (busstype = xxxsysconstant.business_type.yyyy, busstypedesc = "ประเภทธุรกิจคำอธิบาย", operatetype = xxxsysconstant.logoperatepe.query, operatetypedesc = "คำอธิบาย" "/**/**/queryxxxx4datagrid.json", method = requestmethod.post) โมฆะสาธารณะ queryxxxxx4datagrid (httpservletrequest Request, httpservletResponse Arg1, โมเดลรุ่น, นักเขียนนักเขียน) { logger.info ("============================================================================================================ Writer) {logger.info ("================================= HttpservletResponse Arg1, โมเดลโมเดล, นักเขียนนักเขียน) {logger.info (" ==================================== นักเขียน) {logger.info ("====================== httpservletResponse arg1, โมเดลโมเดล, นักเขียนนักเขียน) {logger.info (" ===================== httpsertresters logger.info ("==================== httpservletResponse arg1, โมเดลโมเดล, นักเขียนนักเขียน) {logger.info (" ===================== HttpservletResponse logger.info ("====================== httpservletResponse arg1, โมเดลโมเดล,คำอธิบายประกอบที่กำหนดเอง SpringMVC ด้านบนวิธีการใช้ AOP เพื่อตระหนักถึงการบันทึกเป็นเนื้อหาทั้งหมดที่ฉันแบ่งปันกับคุณ ฉันหวังว่าคุณจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น