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.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  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">
  
  <bean id="juggler" class="com.juggler.Juggler"/>
  <bean id="audience" class="com.audience.Audience"/>
  <!--  Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置:
   <aop:aspectj-autoproxy/> 
  这样Spring就能发现@AspectJ风格的切面并且将切面应用到目标对象。 -->
  <aop:aspectj-autoproxy/>
</beans>

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);
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值