AOP学习(一)

1.简介

  AOp:面向切面编程,相对于OOP面向对象编程。

  Spring的AOP的存在目的是为了解耦。AOP可以让一切类共享相同的行为。在OOP中只能通过继承类或者实现接口,使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足。

  Spring支持AspectJ的注解式切面编程。

  (1)使用@Aspect声明是一个切面;

  (2)使用@After、@Before、@Around定义通知(Advice),可直接将拦截规则(切点)作为参数;

  (3)其中@After、@Before、@Around参数的拦截规则为切点(Pointcut),为了使切点复用,可使用@Pointcut 专门定义拦截规则,然后在@After、@Before、@Around的参数中调用;

  (4)其中符合条件的每一个被拦截处为连接点(JoinPoint)。

  下面示例将演示基于注解拦截和基于方法规则拦截两种方式,演示一种模拟记录操作的日志系统的实现。其中注解式拦截能够很好地控制要拦截的粒度和获得更丰富的信息,Spring本身在事务处理(@Transcational)和数据缓存(@Cacheable)等都使用此种形式的拦截。

2.示例

(1)新建springboot项目,在pom.xml文件中添加spring aop  以及支持AspectJ 依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency> (2)注解拦截,编写拦截规则的注解
/**
* 自定义拦截规则注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
} (3)编写使用注解拦截的 业务类
/**
* 使用注解的被拦截类
*/
@Service
public class DemoAnnotationService { @Action(name="注解式拦截的add操作")
public void add(){}
}
(4)编写使用方法规则拦截的 业务类
/**
* 使用方法规则的被拦截类
*/
@Service
public class DemoMethodService {
public void add(){}
} (5)编写切面
/**
* 切面
*/
@Aspect//注解声明一个切面
@Component//让切面成为Spring容器管理的Bean
public class LoginAspect { //通过@Pointcut注解声明切点
@Pointcut("@annotation(com.wenhuang.springboot.aop.Action)")
public void annotationPointCut(){}; //通过@After 注解声明一个通知,并使用@Pointcut定义切点
@After("annotationPointCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method =signature.getMethod();
Action action =method.getDeclaredAnnotation(Action.class);
System.out.println("注解式拦截,"+ action.name());//通过反射获取注解上的属性
} //通过@Before 注解声明一个通知,直接使用拦截器规则作为参数
@Before("execution(* com.wenhuang.springboot.aop.service.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
Method method =signature.getMethod();
System.out.println("方法规则式拦截,"+method.getName());
}
}
(6)配置类
@Configuration
@ComponentScan("com.wenhuang.springboot.aop")
@EnableAspectJAutoProxy //使用该注解开启Spring对AspectJ代理的支持
public class AopConfig {
}
(7)运行
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();
}
} 结果如下

												

springBoot AOP学习(一)的更多相关文章

  1. SpringBoot+Shiro学习(七):Filter过滤器管理

    SpringBoot+Shiro学习(七):Filter过滤器管理 Hiwayz 关注  0.5 2018.09.06 19:09* 字数 1070 阅读 5922评论 1喜欢 20 先从我们写的一个 ...

  2. Springboot 框架学习

    Springboot 框架学习 前言 Spring Boot是Spring 官方的顶级项目之一,她的其他小伙伴还有Spring Cloud.Spring Framework.Spring Data等等 ...

  3. springboot+aop切点记录请求和响应信息

    本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...

  4. SpringBoot+AOP整合

    SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...

  5. springboot aop 不生效原因解决

    最近参照资料创建Springboot AOP ,结果运行后aop死活不生效. 查明原因: 是我在创建AOP类时选择了Aspect类型,创建后才把这个文件改为Class类型,导致一直不生效, 代码配置这 ...

  6. springboot aop 自定义注解方式实现完善日志记录(完整源码)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...

  7. springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)

    https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...

  8. Aop学习笔记

    在学习编程这段时间我想大家都是习惯了面向过程或者面向对象的思想来编程,较少或者没有接触过面向方面编程的思想. 那么什么是面向方面(Aspect)——其实就是与核心业务处理逻辑无关的切面,例如记录日志. ...

  9. spring 学习(三):aop 学习

    spring 学习(三):aop 学习 aop 概念 1 aop:面向切面(方面)编程,扩展功能不修改源代码实现 2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码 3 aop底层使用动态代 ...

随机推荐

  1. 【JS】【3】标签显示几秒后自动隐藏

    $("#XXX").show().delay(2000).hide(0); 2000,0:可选,速度,(毫秒:"slow":"fast") ...

  2. meta标签常用设置

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  3. ayit-#41. 因数的个数-数论

    搞了两天发现是qpow时大数相乘爆精度了,以前没遇到过,因为大数检测时模数达到了1e18,所以qpow可能会爆,应该利用快速幂原理写一个快速加即可. 先筛出1e6以内的质数,然后把x里<=1e6 ...

  4. MongoDB 教程(四):MongoDB 概念解析

    概述: 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是文档.集合.数据库,下面我们挨个介绍. 下表将帮助您更容易理解Mongo中的一些概念: 数据库 先运行数据库 C: ...

  5. [codechef July Challenge 2017] IPC Trainers

    IPCTRAIN: 训练营教练题目描述本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日程安排有 M 天,每天最多上一节课.第 i 名教练在第 ...

  6. Thinkphp5 Nginx Pathinfo配置

    server { listen ; server_name sui.com; root /tmmee/sad.cn/public; index index.php index.html index.h ...

  7. zk 创建瞬时、顺序节点的原理

    命令: create -s -e /worker/lock xx zk 的实现代码在:PrepRequestProcessor.pRequest2Txn 中 //The number of chang ...

  8. js正则匹配以某字符串开始字符串

    let decode_sql ="select * from table where create_user='user' order by id desc";   decode_ ...

  9. JAVA按数字,字母排序,但不包括大小写和汉字排序

    public class ABC { public static void main(String[] args) { new ABC().sortStringArray(); } public vo ...

  10. bzoj4278

    题解: 把第一个串放好,加一个oo 然后把第二个串倒序放进来 然后就是http://www.cnblogs.com/xuanyiming/p/8510231.html这一题了 代码: #include ...