基于使用AspectJ实现AOP,注解AOP开发

mac2024-05-30  37

1.环境配置,简单的说明。 Spring AOP和Aspect相关jar包,这里直接上代码了

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.41</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>

2.注解开发准备``,配置xml文件,

<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" 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"> <!--开启Aspect自动代理--> <aop:aspectj-autoproxy/> <!--目标类--> <bean id="productDao" class="com.nuc.aspect.ProductDao"/> <!--定义切面类--> <bean id="myAspectAnno" class="com.nuc.aspect.MyAspectAnno"/> </beans>

3.AspectJ提供不同类型的通知,分别为: @Before 前置通知,相当于BeforeAdvice @AfterReturning 后置通知,相当于AfterReturningAdvice @Around 环绕通知,相当于MethodInterceptor @AfterThrowing异常抛出通知,相当于ThrowAdvice @After 最终final通知,不管是否异常,该通知都会执行 4. 几种通知实现代码 很简单,通过execution函数进行定义的。可以定义切点的方法切入 语法 -execution(<访问修饰符><返回类型><方法名>(<参数>)<异常>)!上了代码就好懂了。其中*号与包名有一个空格,不然就报错了。。最后就是别到错包,能bug一整天。。

@Before(value=“execution(* com.nuc.aspect.ProductDao.save(…))”) public void before(JoinPoint joinPoint){

System.out.println("前置通知-----------------"+joinPoint); } @AfterReturning(value = "execution(* com.nuc.aspect.ProductDao.update(..))",returning = "result") public void afterReturning(Object result){ System.out.println("后置通知-------------------"+result); } @Around(value = "execution(* com.nuc.aspect.ProductDao.delete(..))") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕前通知-------------------"); Object obj = proceedingJoinPoint.proceed(); System.out.println("环绕后通知-------------------"); return obj; } @AfterThrowing(value = "execution(* com.nuc.aspect.ProductDao.findOne(..))",throwing = "e") public void afterThrowing(Throwable e){ System.out.println("异常抛出通知------------------"+e.getMessage()); } @After(value = "execution(* com.nuc.aspect.ProductDao.findOne(..))") public void after(){ System.out.println("最终通知-----------------------"); }
最新回复(0)