IDEA SpringBoot 添加AOP(面向切面编程)

1.AOP(Aspect Oriented Programming)相关术语

①增强处理(Advice)
  • 前置增强
  • 后置增强
  • 环绕增强
  • 异常抛出增强
  • 最终增强等类型
②切入点(Pointcut)
  • 何时执行切面类代码
③连接点(Joint Point)
④切面(Aspect)
  • 非核心代码,如日志,异常等,单独取出作为切面类
⑤目标对象(Target Object)
  • Service服务层
⑥AOP代理(AOP Proxy)
⑦织入(Weaving)
  • 即往目标对象中注入切面类的说法

2.关于execution表达式的规定,来源于Spring官网

The following examples show some common pointcut expressions:
下面的示例显示了一些常见的切入点表达式:

The execution of any public method:
执行任何公共方法:

execution(public * *(..))

The execution of any method with a name that begins with set:
执行名称以set开头的任何方法:

execution(* set*(..))

The execution of any method defined by the AccountService interface:
AccountService接口定义的任何方法的执行:

execution(* com.xyz.service.AccountService.*(..))

The execution of any method defined in the service package:
执行服务包中定义的任何方法:

execution(* com.xyz.service.*.*(..))

The execution of any method defined in the service package or one of its sub-packages:
执行服务包或其子包之一中定义的任何方法:

execution(* com.xyz.service..*.*(..))

3.SpringBoot实现AOP

①在SpringBoot工程基础上添加Aop依赖
	<!--AOP面向切面编程-->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
②创建Service类,即目标对象

IUserService接口类

	public interface IUserService {
    /**
     * 用于测试前置通知,后置通知及环绕通知
     */
    void aopTest();

    /**
     * 用户测试后置通知返回值
     * @return
     */
    String aopReturning();

    /**
     * 用于测试后置通知异常返回
     * @throws Exception
     */
    void aopExceptionTest() throws Exception;
}

UserServiceImpl实现类

	@Override
    public void aopTest(){
        System.out.println("用来测试AOP功能");
    }

    @Override
    public String aopReturning() {
        return "返回值";
    }

    @Override
    public void aopExceptionTest() throws Exception {
        throw new Exception("AOP后置异常测试");
    }
③创建AOP类
@Component
@Aspect
public class MyAspect {
    /**
     * 切点前置通知
     */
    @Before("execution(* com.cx.springboot.service..*.*(..))")
    public void before() {
        System.out.println("前置通知");
    }

    /**
     * 切点后置通知
     */
    @After("execution(* com.cx.springboot.service..*.*(..))")
    public void after() {
        System.out.println("后置通知");
    }

    /**
     * 切点环绕通知
     *
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.cx.springboot.service..*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("====环绕开始====");
        // 切面中获取到调用方法名
        String methodName = proceedingJoinPoint.getSignature().getName();
        // 切面中获取到调用方法的参数
        Object[] args = proceedingJoinPoint.getArgs();

        // 执行调用方法
        Object result = proceedingJoinPoint.proceed();

        System.out.println("方法名:" + methodName);
        System.out.println("方法参数:" + args.toString());

        System.out.println("====环绕结束====");

        return result;
    }

    /**
     * 切点后置通知返回值
     *
     * @param joinPoint
     * @param result
     */
    @AfterReturning(pointcut = "execution(* com.cx.springboot.service..*.*(..))", returning = "result")
    public void afterReturning(JoinPoint joinPoint, String result) {
        System.out.println("后置通知返回值:" + result);
    }

    /**
     * 切点后置通知异常返回
     *
     * @param joinPoint
     * @param exception
     */
    @AfterThrowing(pointcut = "execution(* com.cx.springboot.service..*.*(..))",throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint, Exception exception) {
        System.out.println("后置异常返回:" + exception.getMessage());
    }
    
}	
④创建测试AOP类
	@SpringBootTest
	public class UserTest {
    	@Autowired
    	IUserService userService;
    	@Test
    	public void test01(){
        	userService.aopTest();
    	}

    	@Test
    	public void test02(){
        	userService.aopReturning();
    	}

    	@Test
    	public void test03(){
        	try {
            	userService.aopExceptionTest();
        	} catch (Exception e) {
            	System.out.println("异常结束");
        	}
		}
⑤测试结果
  • 前置通知
    在这里插入图片描述
  • 后置通知
    -
  • 环绕通知
    在这里插入图片描述
  • 后置通知返回值
    -
  • 后置通知异常返回
    在这里插入图片描述
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot实现AOP面向切面编程的具体方法如下: 1. 首先,你需要在项目的pom.xml文件中添加spring-boot-starter-aop依赖。可以参考以下代码: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 然后,你需要编写一个用于拦截的bean。这个bean将包含你想要在目标方法执行前后执行的逻辑。你可以使用注解或者编程方式来定义切面。例如,你可以使用@Aspect注解来定义一个切面,然后在切面的方法上使用@Before、@After等注解来定义具体的拦截行为。 3. 接下来,你需要将切面应用到目标对象上,创建代理对象。这个过程称为织入(Weaving)。在Spring Boot中,你可以使用@EnableAspectJAutoProxy注解来启用自动代理,它会根据切面定义自动创建代理对象。 总而言之,Spring Boot实现AOP面向切面编程的具体方法包括:添加依赖、编写用于拦截的bean,以及启用自动代理。这样就能实现在目标方法执行前后执行特定逻辑的效果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合aop面向切面编程过程解析](https://download.csdn.net/download/weixin_38689551/12743012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot实现AOP面向切面编程](https://blog.csdn.net/weixin_52536274/article/details/130375560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ww空ww

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值