최근에 프로젝트는 로깅 기능을 만들어야하며 Spring AOP 주석 방법을 사용하여 구현합니다.
로그 주석을 만듭니다
패키지 com.wyj.annotation; import java.lang.annotation.documented; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target * * ontoration * * @wangyat : wangy a wangy : * @Date : 2016 년 8 월 26 일 오후 8:25:35 pm */@target (eleMentType.Method) @retention (rendentionpolicy.runtime) @documentedPublic @Interface syslog {String action () default "; // action}패싯 알림 클래스를 만듭니다
서라운드 알림을 사용하여 메소드 이름, 매개 변수 및 작업에 소요되는 시간을 기록하십시오.
패키지 com.wyj.aspect; import java.lang.reflect.method; import org.aspectj.lang.proeessingjoinpoint; import org.aspectj.lang.annotation.around; import org.aspectj.lang.annotation.aspect; import org.aspect.lang.annot.annot org.aspectj.lang.reflect.methodsignature; import org.springframework.beans.beans.annotation.autowired; import org.spramframework.stereotyp.component; import com.wyj.annotation.syslog; import com.wyj.entity.syslogentity; com.wyj.service.syslogservice;/** * 로그 섹션 알림 * * * @author : wangyuanjun * @date : 2016 년 8 월 26 일 오후 10:28:57 pm */ @componentpublic class syslogaspect {@autowired private syslogserviceservice; / ** * 입력 지점 */ @pointcut ( " @annotation (com.wyj.annotation.syslog)") public void pointcut () {}/ ** * 서라운드 알림 * * @param joinpoint * @return * @throws trowable */ @around ( "pointCut ()"공개 객체 (ProceedingTintime joppoint). System.CurrentTimeMillis (); // 대상 메소드 실행 객체 result = joinpoint.proceed (); // 실행 시간 (밀리 초) 긴 시간 = System.CurrentTimeMillis () - BegIntime; // 로그 SAVESYSLOG (joinpoint, time)를 저장합니다. 반환 결과; } / ** * 로그를 저장 * * @param joinpoint * @param time * / private void savesyslog (proceedingjoinpoint 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 ()); } // target className을 가져옵니다. // 메소드 이름 String methodName = 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 주석을 스캔하고 시작하십시오
로그 주석의 적용
효과
로그 관리를 구현하기위한 위의 Spring AOP의 Spell 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.