spring 使用aspectJ 实现aop

1.开启包扫描

2.开启aspectj

3.创建类

4.切面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.cj"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

package com.cj.aop;

import org.springframework.stereotype.Component;

@Component
public class User {
    public void add(){
        System.out.println("add...");
    }
}
package com.cj.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class UserProxy {
    @Before("execution(* com.cj.aop.User.add(..))")
    public void before(){
        System.out.println("before...");
    }
}

 

package com.cj.aop;

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

@Component
@Aspect
public class UserProxy {
    @Before("execution(* com.cj.aop.User.add(..))")
    public void before(){
        System.out.println("before...");
    }
    @After("execution(* com.cj.aop.User.add(..))")
    public void after(){
        System.out.println("After...");
    }
    @AfterReturning("execution(* com.cj.aop.User.add(..))")
    public void afterReturning(){
        System.out.println("AfterReturning...");
    }
    @Around("execution(* com.cj.aop.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        System.out.println("Around 之前...");
        proceedingJoinPoint.proceed();
        System.out.println("Around 之后...");
    }
    @AfterThrowing("execution(* com.cj.aop.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing...");
    }
}

 

5. 相同的切入点可以抽取

@Pointcut("execution(* com.cj.aop.User.add(..))")
public void pointDemo(){
}
@Before(value = "pointDemo()")
public void before(){
    System.out.println("before...");
}

6 多个增强类可以设置优先级

package com.cj.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(1)
public class PersonProxy {
    @Before("execution(* com.cj.aop.User.add(..))")
    public void before(){
        System.out.println("person before");
    }
}

 

posted @ 2022-08-20 18:11  写代码的小哥哥  阅读(37)  评论(0编辑  收藏  举报