Home  >  Article  >  Java  >  Spring configuration simple log annotation

Spring configuration simple log annotation

巴扎黑
巴扎黑Original
2017-07-20 10:32:101487browse

java introduced annotations in jdk1.5, and the spring framework also makes full use of java annotations.

The following will explain the simple process of custom annotations in Spring, which will involve AOP (aspect-oriented programming) related concepts in the spring framework.
If you don’t know java annotations, you can first understand java custom annotations: Java custom annotations

1. Create custom annotations

requestUrl is a parameter customized for us

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. Parsing annotations

The AOP (aspect-oriented programming) feature of spring is used here

The @Aspect annotation is used to make this class an aspect class
Specify the entry point through @Pointcut. The entry point specified here is the MyLog annotation type, that is, the method modified by the @MyLog annotation enters the entry point.
  • @Before Pre-notification: A notification that is executed before a certain connection point, but this notification cannot prevent the execution process before the connection point (unless it throws an exception).

  • @Around Surround notification: You can implement operations before and after method execution. You need to execute point.proceed(); within the method and return the result.

  • @AfterReturning Post-notification: Notification executed after a connection point is completed normally: for example, a method does not throw any exception and returns normally.

  • @AfterThrowing Exception notification: Notification executed when the method throws an exception and exits.

  • @After post notification: a notification executed after a connection point is completed normally: for example, a method does not throw any exception and returns normally.

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. Use custom annotations

Use the annotation @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";
    }
}

directly in the controller to start the project and visit http://localhost: 8080/index

Result

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

The above explains the simple process of implementing custom annotations in Spring. If you need to do more custom operations, you need to program in the aspect class. .

The above is the detailed content of Spring configuration simple log annotation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn