zoukankan      html  css  js  c++  java
  • springAop的使用

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口。任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。我们先来了解一下这两个接口的主要方法: 
    1)JoinPoint 
     java.lang.Object[] getArgs():获取连接点方法运行时的入参列表; 
     Signature getSignature() :获取连接点的方法签名对象; 
     java.lang.Object getTarget() :获取连接点所在的目标对象; 
     java.lang.Object getThis() :获取代理对象本身; 
    2)ProceedingJoinPoint 
    ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法: 
     java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; 
     java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。 




    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">
    <!-- 激活组件扫描功能,在包com.hyq.aop及其子包下面自动扫描通过注解配置的组件 -->
    <context:component-scan base-package="com.service.impl.test"/>
    <!-- 激活自动代理功能 -->
    <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
    <aop:aspectj-autoproxy/>
    </beans>


    例子:
    @Component
    @Aspect
    public class appbuyCarAop {

    private Logger logger= LoggerFactory.getLogger(appbuycarAop.class);

    @Autowired
    private BuycarImpl buycarImpl;

    /**配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点*/
    @Pointcut("execution(* com.service.impl.useServiceImpl.save(..)) " +
    "|| execution(* com.service.impl.useServiceImpl.update(..)) " +
    "|| execution(* com.service.impl.buycarImpl.save(..))")
    public void aspect(){

    }

    /**
    * 配置前置通知,使用在方法aspect()上注册的切入点
    * 同时接受JoinPoint切入点对象,可以没有该参数
    */

    @Before("aspect()")
    public void before(JoinPoint joinPoint){
    System.out.println("执行before.....");
    }

    /**
    * 配置后置通知,使用在方法aspect()上注册的切入点
    * @param joinPoint
    */
    @After("aspect()")
    public void after(JoinPoint joinPoint){
    System.out.println("执行after.....");
    }

    /**
    * 配置环绕通知,使用在方法aspect()上注册的切入点
    * @param joinPoint
    */
    @Around("aspect()")
    public void around(JoinPoint joinPoint){
    long start = System.currentTimeMillis();
    try {
    ((ProceedingJoinPoint) joinPoint).proceed();
    long end = System.currentTimeMillis();
    if(logger.isInfoEnabled()){
    logger.info("around " + joinPoint + " Use time : " + (end - start) + " ms!");
    }
    } catch (Throwable e) {
    long end = System.currentTimeMillis();
    if(logger.isInfoEnabled()){
    logger.info("around " + joinPoint + " Use time : " + (end - start) + " ms with exception : " + e.getMessage());
    }
    }
    }

    /**
    * 配置后置返回通知,使用在方法aspect()上注册的切入点
    * @param joinPoint
    */
    @AfterReturning("aspect()")
    public void afterReturn(JoinPoint joinPoint){
    if(logger.isInfoEnabled()){
    logger.info("afterReturn " + joinPoint);
    }
    }

    /**
    * 配置抛出异常后通知,使用在方法aspect()上注册的切入点
    * @param joinPoint
    * @param ex
    */
    @AfterThrowing(pointcut="aspect()", throwing="ex")
    public void afterThrow(JoinPoint joinPoint, Exception ex){
    if(logger.isInfoEnabled()){
    logger.info("afterThrow " + joinPoint + " " + ex.getMessage());
    }
    }
  • 相关阅读:
    CF700C Break Up
    CF865C Gotta Go Fast
    CF1559D2 Mocha and Diana (Hard Version)
    CF1500C Matrix Sorting
    排列计数机
    CSP-S 2021 退役记
    【做题记录】[NOIP2011 提高组] 观光公交
    【做题记录】构造题
    乱搞
    二维树状数组
  • 原文地址:https://www.cnblogs.com/h-c-g/p/10033320.html
Copyright © 2011-2022 走看看