最近、プロジェクトはロギング関数を作成する必要があります。これは、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; * @date:2016年8月26日午後8時25分35秒 */@ターゲット(elementType.Method)@retention(retentionPolicy.runtime)@documentedpublic @interface syslog {string action()default ""; // action}ファセット通知クラスを作成します
サラウンド通知を使用して、操作に費やされたメソッド名、パラメーター、および時間を記録します
パッケージcom.wyj.aspect; import java.lang.refllect.method; import org.aspectj.lang.proceedjoinpoint; Import org.aspectj.lang.annotation.around; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.point.point.pointcut; org.aspectj.lang.reflect.methodsignature; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.stereotype.component; Import com.wyj.annotation.syslog; Import com.wyj.entity.syslogentity; Import; com.wyj.service.syslogservice;/** * logセクション通知 * * * @author:wangyuanjun * @date:2016年8月26日10:28:57 pm */ @componentpublic class syslogaspect {@autowired private syslogservice syslogservice; / ** *エントリポイント */ @pointCut( " @annotation(com.wyj.annotation.syslog)")public void pointcut(){}/ ** * * @param joinpoint * @throws throwable */ @around( "pointcut()" System.CurrentTimemillis(); //ターゲットメソッドオブジェクトresult = joinpoint.proceed();を実行する//実行時間(ミリ秒)long time = system.currenttimemillis() - begintime; // log savesyslog(joinpoint、time)を保存します。返品結果; } / ** * logを保存 * * @param joinpoint * @param time * / private void savesyslog(proceedingjoinpoint joinpoint、long time){methodingignature signature =(methodignature)joinpoint.getSignature();メソッドメソッド= signature.getMethod(); syslogentity syslogentity = new syslogentity(); syslog syslog = method.getannotation(syslog.class); if(syslog!= null){//注釈の説明syslogentity.setoperation(syslog.action()); } // get target classname = joinpoint.getTarget()。getClass()。getName(); //メソッド名を取得します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(time); //システムログを保存syslogservice.save(syslogentity); }}スキャンしてAOPアノテーションを開始します
ログアノテーションの適用
効果
上記のSpring AOPのAspectJアノテーション構成メソッドログ管理を実装することは、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。