Spring系列(八)基于注解方式配置 Spring AOP应用

个人技术博客(weigeblog.cn),欢迎访问!!!
更多技术好文与你分享
欢迎交流学习!!!

1. 快速入门

1.1 导入AOP相关坐标

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.19</version>
     </dependency>

1.2 准备目标类、准备增强类

//目标接口
public interface UserService {
    void show1();
    void show2();
}

//目标类
public class UserServiceImpl implements UserService {
    @Override
    public void show1() {
        System.out.println("show1...");
    }

    @Override
    public void show2() {
        System.out.println("show2...");
    }
}

//增强类
public class MyAdvice {
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        joinPoint.proceed();
        System.out.println("环绕后通知");
    }
}

//启动类
public class ApplicationContextTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextTest.class);
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.show1();
    }
}

1.3 配置注解

//配置目标类注解
@Component("userService")
public class UserServiceImpl implements UserService {
    @Override
    public void show1() {
        System.out.println("show1...");
    }

    @Override
    public void show2() {
        System.out.println("show2...");
    }
}

//配置增强类注解
@Component
@Aspect
public class MyAdvice {
    @Around("execution(void org.example.service..*.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        joinPoint.proceed();
        System.out.println("环绕后通知");
    }
}

//配置启动类注解
@Configuration
@ComponentScan(value = "org.example")
@EnableAspectJAutoProxy
public class ApplicationContextTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextTest.class);
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.show1();
    }
}

1.4 运行结果

在这里插入图片描述

2. 基于注解配置详解

2.1 通知类型配置

//前置通知
@Before("execution(*org.example.aop.*.*(..))")
public void before(JoinPoint joinPoint){}
//后置通知
@AfterReturning("execution(*org.example.aop.*.*(..))")
public void AfterReturning(JoinPoint joinPoint){}
//环绕通知
@Around("execution(* org.example.aop.*.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {}
//异常通知
@AfterThrowing("execution(* org.example.aop.*.*(..))")
public void AfterThrowing(JoinPoint joinPoint){}
//最终通知
@After("execution(* org.example.aop.*.*(..))")
public void After(JoinPoint joinPoint){}

2.2 切点表达式引用配置

使用一个空方法,将切点表达式标注在空方法上,其他通知方法引用即可

@Component
@Aspect
public class AnnoAdvice {
//切点表达式抽取
@Pointcut("execution(* com.itheima.aop.*.*(..))")
public void pointcut(){}
//前置通知
@Before("pointcut()")
public void before(JoinPoint joinPoint){}
//后置通知
@AfterReturning("AnnoAdvice.pointcut()")
public void AfterReturning(JoinPoint joinPoint){}
}

2.3 开启注解的代理的配置

@EnableAspectJAutoProxy,用于代替xml配置文件中的<aop:aspectj-autoproxy/>

@EnableAspectJAutoProxy
public class ApplicationContextTest {
    public static void main(String[] args) {
    }
}

xml配置文件中的<aop:aspectj-autoproxy/>

@EnableAspectJAutoProxy
public class ApplicationContextTest {
    public static void main(String[] args) {
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值