เมื่อเร็ว ๆ นี้โครงการจำเป็นต้องทำฟังก์ชั่นการบันทึกซึ่งฉันใช้วิธีการอธิบายประกอบฤดูใบไม้ผลิ AOP เพื่อนำไปใช้
สร้างคำอธิบายประกอบบันทึก
แพ็คเกจ com.wyj.annotation; นำเข้า java.lang.annotation.documented; นำเข้า java.lang.annotation.ElementType; นำเข้า java.lang.annotation.Retention; นำเข้า java.lang.annotation.retentionpolicy; Wangyuanjun * @date: 26 สิงหาคม 2016 เวลา 8:25:35 น. */@Target (ElementType.method) @retention (RetentionPolicy.runtime) @documentedPublic @interface syslog {String Action ()สร้างคลาสการแจ้งเตือนด้าน
บันทึกชื่อเมธอดพารามิเตอร์และเวลาที่ใช้ในการดำเนินการโดยใช้การแจ้งเตือนรอบทิศทาง
แพ็คเกจ com.wyj.aspect; นำเข้า java.lang.reflect.method; นำเข้า org.aspectj.lang.proceedingjoinpoint; นำเข้า org.aspectj.lang.annotation.around; นำเข้า org.aspectj.lang.annotation.aspect; org.aspectj.lang.reflect.methodsignature; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.Component; นำเข้า com.wyj.annotation.syslog; com.wyj.service.syslogservice;/** * บันทึกการแจ้งเตือน * * * @author: wangyuanjun * @date: 26 สิงหาคม 2016 เวลา 10:28:57 น. */ @ @ @ @ComponentPublic / ** * จุดของรายการ */ @pointcut (" @annotation (com.wyj.annotation.syslog)") โมฆะสาธารณะ pointcut () {}/ ** * การแจ้งเตือนรอบ ๆ * * @param joinpoint * @return * @throws System.currenttimemillis (); // ดำเนินการตามวัตถุวิธีการเป้าหมายผลลัพธ์ = joinpoint.proceed (); // เวลาดำเนินการ (มิลลิวินาที) เป็นเวลานาน = System.currentTimeMillis () - Begintime; // บันทึกบันทึกบันทึก savesyslog (joinpoint เวลา); ผลการกลับมา; } / ** * บันทึกบันทึก * * @param joinpoint * @param เวลา * / โมฆะส่วนตัว savesyslog (การดำเนินการ Joinpoint joinpoint, เป็นเวลานาน) {methodSignature signature = (methodSignature) JoinPoint.getSignature (); วิธีการ = signature.getMethod (); syslogentity syslogentity = new syslogentity (); syslog syslog = method.getannotation (syslog.class); if (syslog! = null) {// คำอธิบายเกี่ยวกับคำอธิบายประกอบ syslogentity.setoperation (syslog.action ()); } // รับ className target = joinPoint.getTarget (). getClass (). getName (); // รับชื่อเมธอดสตริงเมธอดนิเมเมเมนต์ = signature.getName (); syslogentity.setMethod (className + "." + methodName + "()"); // วัตถุพารามิเตอร์ที่ร้องขอ [] args = joinpoint.getargs (); if (args! = null && args.length! = 0 && args [0]! = null) {syslogentity.setparams (args [0] .toString ()); } syslogentity.settime (เวลา); // บันทึกบันทึกระบบ syslogservice.save (syslogentity); -สแกนและเริ่มคำอธิบายประกอบ AOP
แอปพลิเคชันคำอธิบายประกอบบันทึก
ผล
วิธีการกำหนดค่าคำอธิบายประกอบ ASPECTJ ของ Spring AOP ด้านบนเพื่อใช้การจัดการบันทึกเป็นเนื้อหาทั้งหมดที่ฉันแบ่งปันกับคุณ ฉันหวังว่าคุณจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น