利用注解配置切面

本文介绍了如何利用注解来定义AOP切面,包括在XML和JavaConfig中的配置方式,详细阐述了前置和后置通知的定义,并探讨了AspectJ的五个通知定义注解。同时,讲解了如何创建环绕通知,处理通知参数,以及如何通过注解引入新的功能到bean。

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

利用注解定义切面,必要配置自动代理功能
1、在XML中配置

<aop:aspectj-autoproxy/>

2、如果是在JavaConfig中,配置

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcerConfig{}

一、在POJO中定义前置和后置通知

@Aspect   //定义切面
@Component   //实例化该POJO,调用方法
public class Audience {

    @Before("execution(* org.aop.Perform.play())")
    public void silenceCellphone(){
        System.out.println("please silence cell phone");
    }

    @Before("execution(* org.aop.Perform.play())")
    public void takeSeates(){
        System.out.println("please take Seate");
    }

    @Before("execution(* org.aop.Perform.play())")
    public void applause(){
        System.out.println("CLAP  CLAP  CLAP");
    }

    @Before("execution(* org.aop.Perform.play())")
    public void demandRefund(){
        System.out.println("Refund  refund");
    }
}

上面的切点定义可以优化:

@Aspect   //定义切面
@Component   //实例化该POJO,调用方法
public class Audience {
    //方法内容不重要,本身只是一个标识,供@Pointcut注解依附
    @Pointcut("execution(* org.aop.Perform.play())")
    public void profPonit(){}

    //直接引用该切点所依附的方法
    @Before("profPonit()")
    public void silenceCellphone(){
        System.out.println("please silence cell phone");
    }

    @Before("profPonit()")
    public void takeSeates(){
        System.out.println("please take Seate");
    }

    @After("profPonit()")
    public void applause(){
        System.out.println("CLAP  CLAP  CLAP");
    }

    @After("profPonit()")
    public void demandRefund(){
        System.out.println("Refund  refund");
    }
}

AspectJ提供了五个注解来定义通知:
这里写图片描述

二、创建环绕通知

@Aspect
@Component
public class AroundAudience {

    @Pointcut("execution(* org.aop.Performence.play())")
    public void arounPoint(){}

    @Around("arounPoint()")
    public void watchPerformance(ProceedingJoinPoint jp){
        try{
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            jp.proceed();
            System.out.println("CLAP CLAP CALP");
        }catch (Throwable e){
            System.out.println("Demanding a refund");
        }
    }
}

三、处理通知中的参数

@Pointcut("execution(* org.aop.Perform.palyTrace(int)) && args(Num)")
    public void paraPoint(int Num){}

    @Before("paraPoint(Num)")
    public void countTrack(int Num){
        Num++;
        System.out.println("通知中  trackNum="+Num);
    }

四、通过注解引入新功能

@Aspect
@Component
public class EncodeIntro {

    @DeclareParents(value = "org.aop.Performence+",
                    defaultImpl = DefaultEncore.class)
    public static Encorable encorable;
}

value属性指定了哪种类型的bean要引入该接口
defaultImpl 属性指定了为引入功能提实现的类
@DeclareParents注解所标注的静态属性指明了要引入的接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值