美文网首页
Spring AOP @Aspect注解

Spring AOP @Aspect注解

作者: zhy0324 | 来源:发表于2019-08-11 17:33 被阅读0次

    编写切面是开发中一个很重要的环节,能让我们的业务代码更加注重本身代码的实现。可以把其他业务线也用到的一些相同的点提取成切面,方便管理。

    原本不是很懂什么是切面,以及如何编写切面
    在学习《Spring+in+action》的时候,看到了AOP章节,书中的例子,让人豁然开朗。

    本例子是列举基于@Aspect注解

    例子:

    如果将一个大剧场看成一个系统,里面很多演出厅则是系统中的一条条不同的业务线,演出厅的主要业务代码是实现自身的演出。但是每个演出厅都会有观众,观众的进场,喝彩,以及演出失败的退票,大剧场系统也必须要要处理。这个时候就可以把观众看成一个切面。统一在切面管理观众的行为。如:入场前关闭手机、就座、喝彩、演出不尽人意时要求退款。

    首先剧场有一个通用的演出接口

    package com.zhy.edu.aop;
    
    /**
     * 
     * <p>Title:</p>
     * <p>Description:演出通用接口</p>
     * @author zhyHome 
     * @date 2019年8月11日  
     * @version 1.0
     */
    public interface Performance {
    
        /**
         * 
         * <p>@Title: perform</p>   
         * <p>@Description: 演出</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        public void perform() throws Exception;
    }
    

    观众类,然后观众会基于演出做出一系列的行为

    package com.zhy.edu.aop;
    
    import org.aspectj.lang.annotation.After;
    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;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * 
     * <p>Title:</p>
     * <p>Description:观众切面</p>
     * @author zhyHome 
     * @date 2019年8月11日  
     * @version 1.0
     */
    @Aspect
    @Configuration
    public class Audience {
        
        /**
         * 
         * <p>@Title: performance</p>   
         * <p>@Description: 通用切点</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        @Pointcut("execution(** com.zhy.edu.aop.Performance.perform(..))")
        public void performance() {};
    
        /**
         * 
         * <p>@Title: beforeMethod</p>   
         * <p>@Description: 关闭手机(调用通用切点时只需要写通用切点的方法名)</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        @Before("performance()")
        public void silenceCellPhone() {
            System.out.println("观众表演之前关闭手机");
        }
        
        /**
         * 
         * <p>@Title: takeSeats</p>   
         * <p>@Description: 观众就坐</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        @Before("performance()")
        public void takeSeats() {
            System.out.println("观众就坐");
        }
        
        /**
         * 
         * <p>@Title: applause</p>   
         * <p>@Description: 观众喝彩</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        @AfterReturning("performance()")
        public void applause() {
            System.out.println("观众喝彩");
        }
        
        /**
         * 
         * <p>@Title: demandRefund</p>   
         * <p>@Description: 观众退款</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        @AfterThrowing("performance()")
        public void demandRefund() {
            System.out.println("观众退款");
        }
    }
    

    不同的演出厅的演出类

    雨燕演出厅

    package com.zhy.edu.aop;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class SwiftDrama implements Performance{
    
        @Override
        public void perform() throws Exception {
            int random = ((int)(10 * Math.random()))%2;
            System.out.println("雨燕话剧演出中");
            if (random == 1) {
                System.out.println("完美演出");
            }else {
                System.err.println("演出失败");
                throw new Exception();
            }
        }
    
        
    }
    
    

    泰坦尼克演出厅

    package com.zhy.edu.aop;
    
    import org.springframework.stereotype.Component;
    
    /**
     * 
     * <p>Title:</p>
     * <p>Description:泰坦尼克话剧</p>
     * @author zhyHome 
     * @date 2019年8月11日  
     * @version 1.0
     */
    @Component
    public class TitanicDrama implements Performance{
    
        @Override
        public void perform() throws Exception {
            int random = ((int)(10 * Math.random()))%2;
            System.out.println("泰坦尼克话剧演出中");
            if (random == 1) {
                System.out.println("完美演出");
            }else {
                System.err.println("演出失败");
                throw new Exception();
            }
        }
    
    }
    

    执行演出业务代码(junit测试类)

    package com.zhy.edu;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.zhy.edu.aop.SwiftDrama;
    import com.zhy.edu.aop.TitanicDrama;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Autowired
        TitanicDrama performance;
        
        @Autowired
        SwiftDrama swiftDrama;
    
        /**
         * 
         * <p>@Title: performance</p>   
         * <p>@Description: 演出厅测试</p>  
         * @author zhyHome 
         * @date 2019年8月11日   
         * @param:       
         * @return: void      
         * @throws   
         * @version 1.0
         */
        @Test
        public void performance() {
            try {
                System.out.println("---------------------演出厅1:--------------------");
                performance.perform();
                
            } catch (Exception e) {
            }
            
            try {
                System.out.println("---------------------演出厅2:--------------------");
                swiftDrama.perform();
            } catch (Exception e) {
            }
        }
    
    }
    

    输出结果:


    image.png

    这个例子成功的将观众提取成了切面。@Aspect注解的好处是可以将一个普通的POJO变成一个切面,例如观众类。观众可以在此类中管理自己的行为。我们只需要监听演出方法就好了,演出之前之后如何调用就交给Spring,很方便

    相关文章

      网友评论

          本文标题:Spring AOP @Aspect注解

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