Spring Aop日志简易实例

目录

介绍:

1、建立自定义日志注解

2、创建要收集日志的方法

3、创建AOP

4、加入扫描项

5、启动项目运行即可


 


介绍:

AOP(Aspect-Oriented Programming:面向切面编程)能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。

 

1、建立自定义日志注解

package com.demo.gyw.java.custom_annotation;

import java.lang.annotation.*;

/**
 * @Author GouYaoWen
 * @Description 日志注解
 * @Date Create in 10:52 2019/12/3
 */
@Target({
        ElementType.METHOD
})//注解作用域(类)
@Retention(RetentionPolicy.RUNTIME)//注解生命周期(运行时存在,可以通过反射读取)
@Documented //生存JavaDoc时会包含注解
public @interface LogAnnotation {
    String value() default "";

    String author();

    String desc();
}

2、创建要收集日志的方法

package com.demo.gyw.spring.aop;

import com.demo.gyw.java.custom_annotation.LogAnnotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author GouYaoWen
 * @Description 测试控制器
 * @Date Create in 10:47 2019/12/3
 */
@RestController
@RequestMapping("/aop")
public class AopController {

    @RequestMapping("/logAnnotation")
    @LogAnnotation(author = "gyw",desc = "欢迎")//日志注解
    public String welcome() {
        return "欢迎您使用Aop测试工具!";
    }

    @RequestMapping("/throwLogAnnotation")
    @LogAnnotation(author = "gyw",desc = "异常欢迎")//日志注解
    public void throwWelcome() {
        throw new ArithmeticException("这是一个异常!");
    }
}

3、创建AOP

package com.demo.gyw.spring.aop;

import com.demo.gyw.java.custom_annotation.LogAnnotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author GouYaoWen
 * @Description 日志切面
 * @Pointcut 切入点-->那些要使用AOP
 *
 * @Before 前置通知(Before Advice)
 * @AfterReturning 返回之后通知(After Retuning Advice):
 * @AfterThrowing 抛出(异常)后执行通知(After Throwing Advice):
 * @After 后置通知(After Advice):
 * @Around 围绕通知(Around Advice):
 * @Date Create in 10:37 2019/12/3
**/
@Component//把类交给spring管理
@Aspect//面向切面
public class LogAop{
    /**
     * 切入点
     * 扫描那些
     */
    @Pointcut(value="@annotation(com.demo.gyw.java.custom_annotation.LogAnnotation)")
    public void point(){
        System.out.println("point()");
    }

    @Before(value="point()")
    public void before(JoinPoint point){
        System.out.println("Before begin!");
        //获取方法
        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        //方法
        boolean isExist = method.isAnnotationPresent(LogAnnotation.class);
        if (isExist) {
            LogAnnotation u = method.getAnnotation(LogAnnotation.class);
            System.out.println("作者:" + u.author());
            System.out.println("描述:" + u.desc());
        }else{
            System.out.println("方法"+method.getName()+"不存在该注解!");
        }
    }

    @AfterThrowing("point()")
    public void afterThrowing(){
        System.out.println("afterThrowing 异常!");
    }

    @After(value = "point()")
    public void after(){
        System.out.println("after commit!");
    }

    @AfterReturning(value = "point()")
    public void afterReturning(){
        System.out.println("AfterReturning commit!");
    }

    @Around("point()")
    public String around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("around begin!");
        Object obj = joinPoint.proceed();
        System.out.println("around commit!");
        return obj.toString();
    }
}
//SpringBoot中添加@ComponentScans(value = { @ComponentScan(basePackageClasses= LogAop.class)})
/*顺序
around begin!
Before begin!
作者:gyw
描述:欢迎
around commit!
after commit!
AfterReturning commit!*/

4、加入扫描项

@ComponentScans(value = { @ComponentScan(basePackageClasses= LogAop.class)})//面向切面类扫描

5、启动项目运行即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值