在Spring AOP中怎么使用注解来实现

  介绍

这期内容当中小编将会给大家带来有关在Spring AOP中怎么使用注解来实现,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

春对AOP的实现提供了很好的支持。下面我们就使用春天的注解来完成AOP做一个例子。

首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar, aspectjweaver.jar cglib-nodep.jar。然后我们写一个接口

包com.bird.service;
  
  公共接口PersonServer {
  
  公共空间保存(字符串名称);
  公共空间更新(字符串名称、整数id);
  公共字符串getPersonName(整数id);
  
  }
  
  

和一个接口实现类

包com.bird.service.impl;
  
  进口com.bird.service.PersonServer;
  
  公共类PersonServiceBean实现PersonServer {
  
  @Override
  公共空间保存(字符串名称){
  
  System.out.println(“我是保存方法“);//抛出RuntimeException新();
  }
  
  @Override
  公共空间更新(字符串名称,整数id) {
  
  System.out.println(“我是更新()方法“);
  }
  
  @Override
  公共字符串getPersonName(整数id) {
  
  System.out.println(“我是getPersonName()方法“);
  返回“xxx";
  }
  
  }
  
  

下面使用弹簧注解方式对这个Bean进行方法拦截

包com.bird.service;
  
  进口org.aspectj.lang.ProceedingJoinPoint;
  进口org.aspectj.lang.annotation.After;
  进口org.aspectj.lang.annotation.AfterReturning;
  进口org.aspectj.lang.annotation.AfterThrowing;
  进口org.aspectj.lang.annotation.Around;
  进口org.aspectj.lang.annotation.Aspect;
  进口org.aspectj.lang.annotation.Before;
  进口org.aspectj.lang.annotation.Pointcut;/* *
  *切面
  * @author鸟
  *
  */@Aspect
  公开课MyInterceptor {
  @Pointcut(“执行(* com.bird.service.impl.PersonServiceBean。* (. .))“)
  私人空间anyMethod(){}//定义一个切入点
  
  @Before (“anyMethod (),,args(名称)“)
  公共空间doAccessCheck(字符串名称){
  System.out.println(名称);
  System.out.println(“前置通知“);
  }
  
  @AfterReturning (“anyMethod ()“)
  公共空间doAfter () {
  System.out.println(“后置通知“);
  }
  
  @After (“anyMethod ()“)
  公共空间后(){
  System.out.println(“最终通知“);
  }
  
  @AfterThrowing (“anyMethod ()“)
  公共空间doAfterThrow () {
  System.out.println(“例外通知“);
  }
  
  @Around (“anyMethod ()“)
  公共对象doBasicProfiling (ProceedingJoinPoint pjp)抛出Throwable {
  System.out.println(“进入环绕通知“);
  对象对象=pjp.proceed();//执行该方法
  System.out.println(“退出方法“);
  返回对象;
  }
  }
@Pointcut(“执行(* com.bird.service.impl.PersonServiceBean。* (. .))“)

这句话是方法切入点,执行为执行的意思,*代表任意返回值,然后是包名,。*意思是包下面的所有子包。(. .)代表各种方法,然后下面的注解就比较简单了,就是在使用方法前和中,还有环绕拦截/,然后在春天的配置文件中继续配置Bean,需要打开AOP命名空间

& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比;   http://www.springframework.org/schema/beans" & lt;豆类xmlns=?   xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns: aop=癶ttp://www.springframework.org/schema/aop"   xsi: schemaLocation=?   http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   http://www.springframework.org/schema/aop   ,http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"的在      & lt; aop: aspectj-autoproxy/比;   & lt; bean id=皃ersonServiceBean"类=癱om.bird.service.impl.PersonServiceBean"/比;   & lt; bean id=癿yInterceptor"类=癱om.bird.service.MyInterceptor"/比;      & lt;/beans>      

然后建立一个Junit测试

包junit.test;
  
  进口org.junit.Test;
  进口org.springframework.context.ApplicationContext;
  进口org.springframework.context.support.ClassPathXmlApplicationContext;
  
  进口com.bird.service.PersonServer;
  
  公开课SpringAOPTest {
  
  @Test
  公共空间inteceptorTest () {
  ApplicationContext ctx=new ClassPathXmlApplicationContext (“beanAop.xml");
  PersonServer bean=(PersonServer) ctx.getBean (“personServiceBean");
  bean.save(空);
  }
  
  
  }
  
  

在Spring AOP中怎么使用注解来实现