美文网首页
Spring AOP 简化笔记(一)

Spring AOP 简化笔记(一)

作者: LeeSpringFly | 来源:发表于2017-07-04 10:41 被阅读0次
    • 切点表达式
    • 编写配置
    • 编写切点
    • 编写切面(@Aspect注解)
      • 简化切面编码(@Pointcut注解)
      • 环绕通知(@Around注解)
    • 测试

    文件目录

    concert
    |- ConcertConfig
    |- Performance
    |- PerformanceImpl
    |- Audience

    切点表达式

           返回任意类型     方法所属类        方法   使用任意参数
                    |-||-----------------| |-----||--|
           execution(* concert.Performance.perform(..))
           |-------| |-------------------------------|
    在方法执行时触发              指定方法
    

    编写配置

    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan
    public class ConcertConfig {
    }
    

    编写切点

    public interface Performance {
        public void perform();
    }
    
    @Component
    public class PerformanceImpl implements Performance {
        @Override
        public void perform() {
            System.out.println("perform()");
        }
    }
    

    编写切面

    @Aspect
    @Component
    public class Audience {
    
        @Before("execution(* concert.Performance.perform(..))")
        public void silenceCellPhones() {
            System.out.println("Silencing cell phones");
        }
    
        @Before("execution(* concert.Performance.perform(..))")
        public void takeSeats() {
            System.out.println("Taking seats");
        }
    
        @AfterReturning("execution(* concert.Performance.perform(..))")
        public void applause() {
            System.out.println("CLAP CLAP CLAP!!!");
        }
    
        @AfterReturning("execution(* concert.Performance.perform(..))")
        public void demandRefund() {
            System.out.println("Demanding a refund");
        }
    }
    

    切面--简化切面编码(@Pointcut注解)

    @Aspect
    @Component
    public class Audience {
    
        @Pointcut("execution(* concert.Performance.perform(..))")
        public void performance() {
    
        }
    
        @Before("performance()")
        public void silenceCellPhones() {
            System.out.println("Silencing cell phones");
        }
    
        @Before("performance()")
        public void takeSeats() {
            System.out.println("Taking seats");
        }
    
        @AfterReturning("performance()")
        public void applause() {
            System.out.println("CLAP CLAP CLAP!!!");
        }
    
        @AfterReturning("performance()")
        public void demandRefund() {
            System.out.println("Demanding a refund");
        }
    }
    

    切面--环绕通知(@Around注解)

    @Aspect
    @Component
    public class Audience {
    
        @Pointcut("execution(* concert.Performance.perform(..))")
        public void performance() {
        }
    
        @Around("performance()")
        public void watchPerformance(ProceedingJoinPoint jp) {
            try {
                System.out.println("Silencing cell phones");
                System.out.println("Taking seats");
                jp.proceed();
                System.out.println("CLAP CLAP CLAP!!!");
                System.out.println("Demanding a refund");
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
    

    测试

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = {ConcertConfig.class})
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class PerformanceImplTest { 
    
        @Autowired
        private Performance p;
    
        @Test
        public void testPerform() throws Exception {
            p.perform();
        }
    } 
    
    Silencing cell phones
    Taking seats
    perform()
    CLAP CLAP CLAP!!!
    Demanding a refund
    
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:Spring AOP 简化笔记(一)

          本文链接:https://www.haomeiwen.com/subject/zymthxtx.html