栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

AOP与自定义注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AOP与自定义注解

1.使用自定义注解需要引入的包


    1.9.1



    org.aspectj
    aspectjrt
    ${aspectj.version}

2.自定义注解

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@documented
public @interface MyAnnotation {

    String name();

}

3.自定义Aspect

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import javax.xml.ws.Response;

@Aspect
@Component
@ComponentScan
@EnableAspectJAutoProxy
public class MyAspect {

    @Pointcut("@annotation(com.example.demo.annotation.MyAnnotation)")
    private void aspect() {
    }

    
    @Around(value = "aspect()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object retValue = null;
        try {
            Object[] args = joinPoint.getArgs(); //得到方法执行所需的参数
            System.out.println("=================Around===前置======================");
            retValue = joinPoint.proceed(args);//明确调用业务层方法(切入点方法)
            System.out.println("=================Around===后置===================");
            return retValue;
        } catch (Throwable throwable) {
            System.out.println("=================Around===异常===================");
            throwable.printStackTrace();
        } finally {
            System.out.println("=================Around===最终===================");
        }
        return retValue;
    }


    
    @Before(value = "aspect()")
    public void before() {
        System.out.println("=================Before======================");
    }
    
    
    @After(value = "aspect()")
    public void after() {
        System.out.println("=================After======================");
    }
    
    
    @AfterReturning(value = "aspect()", returning = "response")
    public void afterReturning(JoinPoint joinPoint, Response response) {
        System.out.println("=================AfterReturning======================");
    }

    
    @AfterThrowing(value = "aspect()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        System.out.println("=================AfterThrowing======================");
    }

}

4.执行效果

@Around 正常执行:
=================Around===前置======================
=============================已进入加注解的方法=============================
=================Around===后置===================
=================Around===最终===================
===============================方法执行完成===========================
@Around 异常执行:
=================Around===前置======================
=============================已进入加注解的方法=============================
=================Around===异常===================
java.lang.NullPointerException
	
=================Around===最终===================
===============================方法执行完成===========================

other 正常执行:
=================Before======================
=============================已进入加注解的方法=============================
=================After======================
===============================方法执行完成===========================

other 异常执行:
=================Before======================
=============================已进入加注解的方法=============================
=================AfterThrowing======================
=================After======================
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号