Baru -baru ini, proyek perlu membuat fungsi logging, yang saya gunakan Metode Anotasi AOP Spring untuk diimplementasikan.
Buat Anotasi Log
Paket com.wyj.annotation; impor java.lang.annotation.dokumen; impor java.lang.annotation.elementType; impor java.lang.annotation.retention; import java.lang.annotation.Retensionpolicy; impor java.lang.annotation.target; @Date: 26 Agustus 2016 jam 8:25:35 pm */@target (elementType.method) @retention (retentionpolicy.runtime) @documentedpublic @interface syslog {string action () default ""; // action}Buat kelas pemberitahuan facet
Catat nama metode, parameter, dan waktu yang dihabiskan untuk operasi, menggunakan pemberitahuan surround
Paket com.wyj.aspect; import java.lang.reflect.method; impor org.aspectj.lang.proedingjoinpoint; impor org.aspectj.lang.annotation.around; import org.aspectj.lang.annotation.aspect; impor org.aspecpy.annotation org.aspectj.lang.reflect.methodsignature; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.stereotype.sponent; import com.wyj.annotation.syslog; impor com.wyj.entity.sysyity.sysyity; com.wyj.service.SysLogService;/** * Log Section Notification* * * @author: WangYuanJun * @date: August 26, 2016 at 10:28:57 pm */@Aspect@Componentpublic class SysLogAspect { @Autowired private SysLogService sysLogService; /** * Point of entry*/ @Pointcut("@annotation(com.wyj.annotation.SysLog)") public void pointCut() {} /** * Surround notification* * @param joinPoint * @return * @throws Throwable */ @Around("pointCut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { // Start time long beginTime = System.currentTimemillis (); // Jalankan hasil objek metode target = joinpoint.proed (); // Waktu Eksekusi (milidetik) lama = System.currentTimemillis () - BeginTime; // simpan log savesyslog (goinpoint, waktu); hasil pengembalian; } / ** * Simpan log * * @param joinpoint * @param Time * / private void saveSySlog (ProsidingjOintPoint gabungan, lama) {MethodyInchature Signature = (MethodyIchature) goinpoint.getSignature (); Metode metode = Signature.getMethod (); Syslogentity syslogentity = Syslogentity baru (); Syslog syslog = method.getannotation (syslog.class); if (syslog! = null) {// deskripsi pada anotasi syslogentity.setOperation (syslog.action ()); } // Dapatkan target className = joinpoint.getTarget (). GetClass (). GetName (); // Dapatkan Metode Nama String MethodName = Signature.getName (); Syslogentity.setMethod (className + "." + MethodName + "()"); // objek parameter yang diminta [] args = joinpoint.getArgs (); if (args! = null && args.length! = 0 && args [0]! = null) {syslogentity.setParams (args [0] .toString ()); } syslogentity.settime (waktu); // simpan sistem log syslogservice.save (syslogentity); }}Pindai dan mulai anotasi AOP
Penerapan anotasi log
Memengaruhi
Metode konfigurasi anotasi AspectJ Spring AOP di atas untuk mengimplementasikan manajemen log adalah semua konten yang saya bagikan dengan Anda. Saya harap Anda dapat memberi Anda referensi dan saya harap Anda dapat mendukung wulin.com lebih lanjut.