AOP基于注解的配置

package com.wzk.service;

public interface AccountService {

    /**
     * 模拟更新
     */
    void updateAccount();

    int saveAccount();

    void deleteAccount(int i);
}

package com.wzk.service.impl;

import com.wzk.service.AccountService;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Override
    public void updateAccount() {
        System.out.println("执行了更新操作");
    }

    @Override
    public int saveAccount() {
        System.out.println("执行了保存操作");
        return 0;
    }

    @Override
    public void deleteAccount(int i) {
        System.out.println("执行了删除操作");
    }
}

package com.wzk.utils;

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

@Component("logger")
@Aspect
public class Logging {

    @Pointcut("execution(* com.wzk.service.impl.AccountServiceImpl.*(..))")
    private void tl(){

    }
    /**
     * 前置通知
     */
    @Before("tl()")
    public void beforePrintLog(){
        System.out.println("前置通知Logging中的beforePrintLog方法打印了日志文件");
    }

    /**
     * 后置通知
     */
    @AfterReturning("tl()")
    public void afterReturning(){
        System.out.println("后置通知Logging中的afterReturning方法打印了日志文件");
    }

    /**
     * 异常通知
     */
    @AfterThrowing("tl()")
    public void afterThrowing(){
        System.out.println("异常通知Logging中的afterThrowing方法打印了日志文件");
    }

    /**
     * 最终通知
     */
    @After("tl()")
    public void afterPrintLog(){
        System.out.println("最终通知Logging中的afterPrintLog方法打印了日志文件");
    }

    /**
     * 环绕通知
     * 问题:当我们配置了环绕通知之后,切入点没有方法执行,而通知方法执行了。
     * 分析:通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有
     * 解决:spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed。此方法就相当于明确调用切入点方法。
     *       该接口可以作为环绕通知的方法参数。在程序执行时,spring框架会为我们提供接口的实现类供我们使用
     * spring中的环绕通知:
     *          它是spring框架为我们提供的一种可以在代码中手动控制增强的方法何时执行的方式
     * @param pjp
     * @return
     */
    @Around("tl()")
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        Object value = null;
        try{
            Object[] args = pjp.getArgs(); //得到方法执行所需的参数

            System.out.println("Logging中的方法aroundPrintLog方法开始执行日志了。。。。前置");

            value = pjp.proceed(args); //明确调用业务层方法(切入点方法)

            System.out.println("Logging中的方法aroundPrintLog方法开始执行日志了。。。。后置");
            return value;
        }catch (Throwable t){
            System.out.println("Logging中的方法aroundPrintLog方法开始执行日志了。。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logging中的方法aroundPrintLog方法开始执行日志了。。。。最终");
        }
    }
}

@Configuration
@ComponentScan(basePackages="com.wzk")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值