Spring AOP:基于AspectJ注解开发
2023-06-13 09:20:10 时间
在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。
定义切面@Aspect
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Logging { * 定义切入点 @Pointcut( execution(* net.biancheng.*.*(..)) ) private void selectAll() { * 前置通知 @Before( selectAll() ) public void beforeAdvice() { System.out.println( 前置通知 * 后置通知 @After( selectAll() ) public void afterAdvice() { System.out.println( 后置通知 * 返回后通知 @AfterReturning(pointcut = selectAll() , returning = retVal ) public void afterReturningAdvice(Object retVal) { System.out.println( 返回值为: + retVal.toString()); * 抛出异常通知 @AfterThrowing(pointcut = selectAll() , throwing = ex ) public void afterThrowingAdvice(IllegalArgumentException ex) { System.out.println( 这里的异常为: + ex.toString()); }
为此,AspectJ 框架为 AOP 开发提供了一套注解。AspectJ 允许使用注解定义切面、切入点和增强处理,Spring 框架可以根据这些注解生成 AOP 代理。
关于注解的介绍如表 1 所示。
定义切面@Aspect
AspectJ 类和其它普通的 Bean 一样,可以有方法和字段,不同的是 AspectJ 类需要使用 @Aspect 注解,如下所示。
package net.biancheng; import org.aspectj.lang.annotation.Aspect; @Aspect public class AspectModule { }
AspectJ 类也可以像其它 Bean 一样在 XML 中配置,如下。
bean id = myAspect class = net.biancheng.AspectModule /bean定义切入点@Pointcut
@Pointcut 注解用来定义一个切入点,如下。
// 要求:方法必须是private,返回值类型为void,名称自定义,没有参数 @Pointcut( execution(*net.biancheng..*.*(..)) ) private void myPointCut() { }
相当于以下代码
aop:pointcut expression= execution(*net.biancheng..*.*(..)) id= myPointCut /
关于 execution 中表达式的使用说明,我们在《AspectJ基于XML开发AOP》一节介绍。
定义通知advice@AspectJ 支持 5 种类型的 advice,以下为使用 @Before 的示例。
@Before( myPointCut() ) public void beforeAdvice(){ }
下面使用 Eclipse IDE 演示 AspectJ 基于注解开发 AOP,步骤如下:
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Logging { * 定义切入点 @Pointcut( execution(* net.biancheng.*.*(..)) ) private void selectAll() { * 前置通知 @Before( selectAll() ) public void beforeAdvice() { System.out.println( 前置通知 * 后置通知 @After( selectAll() ) public void afterAdvice() { System.out.println( 后置通知 * 返回后通知 @AfterReturning(pointcut = selectAll() , returning = retVal ) public void afterReturningAdvice(Object retVal) { System.out.println( 返回值为: + retVal.toString()); * 抛出异常通知 @AfterThrowing(pointcut = selectAll() , throwing = ex ) public void afterThrowingAdvice(IllegalArgumentException ex) { System.out.println( 这里的异常为: + ex.toString()); }
Man 类代码如下。
package net.biancheng; public class Man { private String name; private int age; public String getName() { return name; public void setName(String name) { this.name = name; public int getAge() { return age; public void setAge(int age) { this.age = age; public void throwException() { System.out.println( 抛出异常 throw new IllegalArgumentException(); }
Beans.xml 代码如下。
?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 xsi:schemaLocation= http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd aop:aspectj-autoproxy / bean id= man >MainApp 类代码如下。
package net.biancheng; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( Beans.xml Man man = (Man) context.getBean( man man.getName(); man.getAge(); man.throwException(); }运行结果如下。
前置通知
后置通知
返回值为:bianchengbang
前置通知
后置通知
返回值为:12
前置通知
抛出异常
后置通知
这里的异常为:java.lang.IllegalArgumentException4592.html
springSpring CloudSpring DataSpring frameworkSpring SecuritySpringBootSpringMVC
相关文章
- Spring学习笔记(五)——JdbcTemplate和spring中声明式事务
- batch spring 重复执行_Spring Batch批处理
- Spring——【第一章入门】:核心Aop与Ioc
- Spring、Spring MVC和Spring Boot
- 将Bean交给Spring容器管理的几种方式
- 【10】Spring源码-分析篇-AOP源码分析
- springioc和aop原理_描述spring框架的工作原理
- aop事务管理-Spring事务实现原理
- Spring AOP 笔记 简单易解
- Spring AOP:面向切面编程的利器
- Spring Boot集成Disruptor
- 基于Spring boot的外卖项目瑞吉外卖Day10使用Redis缓存技术
- 使用Spring定时任务并且通过AOP监控任务执行情况详解编程语言
- spring aop详解编程语言
- Spring与多线程详解编程语言
- Spring事务处理的实现详解编程语言
- Spring Boot2.0之热部署原理详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring通知类型及使用ProxyFactoryBean创建AOP代理
- Spring框架致力于搭建基于MSSQL的稳健开发体系(spring mssql)