TreeMind树图在线AI思维导图
当前位置:树图思维导图模板IT互联网互联网干货Java-SpringAOP-面向切面编程思维导图

Java-SpringAOP-面向切面编程思维导图

  收藏
  分享
免费下载
免费使用文件
U913167374 浏览量:142022-11-25 15:41:59
已被使用0次
查看详情Java-SpringAOP-面向切面编程思维导图

讲述了Java:SpringAOP-面向切面编程,包括:编写拦截规则的注解、编写被拦截类。

树图思维导图提供 Java-SpringAOP-面向切面编程 在线思维导图免费制作,点击“编辑”按钮,可对 Java-SpringAOP-面向切面编程  进行在线思维导图编辑,本思维导图属于思维导图模板主题,文件编号是:24f021ae703f709ce5b5552429a3221f

思维导图大纲

Java-SpringAOP-面向切面编程思维导图模板大纲

添加spring aop支持及AspectJ依赖

<!-- spring aop支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- aspectj支持 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency>

配置类

package com.wisely.highlight_spring4.ch1.aop; @Configuration @ComponentScan("com.wisely.highlight_spring4.ch1.aop") @EnableAspectJAutoProxy //1 开启Spring对AspectJ代理的支持。 public class AopConfig { }

编写拦截规则的注解

package com.wisely.highlight_spring4.ch1.aop; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }

编写被拦截类

使用注解

package com.wisely.highlight_spring4.ch1.aop; @Service public class DemoAnnotationService { @Action(name="注解式拦截的add操作") public void add(){} }

使用方法规则

package com.wisely.highlight_spring4.ch1.aop; @Service public class DemoMethodService { public void add(){} }

编写切面

package com.wisely.highlight_spring4.ch1.aop; @Aspect //1 声明一个切面 @Component //2 让此切面成为Spring容器管理的Bean。 public class LogAspect { @Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") //3 声明切点。 public void annotationPointCut(){}; @After("annotationPointCut()") //4 声明一个建言,并使用@PointCut定义的切点 public void after(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.out.println("注解式拦截 " + action.name()); //5 通过反射可获得注解上的属性,然后做日志记录相关的操作,下面的相同。 } @Before("execution(* com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") //6 声明一个建言,此建言直接使用拦截规则作为参数。 public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截,"+method.getName()); } }

运行

public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); //1 DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); context.close(); } }

输出

注解式拦截 注解式拦截的add操作 方法规则式拦截, add

相关思维导图模板

Lot Track In 代码逻辑思维导图

树图思维导图提供 Lot Track In 代码逻辑 在线思维导图免费制作,点击“编辑”按钮,可对 Lot Track In 代码逻辑  进行在线思维导图编辑,本思维导图属于思维导图模板主题,文件编号是:e30c4a1ae784f2017b25722a66898e98

高质量编程及测试思维导图

树图思维导图提供 高质量编程及测试 在线思维导图免费制作,点击“编辑”按钮,可对 高质量编程及测试  进行在线思维导图编辑,本思维导图属于思维导图模板主题,文件编号是:cc033ab916679f39db338cb5a460d78d