spring aop 001 : 增强顺序

package com.thereisnospon.spring.demo.course.imp.c004_aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
/**
 * 如果增强在同一个切面类中声明,则依照增强在切面类中定义的顺序进行织入;
 *
 * 如果增强位于不同的切面类中,且这些切面类都实现了org.springframework.core.Ordered接口,则由接口方法的顺序号决定(顺序号小的先织入);
 *
 * 如果增强位于不同的切面类中,且这些切面类没有实现org.springframework.core.Ordered接口,织入的顺序是不确定的。
 */
public class T002_Aop02_TargetThis2 {

    public static class BookService {
        public void doService() {
            System.out.println("doService");
        }
    }

    @Aspect
    @Order(value = 2)
    public static class BookAspect {
        @Before("within(com.thereisnospon.spring.demo.course.imp.c004_aop.T002_Aop02_TargetThis2.BookService)")
        public void be() {
            System.out.println("BookAspect");
        }
    }

    @Aspect
    public static class BookAspect2 implements Ordered {

        @Before("within(com.thereisnospon.spring.demo.course.imp.c004_aop.T002_Aop02_TargetThis2.BookService)")
        public void be() {
            System.out.println("BookAspect2");
        }

        @Override
        public int getOrder() {
            return 1;
        }
    }

    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    public static class Config {
        @Bean
        public BookAspect bookAspect() {
            return new BookAspect();
        }

        @Bean
        public BookAspect2 bookAspect2() {
            return new BookAspect2();
        }

        @Bean
        public BookService bookService() {
            return new BookService();
        }
    }

    public static void test1() {
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        BookService bookService = (BookService) context.getBean("bookService");
        bookService.doService();
    }

    public static void main(String[] args) {
        test1();
        /*输出
        BookAspect2
        BookAspect
        doService
         */
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值