AOP2(注解实现 前置,后置,环绕通知)

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 takeSeats(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);
  System.out.println("----表演马上开始,请先占位----");
 }
 @Before("execution(* com.juggler.Performer.perform(..))")
 public void turnOffCellPhones()
 {
  System.out.println("-----开始之前,请关机---");
 }
 @AfterReturning("execution(* com.juggler.Performer.perform(..))")
 public void applaud() {
  
  System.out.println("----表演的不错,请鼓掌---");
 }
 @After("execution(* com.juggler.Performer.perform(..))")
 public void turnOnCellPhones() {
  System.out.println("---演出结束,可以开机---");
 }
 @AfterThrowing("execution(* com.juggler.Performer.perform(..))")
 public void demandRefund() {
  System.out.println("---表演的不好,退钱---");
 }
 @Around("execution(* com.juggler.Performer.perform(..))")
 public void watchPerformance(ProceedingJoinPoint joinPoint) {
  try {
   System.out.println("=====环绕开始=====");
   long start=System.currentTimeMillis();
   System.out.println("签名:"+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);
 }
}

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