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. 封装jsonp

    1.写一个类封装jsonp: jsonp(url, params, success, funName)     参数url:请求地址     参数params:请求数据,可以是json对象,或形如&q ...

  2. Git将本地项目上传到GitHub

    本文转载于:https://segmentfault.com/a/1190000011909294 https://www.cnblogs.com/cxk1995/p/5800196.html 我们使 ...

  3. bootstrap table导出功能无效报错Uncaught INVALID_CHARACTER_ERR: DOM Exception 5和导出中文乱码问题

    由于表格数据中含有中文导致的,在网页的开发者选项中报一个 Uncaught INVALID_CHARACTER_ERR: DOM Exception 5 问题.这个问题是由于BootStrap tab ...

  4. Oracle 11.2.0.4.0 Dataguard部署和日常维护(1)-数据库安装篇

    本次测试环境 系统版本 CentOS release 6.8 主机名 ec2t-userdata-01 ec2t-userdata-01 IP地址 10.189.102.118 10.189.100. ...

  5. js浮点数相加、减、乘、除精确计算

    js 浮点数计算时 ,无缘无辜 后边冒出一堆 小数点………… 貌似js本身的问题,类型不定?????? 只能自己写函数处理..  http://blog.csdn.net/w4bobo/article ...

  6. redisObject

    typedef struct redisObject {    unsigned type:4;    unsigned encoding:4;    unsigned lru:REDIS_LRU_B ...

  7. 使用python操作hdfs,并grep想要的数据

    代码如下: import subprocess for day in range(24, 30): for h in range(0, 24): filename = "tls-metada ...

  8. moment.js 常用(几天前、相差几天、自然周、自然月)

    let pickDate = moment(this.searchForm.date); let firstDay = pickDate.day(0).format('YYYYMMDD');//上周天 ...

  9. Tomcat禁用SSLv3和RC4算法

    1.禁用SSLv3(SSL 3.0 POODLE攻击信息泄露漏洞(CVE-2014-3566)[原理扫描]) 编缉$CATALINA_HOEM/conf/server.xml配置文件,找到https端 ...

  10. 微信PC客户端无法发送图片,怎么解决?

    今天登陆电脑的微信客户端,无法发送截图图片,该怎么办? 解决方法 1.在任务栏找到程序,右键找到设置