[sping reveal] 15, afterreturning

@afterreturning

We similarly write several test classes

 

package cn.cutter.start.bean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component
public  class AterReturningTestBean {

    private static final Log logger = LogFactory.getLog(AterReturningTestBean.class);

    public  void aterReturningTest () {
        logger.info( "aterReturningTest sds" );
    }
    
    public  int aterReturningTestInt () {
        logger.info ( "aterReturningTestInt" );
        
        return 666;
    }
    
}

 

Write a few test interceptors

package cn.cutter.start.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 *
 * @author xiaof
 *
 */
@Component
@Aspect
public  class AterReturningAspect {

    private static final Log logger = LogFactory.getLog(AterReturningAspect.class);
    
    //execution(* cn.cutter.start.bean.BeofreTestBean.*(..))
    @Pointcut("execution(* cn.cutter.start.bean.AterReturningTestBean.*(..))")
    private void pointCut1() {}
    
    @AfterReturning(pointcut="pointCut1()", returning="value1")
    public void testReturn1(int value1) {
        
        logger.info( "Return interception: " + value1);
        
    }
    
    @AfterReturning(pointcut="pointCut1()")
    public void testReturn2() {
        
        logger.info( "return interception: no parameters" );
        
    }
    
}

result:

@Test
    public void testAop4() {
        ApplicationContext ctx = this.before();
        
        AterReturningTestBean att = (AterReturningTestBean) ctx.getBean ("aterReturningTestBean" );
        
//         att.aterReturningTest ();
        
        att.aterReturningTestInt ();
        
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127662&siteId=291194637