AspectJ简介
- 它不属于spring;
- AspectJ是一个AOP的框架;
- 定义了AOP语法;
- 有一个专门的编译器用来生成遵守Java字节编码规范的Class文件
什么是AspectJ
- AspectJ是使用面向切面的一个框架
- 它扩展了Java语言(它本身也是一种语言)
- 支持原生Java代码 有自己的编译器
- 将代码翻译成Java字节码文件
- 是为了方便编写AOP代码而出现的
- 使用AOP编程的三个重点 通知 切点 织入
实现AOP的方式
原生使用切面
添加AOP坐标
1 | org.springframework.bootspring-boot-starter-aop |
创建增强类MyAOP
对service层下的所有类的所有方法进行增强
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @Component @Aspect public class MyAOP { //定义切点 @Pointcut ( "execution(* com.moming.service.*.*(..))" ) public void point(){} @Before ( "point()" ) public void before(){ System.out.println( "===>前置通知" ); } @After ( "point()" ) public void after(){ System.out.println( "===>后置通知" ); } @Around ( "point()" ) public Object arround(ProceedingJoinPoint pjp) throws Throwable { System.out.println( "===>环绕前" ); Object resules = pjp.proceed(); System.out.println( "===>环绕后" ); return resules; } @AfterReturning (value = "point()" ,returning = "ret" ) public void returning(JoinPoint jp, String ret){ Object[] args = jp.getArgs(); System.out.println( "返回后通知获取参数: " +Arrays.toString(args)); System.out.println( "===>返回后通知,返回值: " +ret); } @AfterThrowing ( "point()" ) public void throwing(){ System.out.println( "===>异常通知" ); } } |
service/OrderService
1 2 3 4 5 6 7 | @Service public class OrderService { public String order( int id){ System.out.println( "===>目标方法:订单业务ID:" +id); return "001202210121010" ; } } |
启动类测试
1 2 3 4 5 6 7 8 | @SpringBootApplication public class App2 { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App2. class , args); OrderService bean = context.getBean(OrderService. class ); System.out.println(bean.order( 1 )); } } |
无异常时
有异常时,后续代码就不再执行了
补充配置说明
//两种占位符
//* 代表的是一个单词,b* 代表的是以b开头的单词。 例如 bds
//.. 通配符 ,代表的是0个或者多个匹配项
通过注解使用切面
声明注解NeedCut
1 2 3 4 5 | @Documented @Retention (RetentionPolicy.RUNTIME) @Target ({ElementType.TYPE,ElementType.METHOD}) public @interface NeedCut { } |
切换注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @Component @Aspect public class MyAOP { //定义切点 @Pointcut ( "@annotation(com.moming.annotation.NeedCut)" ) public void point(){} @Before ( "point()" ) public void before(){ System.out.println( "===>前置通知" ); } @After ( "point()" ) public void after(){ System.out.println( "===>后置通知" ); } @Around ( "point()" ) public Object arround(ProceedingJoinPoint pjp) throws Throwable { System.out.println( "===>环绕前" ); Object resules = pjp.proceed(); System.out.println( "===>环绕后" ); return resules; } @AfterReturning (value = "point()" ,returning = "ret" ) public void returning(JoinPoint jp, String ret){ Object[] args = jp.getArgs(); System.out.println( "返回后通知获取参数: " +Arrays.toString(args)); System.out.println( "===>返回后通知,返回值: " +ret); } @AfterThrowing ( "point()" ) public void throwing(){ System.out.println( "===>异常通知" ); } } |
使用注解@NeedCut
1 2 3 4 5 6 7 8 9 10 11 | @Service public class OrderService { @NeedCut public String order( int id){ System.out.println( "===>目标方法:订单业务ID:" +id); return "001202210121010" ; } public void add(){ System.out.println( "===>添加订单" ); } } |
测试
1 2 3 4 5 6 7 8 9 10 | @SpringBootApplication public class App2 { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App2. class , args); OrderService bean = context.getBean(OrderService. class ); System.out.println(bean.order( 01 )); System.out.println( "-------------------" ); bean.add(); } } |
使用@NeedCut注解的方法才进行增强
到此这篇关于SpringBoot AOP AspectJ切面技术介绍与实现方式的文章就介绍到这了,更多相关SpringBoot AOP AspectJ切面内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!