zoukankan      html  css  js  c++  java
  • springboot切面编程,自定义注解

    目标:创建自定义注解,实现切面编程

    首先在pom文件加入:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
            </dependency> 

    创建Annontation:

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface AoundAnnotation {
    
    }

    创建Annontation的处理类aspect

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class AoundAnnotationAspect {
    
        /**
         * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果
         * '@Pointcut("execution(* yxm.zyf.love.service.impl.*.*(..))")'
         */
        @Pointcut("@annotation(yxm.zyf.love.annontation.AoundAnnotation)")
        public void operationLog(){}
    
        /**
         * 环绕通知
         * */
        @Around("operationLog()")
        public boolean doAround(ProceedingJoinPoint joinPoint) throws Throwable{
            boolean b=false;
            System.out.println("进入切面");
            return (boolean)joinPoint.proceed();
            //return b;
        }
        
        /**
         * 进入业务方法前
         * */
        @Before("operationLog()")
        public void doBeforeAdvice(JoinPoint joinPoint){
            System.out.println("进入方法前执行.....");
    
        }
    
        /**
         * 处理完请求,返回内容
         * @param ret
         */
        @AfterReturning(returning = "ret", pointcut = "operationLog()")
        public void doAfterReturning(Object ret) {
            System.out.println("方法的返回值 : " + ret);
        }
    
        /**
         * 后置异常通知
         */
        @AfterThrowing("operationLog()")
        public void throwss(JoinPoint jp){
            System.out.println("方法异常时执行.....");
        }
    
    
        /**
         * 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
         */
        @After("operationLog()")
        public void after(JoinPoint jp){
            System.out.println("方法最后执行.....");
        }
    
    }

    测试:

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = DemoApplication.class)
    public class TestAnnotation {
    
        @Autowired
        private AnnontationServiceTestImpl a;
        
        @Test
        public void testA() throws Exception{
            System.out.println("单元测试开始");
            a.anoundTest();
            System.out.println("单元测试完成");
        }
    }

  • 相关阅读:
    学习软件工程的个人总结
    结对编程——黄金点游戏
    读取程序字符,行数,单词的个人程序
    c#代码分析
    Visual Studio2013的安装过程及练习测试
    小学三年级出题程序
    超级课程表的个人评价
    对软件工程的个人困惑
    个人附加作业
    个人最终总结
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/10833170.html
Copyright © 2011-2022 走看看