(笔记)Spring实战_面向切面的Spring(4)_注解切面

AspectJ面向注解的模型可以非常简便地通过少量注解把任意类转变成切面。这种新特性通常称为@AspectJ。

package com.springinaction.springidol;


import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class AudienceOfAspectJ
{

    @Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))")
    public void performance()
    {

    }

    @Before("performance()")
    public void takeSeats()
    {
        System.out.println("The audience is taking their seats.");
    }

    @Before("performance()")
    public void turnOffCellPhones()
    {
        System.out.println("The audience is turning off their cellphones.");
    }

    @AfterReturning("performance()")
    public void applaud()
    {
        System.out.println("CLAP CLAP CLAP CLAP");
    }

    @AfterThrowing("performance()")
    public void demandRefund()
    {
        System.out.println("Boo!We want our money back!");
    }

}

@Pointcut注解用于定义一个可以在@AspectJ切面内可重用的切点。
切点的名称来源于注解所应用的方法名称。因此,该切点的名称为performance()。
因为AudienceOfAspectJ类本身包含了所有它需要定义的切点和通知,所有我们不再需要在XML配置中声明切点和通知。我们需要在Spring上下文中声明一个自动代理Bean,该Bean知道如何把@AspectJ注解所标注的Bean转变为代理通知。
为此,Spring自带了名为AnnotationAwareAspectJAutoProxyCreator的自动代理创建类。我们可以在Spring上下文中把AnnotationAwareAspectJAutoProxyCreator注册为一个Bean。
为了简化如此长的名字,Spring在aop命名空间中提供了一个自定义的配置元素:<aop:aspectj-autoproxy />
我们需要记住<aop:aspectj-autoproxy />仅仅使用@AspectJ注解作为指引来创建基于代理的切面,但本质上它仍然是一个Spring风格的切面。这意味着虽然我们使用@AspectJ的注解,但是我们仍然限于代理方法的调用。如果想利用AspectJ的所有能力,我们必须在运行时使用AspectJ并不依赖Spring来创建基于代理的切面。
<aop:aspect>相对于@AspectJ的一个明显优势是我们不需要实现切面功能的源码。通过@AspectJ,我们必须标注类和方法,它需要有源码。而<aop:aspect>可以引用任意一个Bean。
1.注解环绕通知

    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinpoint)
    {
        try
        {
            System.out.println("The audience is taking their seats.");
            System.out.println("The audienct is turning off their cellphones.");
            long start = System.currentTimeMillis();
            joinpoint.proceed();
            long end = System.currentTimeMillis();
            System.out.println("CLAP CLAP CLAP CLAP");
            System.out.println("The performance took " + (end - start) + " milliseconds.");
        }
        catch (Throwable e)
        {
            System.out.println("Boo!We want out money back!");
        }
    }

2.传递参数给标注的通知

package com.springinaction.springidol;


import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class MagicianOfAspectJ implements MindReader
{

    private String thoughts;

    @Pointcut("execution(* com.springinaction.springidol.Thinker.thinkOfSomething(String)) && args(thoughts)")
    public void thinking(String thoughts)
    {

    }

    @Before("thinking(thoughts)")
    public void interceptThoughts(String thoughts)
    {
        System.out.println("Intercepting volunteer's thoughts");
        this.thoughts = thoughts;
    }

    public String getThoughts()
    {
        return thoughts;
    }

}

3.标注引入
等价于<aop:declare-parents>的注解是@AspectJ的@DeclareParents。

package com.springinaction.springidol;


import org.aspectj.lang.annotation.DeclareParents;


public class ContestantIntroducer
{

    @DeclareParents(value = "com.springinaction.springidol.Performer", defaultImpl = GraciousContestant.class)
    public static Contestant contestant;
}
    <bean class="com.springinaction.springidol.ContestantIntroducer" />

ContestantIntroducer是一个切面。但是不同于我们之前所创建的那些切面,该切面并没有提供前置、后置或环绕通知。它为Performer Bean引入了Contestant接口。
value属性等同于<aop:declare-parents>的types-matching属性。它标识应该被引入指定接口的Bean的类型。
defaultImpl属性等同于<aop:declare-parents>的default-impl属性。它标识该类提供了所引入接口的实现。
由@DeclareParents注解所标注的static属性指定了将被引入的接口。
当发现使用@Aspect注解所标注的Bean时,<aop:aspectj-autoproxy />将自动创建代理。依据被调用的方式是属于被代理的Bean还是引入的接口,该代理把调用委托给被代理的Bean或引入的实现。
我们需要关注的一个注意事项是@DeclareParents没有对应于<aop:declare-parents>的delegate-ref属性所对应的等价物。这是因为@DeclareParents是一个@AspectJ注解。@AspectJ是一个不同于Spring的项目,因此它的注解并不了解Spring的Bean。这意味着如果我们想委托给Spring所配置的Bean,那@DeclareParents并不能满足需求,我们只能借助于<aop:declare-parents>
Spring AOP有助于把横切关注点从应用的业务逻辑中分离出来。但是正如我们所看到的,Spring切面仍然只是基于代理的,而且限于通知方法的调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值