AspectJ是一个面向切面的框架,他扩展了Java
使用AspectJ实现AOP有两种形式:
一种是通过注解,一种是通过XML
第一种:
接口
package demo19; /** * Created by mycom on 2018/3/12. */ public interface IUserService { public void select(); public void update(); public void insert(); public void delect(); }
实现类
package demo19; /** * Created by mycom on 2018/3/12. */ public class UserServiceimpl implements IUserService { public void select() { /*int i=2/0;*/ System.out.println("select ok!"); } public void update() { System.out.println("update ok!"); } public void insert() { System.out.println("insert ok!"); } public void delect() { System.out.println("delect ok!"); } }
MyAspect类
package demo19; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; /** * Created by mycom on 2018/3/12. */ @Aspect public class MyAspect { @Pointcut(value = "execution(* demo19.*.update(..))") public void update(){ } @Pointcut(value = "execution(* demo19.*.insert(..))") public void insert(){ } @Before("execution(* demo19.*.insert(..))") public void myBefore(){ System.out.println("before==="); } /*@AfterReturning("execution(* demo19.*.select(..))") public void myAfter(){ System.out.println("after==="); }*/ //环绕增强 @Around("update()||insert()") public void myAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("before====="); joinPoint.proceed(); System.out.println("after==="); } @AfterThrowing(value = "execution(* demo19.*.select(..))",throwing = "error") public void myThrows(Exception error){ System.out.println("出现异常了"+error); } @After("execution(* demo19.*.select(..))") public void myFinally(){ System.out.println("最终增强"); } }
配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--1.目标对象--> <bean id="service" class="demo19.UserServiceimpl"></bean> <!--2.增强/通知--> <bean id="beforeAdvice" class="demo19.MyAspect"></bean> <!--自动代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
测试类
//Aspectj增强 基于注解版的增强 @Test public void t3() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextAspectj.xml"); IUserService service =(IUserService) context.getBean("service"); // try { // service.select(); // }catch (Exception ex){ // ex.printStackTrace(); // } // service.delect(); service.insert(); service.update(); }
第二种:
接口和实现类和上一种相同,MyAspect类不同
package demoAspectj; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Pointcut; /** * Created by mycom on 2018/3/12. */ public class MyAspectj { // 前置通知 public void before(){ System.out.println("前置通知方法before()"); } public void before(JoinPoint jp){ System.out.println("前置通知方法before()jp="+jp); } // 后置通知 public void afterReturing(){ System.out.println("后置通知方法"); } public void afterReturing(String result){ System.out.println("后置通知方法 result = " + result); } // 环绕通知 public void around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕通知方法,目标方法执行之前"); // 执行目标方法 Object result = pjp.proceed(); System.out.println("环绕通知方法,目标方法执行之后"); } // 异常通知 public void afterThrowing(){ System.out.println("异常通知方法"); } // 最终通知 public void after(){ System.out.println("最终通知方法"); } }
配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--1.目标对象--> <bean id="service" class="demoAspectj.UserServiceImpl"></bean> <!--2.增强/通知--> <bean id="myAspectj" class="demoAspectj.MyAspectj"></bean> <!-- aop配置 --> <aop:config> <aop:pointcut expression="execution(* demoAspectj.*.select(..))" id="doFirstPointcut"/> <aop:pointcut expression="execution(* demoAspectj.*.update(..))" id="doSecondPointcut"/> <aop:pointcut expression="execution(* demoAspectj.*.insert(..))" id="doThirdPointcut"/> <aop:aspect ref="myAspectj"> <aop:before method="before" pointcut-ref="doFirstPointcut"/> <aop:after-returning method="afterReturing" pointcut-ref="doSecondPointcut"/> <aop:around method="around" pointcut-ref="doThirdPointcut" /> </aop:aspect> </aop:config> </beans>
测试方法
@Test public void t2(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextAspect.xml"); IUserService service =(IUserService) context.getBean("service"); service.select(); service.update(); service.insert(); }