Springboot的Aop实现

## 只记录使用,无概念

一、创建项目

创建springboot项目,勾选web和aop,项目结构如图
在这里插入图片描述

二、编写代码

(1)业务逻辑代码,UserService和UserServiceImpl

package com.zjl.aop.service;

public interface UserService {

    /**
     * 添加
     *
     * @return int
     */
    int add();

    /**
     * 更新
     *
     * @return int
     */
    int update();

    /**
     * 删除
     *
     * @return int
     */
    int delete();

    /**
     * 选择
     *
     * @return int
     */
    void select();

}

package com.zjl.aop.service.impl;

import com.zjl.aop.service.UserService;
import org.springframework.stereotype.Service;

/**
 * 用户服务impl
 * @author zjl
 * @date 2021/06/07
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public int add() {
        System.out.println("添加用户方法");
        return 0;
    }

    @Override
    public int update() {
        System.out.println("修改用户方法");
        return 0;
    }

    @Override
    public int delete() {
        System.out.println("删除用户方法");
        return 10;
    }

    @Override
    public void select() {
        System.out.println("查询用户方法");
        throw new NullPointerException();
    }


}

(2)切面类代码

package com.zjl.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 日志切面
 * 1.Before  前置通知
 * 2.After   后置通知
 * 3.Around  环绕通知
 * 4.AfterReturning  返回通知
 * 3.AfterThrowing   异常通知
 * @author zjl
 * @date 2021/06/07
 */
@Aspect
@Component
public class LogAspect {

    @Pointcut("execution(* com.zjl.aop.controller.*.*(..))")
    public void controller(){
    }

    @Before("controller()")
    public void before2(JoinPoint point){
        String name=point.getSignature().getName();
        System.out.println(name+"方法的controller前置通知");
    }

    @Pointcut("execution(* com.zjl.aop.service.*.*(..))")
    public void service(){
    }

    @Before("service()")
    public void before(JoinPoint point){
        String name=point.getSignature().getName();
        System.out.println(name+"方法的service前置通知");
    }

    @After("service()")
    public void after(JoinPoint point){
        String name=point.getSignature().getName();
        System.out.println(name+"方法的service后置通知");
    }


    @Around("execution(* com.zjl.aop.service.*.update(..))")
    public int around(ProceedingJoinPoint point) throws Throwable {
        String name=point.getSignature().getName();
        System.out.println(name+"方法的service前置环绕通知");
        point.proceed();
        System.out.println(name+"方法的service后置环绕通知");
        return 0;
    }

    @AfterReturning(pointcut = "execution(* com.zjl.aop.service.*.delete(..))",returning = "result")
    public void AfterReturning(JoinPoint point,Object result) throws Throwable {
        String name=point.getSignature().getName();
        System.out.println(name+"方法的service返回通知, 返回结果为: "+result);
    }

    @AfterThrowing(pointcut = "execution(* com.zjl.aop.service.*.select(..))",throwing = "e")
    public void AfterThrowing(JoinPoint point,Throwable e){
        String name=point.getSignature().getName();
        System.out.println(name+"方法的service异常通知, 异常为: "+e.getMessage());
    }


}

(3)测试类代码

package com.zjl.aop;

import com.zjl.aop.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AopApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    void testAdd() {
        userService.add();
    }

    @Test
    void testUpdate() {
        userService.update();
    }

    @Test
    void testDelete() {
        userService.delete();
    }

    @Test
    void testSelect() {
        userService.select();
    }

}

(4)测试结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值