spring aop改变目标方法的返回值

package com.example.tuomin.interator;

import org.apache.log4j.Logger;
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.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 请求拦截器 切面编程
 * @author sanch
 *
 */
@Aspect // 表示该类是一个通知类
@Component //spring注解方式bean注入 交给spring管理
public class RequestInterceptor {
	private  final static Logger logger = Logger.getLogger(RequestInterceptor.class);
	
	//定义一个空方法 借用其注解抽取切点表达式 
	@Pointcut("execution (* com.example.tuomin.control.*.*(..)) && @annotation(com.example.tuomin.annotations.TuominAnn) ")
	public void point() {}
		
	@Before("point()")
	public void before(JoinPoint joinPoint) throws Exception{
			System.out.println("进入aop-----------------------");
	}
	@After("point()")
	public void after(JoinPoint joinPoint){
		for (int i = 0; i < joinPoint.getArgs().length; i++) {  
            System.out.println(joinPoint.getArgs()[i]);  
        }  
        System.out.println(joinPoint.getSignature().getName());  
          
        System.out.println("=====checkSecurity====");  

	}

	 @AfterReturning(returning="rvt", pointcut="@annotation(com.example.tuomin.annotations.TuominAnn)")
	 public Object AfterExec(JoinPoint joinPoint,Object rvt){
		 System.out.println(rvt);
		 return "---"+rvt;

	 }
	 
	 @Around("point()")
	 public Object around(ProceedingJoinPoint pjp) throws Throwable{
			System.out.println("beginning----"); 
			Object object = pjp.proceed();    //运行doSth(),返回值用一个Object类型来接收
			object = "Mission Two";   //改变返回值
		    System.out.println("ending----");
			return object;    
		}



}

 

你可能感兴趣的:(spring)