AOP3(随记,在前置,后置,环绕通知中获取目标方法的返回值等)

1.需要插入到目标方法各个位置的方法(即切面)

package com.audience;
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.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class Audience {
 @Before("execution(* com.juggler.Performer.perform(..))")
 public void before(JoinPoint joinPoint)
 {
  String classname=joinPoint.getTarget().getClass().getName();
  String methodname=joinPoint.getSignature().getName();
  StringBuilder log=new StringBuilder();
  log.append("before:")
  .append(classname)
  .append("@")
  .append(methodname)
  .append(",Param:");
  Object[] args=joinPoint.getArgs();
  int i=args.length;
  for(Object arg : args)
  {
   if(i==1)
    log.append(arg.toString());
   else {
    log.append(arg.toString()+",");
   }
  }
  System.out.println(log);
 }
 @AfterReturning("execution(* com.juggler.Performer.perform(..))")
 public void afterReturning(JoinPoint joinPoint) {
  System.out.println("afterReturning:"+joinPoint.getSignature());
 }
 @After("execution(* com.juggler.Performer.perform(..))")
 public void after(JoinPoint joinPoint) {
  System.out.println("after:"+joinPoint.getSignature());
 }
 @AfterThrowing("execution(* com.juggler.Performer.perform(..))")
 public void throwing() {
  System.out.println("---异常信息---");
 }
 @Around("execution(* com.juggler.Performer.perform(..))")
 public void around(ProceedingJoinPoint joinPoint) {
  try {
   System.out.println("------环绕开始------");
   long start=System.currentTimeMillis();
   System.out.println("around:"+joinPoint.getSignature());
   joinPoint.proceed();
   long end=System.currentTimeMillis();
   System.out.println("共耗时="+(end-start)+"毫秒");
   System.out.println("------环绕结束------");
  } catch (Throwable e) {
   System.out.println("切入失败");
   e.printStackTrace();
  }
 }
}

2.目标方法的接口类

package com.juggler;

import org.aspectj.lang.annotation.Pointcut;

public interface Performer {

 public void perform(int i);
}

3.目标方法的实现类

package com.juggler;

public class Juggler implements Performer{
 @Override
 public void perform(int i) {
  System.out.println("----Juggler的perform方法被调用----");
 }
}

4.配置文件


  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  
  
  
  
  

5.测试类

package com.Test;
import javax.enterprise.inject.New;
import javax.faces.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.juggler.Performer;
public class test {
 public static void main(String[] arges) {
  ApplicationContext context=new ClassPathXmlApplicationContext("config.xml");
  //此时必须是接口类的对象
  Performer performer=(Performer) context.getBean("juggler");
  System.out.println("----Service层的日志文件的输出如下----");
  performer.perform(2);
 }
}

你可能感兴趣的:(spring,AOP,面包)