|
| 1 | +package cn.aofeng.demo.aspectj; |
| 2 | + |
| 3 | +import org.apache.commons.lang.ArrayUtils; |
| 4 | +import org.aspectj.lang.JoinPoint; |
| 5 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 6 | +import org.aspectj.lang.annotation.After; |
| 7 | +import org.aspectj.lang.annotation.AfterReturning; |
| 8 | +import org.aspectj.lang.annotation.AfterThrowing; |
| 9 | +import org.aspectj.lang.annotation.Around; |
| 10 | +import org.aspectj.lang.annotation.Aspect; |
| 11 | +import org.aspectj.lang.annotation.Before; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * 拦截器。 |
| 17 | + * |
| 18 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 19 | + */ |
| 20 | +@Aspect |
| 21 | +public class BusinessServiceInterceptor { |
| 22 | + |
| 23 | + private static Logger _logger = LoggerFactory.getLogger(BusinessServiceInterceptor.class); |
| 24 | + |
| 25 | + private char _style = '-'; |
| 26 | + |
| 27 | + /** |
| 28 | + * <pre> |
| 29 | + * Before通知不能修改方法传入的参数。 |
| 30 | + * </pre> |
| 31 | + */ |
| 32 | + @Before("execution(public * cn.aofeng.demo.aspectj.BusinessService.add(..))") |
| 33 | + public void beforeAdd(JoinPoint joinPoint) { |
| 34 | + _logger.info( String.format("拦截到方法:%s, 传入参数:%s", |
| 35 | + joinPoint.getSignature().getName(), |
| 36 | + ArrayUtils.toString(joinPoint.getArgs()) ) ); |
| 37 | + BusinessService.printLine(_style); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * <pre> |
| 42 | + * After通知不能修改方法的返回值。 |
| 43 | + * 如果被拦截的方法抛出异常,拦截代码仍然正常执行。 |
| 44 | + * </pre> |
| 45 | + */ |
| 46 | + @After("execution(public * cn.aofeng.demo.aspectj.BusinessService.join(..))") |
| 47 | + public void beforeAddSupportMultiArgs(JoinPoint joinPoint) { |
| 48 | + _logger.info("Signature.name:"+joinPoint.getSignature().getName()); |
| 49 | + _logger.info("Args:" + ArrayUtils.toString(joinPoint.getArgs())); |
| 50 | + _logger.info("Target:" + ArrayUtils.toString(joinPoint.getTarget())); |
| 51 | + _logger.info("This:" + ArrayUtils.toString(joinPoint.getThis())); |
| 52 | + _logger.info("Kind:" + ArrayUtils.toString(joinPoint.getKind())); |
| 53 | + _logger.info("SourceLocation:" + ArrayUtils.toString(joinPoint.getSourceLocation())); |
| 54 | + |
| 55 | + // 试图修改传入的参数 |
| 56 | + joinPoint.getArgs()[0] = "100"; |
| 57 | + |
| 58 | + BusinessService.printLine(_style); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * <pre> |
| 63 | + * AfterReturning通知不能修改方法的返回值。 |
| 64 | + * 如果被拦截的方法抛出异常,拦截代码不再执行。 |
| 65 | + * </pre> |
| 66 | + */ |
| 67 | + @AfterReturning(pointcut="execution(public * cn.aofeng.demo.aspectj.BusinessService.join(..))", returning="result") |
| 68 | + public void afterReturnAdd(JoinPoint joinPoint, Object result) { |
| 69 | + _logger.info( String.format("拦截到方法:%s, 传入参数:%s, 执行结果:%s", |
| 70 | + joinPoint.getSignature().getName(), |
| 71 | + ArrayUtils.toString(joinPoint.getArgs()), |
| 72 | + result) ); |
| 73 | + |
| 74 | + // 试图修改返回值 |
| 75 | + result = "hello, changed"; |
| 76 | + |
| 77 | + BusinessService.printLine(_style); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * <pre> |
| 82 | + * 只在被拦截的方法抛出异常时才执行。 |
| 83 | + * </pre> |
| 84 | + */ |
| 85 | + @AfterThrowing(pointcut="execution(public * cn.aofeng.demo.aspectj.BusinessService.join(..))", throwing="ex") |
| 86 | + public void afterThrowingAdd(JoinPoint joinPoint, Exception ex) { |
| 87 | + _logger.info( String.format("拦截到方法:%s, 传入参数:%s", |
| 88 | + joinPoint.getSignature().getName(), |
| 89 | + ArrayUtils.toString(joinPoint.getArgs()) ) ); |
| 90 | + if (null != ex) { |
| 91 | + _logger.info("拦截到异常:", ex); |
| 92 | + } |
| 93 | + |
| 94 | + BusinessService.printLine(_style); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * <pre> |
| 99 | + * {@link ProceedingJoinPoint}只能在Around通知中使用。 |
| 100 | + * Around通知可以修改被拦截方法的传入参数和返回值。 |
| 101 | + * </pre> |
| 102 | + */ |
| 103 | + @Around("execution(public * cn.aofeng.demo.aspectj.BusinessService.addPrefix(..))") |
| 104 | + public Object afterAround(ProceedingJoinPoint joinPoint) throws Throwable { |
| 105 | + _logger.info( String.format("拦截到方法:%s, 传入参数:%s", |
| 106 | + joinPoint.getSignature().getName(), |
| 107 | + ArrayUtils.toString(joinPoint.getArgs()) ) ); |
| 108 | + |
| 109 | + Object result = null; |
| 110 | + try { |
| 111 | + result = joinPoint.proceed(); |
| 112 | + _logger.info("执行结果:" + result); |
| 113 | + } catch (Throwable e) { |
| 114 | + throw e; |
| 115 | + } finally { |
| 116 | + BusinessService.printLine(_style); |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments