Spring AOP

Spring AOP的术语

  1. 通知(advice):定义了切面的功能和调用的时机
    通知分为:前置通知(Before)、后置通知(After)、返回通知(After-returning)、异常通知(After-throwing)、环绕通知(Around)
  2. 连接点(join point):定义了所有需要插入通知的点
  3. 切点(Pointcut):定义了何处插入通知,是连接点的子集,切点的定义会匹配所要织入的一个或多个连接点
  4. 切面(Aspect):是切点和通知的集合

Spring AOP与AspectJ

  1. AspectJ还支持属性与构造器的连接点,SpringAOP只支持方法的连接点
  2. AspectJ支持编译期类加载期运行期三种方式的AOP实现,Spring AOP只支持运行期的AOP实现
    编译期:AOP切面在编译期织入目标,需织入编译器支持
    类加载器:AOP切面在类加载时织入目标,需要特殊的类加载器
    运行期:为目标动态创建一个代理对象来织入切面(所以只支持方法级)

引入与织入

  1. 引入(Introduction):允许我们向现有类添加新方法或属性
  2. 织入(Weaving):把切面应用到目标对象并创建新的代理对象

JavaConfig方式配置AOP

  1. 配置切面
    `@Aspect
    public class Audience {
    //@Pointcut能够使不用在每个织入方法上写上"excution"的切点
    @Pointcut(“excution(** concert.Performance.perform(…))”)
    public void performance(){}

    //@Before指定通知advice的调用时机,"excution"指定切点
    //因为有配置@Pointcut,这里可用@Before(“performance()”)简化
    @Before(“excution(** concert.Performance.perform(…))”)
    public void silenceCellPhones() {

    }

    //注意环绕通知有个ProceedingJoinPoint类型的入参
    //注意当目标方法需要参数(int trackNumber)时,参数的传递方式
    @Around(“excution(** concert.Performance.perform2(int)) && args(trackNumber)”)
    public void watchPerformance(ProceedingJoinPoint jp, int trackNumber) {
    try {

    jp.proceed(trackNumber);

    }catch (Throwable e) {

    }
    }
    }`

  2. 使能SpringAOP,并注入目标bean与切面bean
    `@Configuration
    @EnableAspectJAutoProxy
    public class AOPConfig {
    //可用扫描方式注入切面bean与目标bean

    //注入切面
    @Bean
    public Audience audience() {
    return new Audience();
    }
    //注入目标bean

    }
    `

引入新功能

Spring AOP 往目标bean引入新功能,是通过当访问代理目标bean的新方法时,导向了被代理包装的新功能方法的实现bean,即一个代理bean代理了两个目标bean
@Aspect public class EncoreableIntroduction { //value 代表目标类,+代表子类 //defaultImpl代表引入新功能的实现 //@DeclareParents一般标注静态属性 @DeclareParents(value="concert.Performance+", defaultImpl=DefaultEncoreable.class) public static Encoreable encoreble; }

JavaConfig与XML配置的选择

当不能用注解配置通知类源码时(通知类源代码不在手中),选用XML

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值