Spring 基于注解的AOP使用(动态代理)

本文介绍了Spring基于注解的AOP使用,重点在于环绕通知的应用。通过配置XML,定义service和logger的注解,关闭其他通知类型,只启用环绕通知。在测试中展示了前置、最终和后置通知的执行顺序,并提供了Maven的依赖引入以及不使用XML的配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring 基于注解的AOP使用

直接上代码

1.bena.xml配置

<!-- 配置spring创建容器时要扫描的保-->
   <context:component-scan base-package="com.joyTop"></context:component-scan>
   <!-- 配置spring 开启注解AOP的支持-->
   <aop:aspectj-autoproxy ></aop:aspectj-autoproxy>

2.service提供注解

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
   public void saveAccount()
   {
       //int i=1/0;
       System.out.println("保存");
   }
   public void updateAccount(int i) {
       System.out.println("更新");
   }
   public int deleteAccount() {
       System.out.println("删除");
       return 0;
   }
}
  1. Logger提供注解
    因为注解的spring前置通知、后置、异常通知、最终通知有执行顺序问题,我们建议使用注解环绕通知。已经把其余通知关闭,只开启环绕通知
//用于记录日志的工具类,它里面提供了公共的代码
@Component("logger") //交给spring容器管理
@Aspect //表示当前是一个切面
public class Logger {

   //统一切入点
   @Pointcut("execution(* com.joyTop.service.impl.*.*(..))")
   private void pt1(){}
   //用于打印日志,计划让其在切入点方法执行之前(切入点方法就是业务层方法)
   public void printLog() {
       System.out.println("Logger类开始记录日志");
   }
   //前置通知
   //@Before("pt1()")
   public void beforpringLog() {
       System.out.println("前置通知beforpringLog类开始记录日志");
   }
   //后置通知
   //@AfterReturning("pt1()")
   public void afterReturningprintLog() {
       System.out.println("后置通知afterReturningprintLog类开始记录日志");
   }
   //异常通知
   //@AfterThrowing("pt1()")
   public void afterThrowingPrintLog() {
       System.out.println("异常通知afterThrowingPrintLog类开始记录日志");
   }
   //最终通知
   //@After("pt1()")
   public void afterPrintLog() {
       System.out.println("最终通知afterPrintLog类开始记录日志");
   }
   
   @Around("pt1()")-------------------------------------环绕通知
   public Object aroundPringLog(ProceedingJoinPoint pjp) {
       Object rtValue = null;
       try {
           Object[] args = pjp.getArgs();//得到方法执行所需的参数
           System.out.println("前置aroundPringLog环绕通知");
           rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
           System.out.println("后置aroundPringLog环绕通知");
           return rtValue;
       } catch (Throwable t) {
           System.out.println("异常aroundPringLog环绕通知");
           throw new RuntimeException(t);
       } finally {
           System.out.println("最终aroundPringLog环绕通知");
   }
}
  1. AOPTest测试方法
public class AOPTest {
  public static void main(String[] args) {
      //1.获取容器
      //ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
      //2.获取对象
      IAccountService as = ac.getBean("accountService", IAccountService.class);
      as.saveAccount();
  }
}

5.运行结果
前置通知beforpringLog类开始记录日志
保存
最终通知afterPrintLog类开始记录日志
后置通知afterReturningprintLog类开始记录日志

6.Mave的引入

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8.RELEASE</version>
        </dependency>
    </dependencies>

7.不使用XML的配置方式
新建一个配置类

@Configuration  //这是一个配置类
@ComponentScan(basePackages = "com.joyTop")  //要扫描的包
@EnableAspectJAutoProxy //开启注解AOP的支持
public class SpringConfiguration {
}

测试时需要更换获取容器的类:AnnotationConfigApplicationContext

        //1.获取容器     
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //2.获取对象
        IAccountService as = ac.getBean("accountService", IAccountService.class);
        as.saveAccount();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值