ホームページ  >  記事  >  Java  >  Spring 構成の単純なログの注釈

Spring 構成の単純なログの注釈

巴扎黑
巴扎黑オリジナル
2017-07-20 10:32:101487ブラウズ

Java は jdk1.5 でアノテーションを導入し、Spring フレームワークでも Java アノテーションが最大限に活用されています。

以下では、Spring のカスタム アノテーションの簡単なプロセスについて説明します。これには、Spring フレームワークの AOP (アスペクト指向プログラミング) 関連の概念が含まれます。
Java アノテーションについて知らない場合は、まず Java カスタム アノテーションを理解することができます: Java カスタム アノテーション

1. カスタム アノテーションを作成します

requestUrl は私たちがカスタマイズしたパラメーターです

package com.sam.annotation;

import java.lang.annotation.*;

/**
 * @author sam
 * @since 2017/7/13
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyLog {

    String requestUrl();

}

2. ここで使用します。 SpringのAOP(アスペクト指向プログラミング)機能

@Aspectアノテーションでこのクラスをアスペクトクラスにする

@Pointcutでエントリポイントを指定する ここで指定するエントリポイントはMyLogアノテーションタイプで、@で修飾されたメソッドです。 MyLog 注釈、このエントリ ポイントを入力します。
    @Before Pre-notification: 特定の接続ポイントより前に実行される通知ですが、この通知は接続ポイントより前の実行プロセスを防ぐことはできません (例外をスローしない限り)。
  • @Around Around 通知: メソッド実行の前後に point.proceed(); を実行して結果を返す必要があります。
  • @AfterReturning 事後通知: 接続ポイントが正常に完了した後に実行される通知。たとえば、メソッドは例外をスローせず、正常に戻ります。
  • @AfterThrowing 例外通知: メソッドが例外をスローして終了するときに実行される通知。
  • @After Post 通知: 接続ポイントが正常に完了した後に実行される通知。たとえば、メソッドは例外をスローせず、通常どおりに戻ります。
  • package com.sam.annotation;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    
    /**
     * @author sam
     * @since 2017/7/13
     */
    @Aspect //AOP 切面
    @Component
    public class MyLogAspect {
    
    
        //切入点
        @Pointcut(value = "@annotation(com.sam.annotation.MyLog)")
        private void pointcut() {
    
        }
    
    
        /**
         * 在方法执行前后
         *
         * @param point
         * @param myLog
         * @return
         */
        @Around(value = "pointcut() && @annotation(myLog)")
        public Object around(ProceedingJoinPoint point, MyLog myLog) {
    
            System.out.println("++++执行了around方法++++");
    
            String requestUrl = myLog.requestUrl();
    
            //拦截的类名
            Class clazz = point.getTarget().getClass();
            //拦截的方法
            Method method = ((MethodSignature) point.getSignature()).getMethod();
    
            System.out.println("执行了 类:" + clazz + " 方法:" + method + " 自定义请求地址:" + requestUrl);
    
            try {
                return point.proceed(); //执行程序
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                return throwable.getMessage();
            }
        }
    
        /**
         * 方法执行后
         *
         * @param joinPoint
         * @param myLog
         * @param result
         * @return
         */
        @AfterReturning(value = "pointcut() && @annotation(myLog)", returning = "result")
        public Object afterReturning(JoinPoint joinPoint, MyLog myLog, Object result) {
    
            System.out.println("++++执行了afterReturning方法++++");
    
            System.out.println("执行结果:" + result);
    
            return result;
        }
    
        /**
         * 方法执行后 并抛出异常
         *
         * @param joinPoint
         * @param myLog
         * @param ex
         */
        @AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex")
        public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) {
            System.out.println("++++执行了afterThrowing方法++++");
            System.out.println("请求:" + myLog.requestUrl() + " 出现异常");
        }
    
    }
  • 3. カスタムアノテーションを使用します

コントローラーで直接アノテーション @MyLog

package com.sam.controller;

import com.sam.annotation.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sam
 * @since 2017/7/13
 */
@RestController
@RequestMapping(value = "/index")
public class IndexController {

    @MyLog(requestUrl = "/index请求")
    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "index";
    }
}

を使用し、http://localhost:8080/index にアクセスします

結果

++++执行了around方法++++
执行了 类:class com.yfs.controller.IndexController 方法:public java.lang.String com.yfs.controller.IndexController.index() 自定义请求地址:/index请求
++++执行了afterReturning方法++++
执行结果:index

上記は説明ですself の Spring 実装 アノテーションを定義する単純なプロセスには、より多くのカスタム操作が必要であり、アスペクト クラスでプログラムする必要があります。

以上がSpring 構成の単純なログの注釈の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。