AOP-面向切面编程
2018-01-29 10:09
281 查看
1、AOP简介:
URL:https://baike.baidu.com/item/AOP/1332219?fr=aladdin
2、用例:
(1)添加spring aop支持AspectJ依赖。
(2)编写拦截规则注解
(3)编写使用注解的被拦截类
(4)编写使用方法规则拦截类
(5)编写切面
(6)配置类
(7)运行
(8)运行结果:
URL:https://baike.baidu.com/item/AOP/1332219?fr=aladdin
2、用例:
(1)添加spring aop支持AspectJ依赖。
<!-- spring aop 支持--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.11.RELEASE</version> </dependency> <!-- aspectj 支持--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.8</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency>
(2)编写拦截规则注解
package com.houkang.aop; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }
(3)编写使用注解的被拦截类
package com.houkang.aop; import org.springframework.stereotype.Service; @Service public class DemoAnnotationService { @Action(name = "注解式拦截的add操作") public void add(){} }
(4)编写使用方法规则拦截类
package com.houkang.aop; import org.springframework.stereotype.Service; @Service public class DemoMethodService { public void add(){} }
(5)编写切面
package com.houkang.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component public class LogAspect { @Pointcut("@annotation(com.houkang.aop.Action)") public void annotationPointCut() { } @After("annotationPointCut()") 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()); } @Before("execution(* com.houkang.aop.DemoMethodService.* (..))") public void before(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截," + method.getName()); } }
(6)配置类
package com.houkang.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @ComponentScan("com.houkang.aop") @EnableAspectJAutoProxy public class AopConfig { }
(7)运行
package com.houkang.aop; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); context.close(); } }
(8)运行结果:
一月 29, 2018 10:06:17 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@254989ff: startup date [Mon Jan 29 10:06:17 CST 2018]; root of context hierarchy 注解式拦截 注解式拦截的add操作 方法规则式拦截,add 一月 29, 2018 10:06:17 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose 信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@254989ff: startup date [Mon Jan 29 10:06:17 CST 2018]; root of context hierarchy
相关文章推荐
- 面向切面编程(AOP)的理解
- 【spring系列】(1)——对于面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- AOP(面向切面编程)
- 轻松理解AOP(面向切面编程)
- 第八讲 面向切面编程(AOP)
- 大数据WEB阶段Spring框架 AOP面向切面编程(一)
- Spring AOP——面向切面编程(上)
- 【spring框架】(三)面向切面编程(AOP)
- AOP面向切面编程
- spring Aop 面向切面编程简单实例
- AOP(面向切面编程)
- 面向切面编程(1):AOP介绍
- Spring AOP(面向切面编程)【AOP中的术语】
- AOP 面向切面编程
- Spring实践之面向切面编程(AOP)
- AOP面向切面编程
- 【spring源码学习】spring的AOP面向切面编程的实现解析
- AOP:面向切面编程
- 【转载】面向切面编程(AOP)学习