SpringBoot中使用AOP

一、什么是AOP

AOP(Aspect Oriented Programming):面向切面编程,通过预编译的方式和运行期动态代理实现程序功能的统一维护的一种技术。

二、使用步骤

在pom中添加一下依赖:

<!--    <parent>-->
<!--        <groupId>org.springframework.boot</groupId>-->
<!--        <artifactId>spring-boot-starter-parent</artifactId>-->
<!--        <version>2.3.3.RELEASE</version>-->
<!--    </parent>-->
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
    </dependency>

方式一:
1.编写切面类


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class MyAspect {
    Logger logger = LoggerFactory.getLogger(MyAspect.class);

    @Pointcut("execution(public * com.learn.demo.mylearnaop.service.impl.CalcServiceImpl.*(..))")
    public void myPointCut(){

    }

    @Before("myPointCut()")
    public void beforeNotify() {
        logger.info("**********@Before前置通知MyAspect");
    }
    @After("myPointCut()")
    public void afterNotify() {
        logger.info("**********@Afte后置通知MyAspect");
    }

    @AfterReturning("myPointCut()")
    public void afterReturningNotify() {
        logger.info("**********@afterReturning返回后通知MyAspect");
    }

    @AfterThrowing("myPointCut()")
    public void AfterThrowingNotify() {
        logger.info("**********@AfterThrowing异常通知MyAspect");
    }

    @Around("myPointCut()")
    public Object AroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = proceedingJoinPoint.proceed();
        logger.info("**********@Around环绕通知MyAspect");
        return proceed;
    }
}

2.实现类

import com.learn.demo.mylearnaop.service.CalcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class CalcServiceImpl implements CalcService {
    private static Logger logger = LoggerFactory.getLogger(CalcServiceImpl.class);

    @Override
    public void testAop() {
        logger.info("=========testAop=============");
    }
}

3.单元测试


@SpringBootTest
@RunWith(SpringRunner.class)
public class MylearnaopApplicationTests {

    @Resource
    private CalcService calcService;

   
    @Test
    public void testAop() {
        calcService.testAop();
    }

}

4.测试结果
在这里插入图片描述
方式二:
1.实现自定义注解


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value() default "MyAnnotation注解";
}

2.编写切面类


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class MyAspect {
    Logger logger = LoggerFactory.getLogger(MyAspect.class);

    @Pointcut("@annotation(com.learn.demo.mylearnaop.annotation.LogAnnotation)")
//    @Pointcut("execution(public * com.learn.demo.mylearnaop.service.impl.CalcServiceImpl.*(..))")
    public void myPointCut() {

    }

    @Before("myPointCut()")
    public void beforeNotify() {
        logger.info("**********@Before前置通知MyAspect");
    }

    @After("myPointCut()")
    public void afterNotify() {
        logger.info("**********@Afte后置通知MyAspect");
    }

    @AfterReturning("myPointCut()")
    public void afterReturningNotify() {
        logger.info("**********@afterReturning返回后通知MyAspect");
    }

    @AfterThrowing("myPointCut()")
    public void AfterThrowingNotify() {
        logger.info("**********@AfterThrowing异常通知MyAspect");
    }

    @Around("myPointCut()")
    public Object AroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = proceedingJoinPoint.proceed();
        logger.info("**********@Around环绕通知MyAspect");
        return proceed;
    }
}

3.使用注解


import com.learn.demo.mylearnaop.annotation.MyAnnotation;
import com.learn.demo.mylearnaop.service.CalcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class CalcServiceImpl implements CalcService {
    private static Logger logger = LoggerFactory.getLogger(CalcServiceImpl.class);


    @Override
    @MyAnnotation
    public void testAop() {
        logger.info("=========testAop=============");
    }
}

4.单元测试


import com.learn.demo.mylearnaop.service.CalcService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MylearnaopApplicationTests {

    @Resource
    private CalcService calcService;

  
    @Test
    public void testAop() {
        calcService.testAop();
    }

}

5.测试结果

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值