springboot 中使用AOP
网上关于AOP的例子好多,各种名词解释也一大堆,反正名词各种晦涩,自己写个最最最简单的例子入门mark一下,以后再深入学习。
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
定义切面
@Aspect
@Configuration
public class AopConfiguration { }
切面内定义切入点,就是执行的条件
@Pointcut("execution(* com.test.service.*.*(..))")
public void executeService()
{ }
切入点的方法不用任何代码,返回值是void,最重要的是执行的条件的表达式:
1)execution:用于匹配子表达式。 //匹配com.cjm.model包及其子包中所有类中的所有方法,返回类型任意,方法参数任意
@Pointcut("execution(* com.cjm.model..*.*(..))")
public void before(){} 2)within:用于匹配连接点所在的Java类或者包。 //匹配Person类中的所有方法
@Pointcut("within(com.cjm.model.Person)")
public void before(){} //匹配com.cjm包及其子包中所有类中的所有方法 @Pointcut("within(com.cjm..*)")
public void before(){} 3) this:用于向通知方法中传入代理对象的引用。
@Before("before() && this(proxy)")
public void beforeAdvide(JoinPoint point, Object proxy){
//处理逻辑
} 4)target:用于向通知方法中传入目标对象的引用。
@Before("before() && target(target)
public void beforeAdvide(JoinPoint point, Object proxy){
//处理逻辑
} 5)args:用于将参数传入到通知方法中。
@Before("before() && args(age,username)")
public void beforeAdvide(JoinPoint point, int age, String username){
//处理逻辑
} 6)@within :用于匹配在类一级使用了参数确定的注解的类,其所有方法都将被匹配。 @Pointcut("@within(com.cjm.annotation.AdviceAnnotation)") - 所有被@AdviceAnnotation标注的类都将匹配
public void before(){} 7)@target :和@within的功能类似,但必须要指定注解接口的保留策略为RUNTIME。
@Pointcut("@target(com.cjm.annotation.AdviceAnnotation)")
public void before(){} 8)@args :传入连接点的对象对应的Java类必须被@args指定的Annotation注解标注。
@Before("@args(com.cjm.annotation.AdviceAnnotation)")
public void beforeAdvide(JoinPoint point){
//处理逻辑
} 9)@annotation :匹配连接点被它参数指定的Annotation注解的方法。也就是说,所有被指定注解标注的方法都将匹配。
@Pointcut("@annotation(com.cjm.annotation.AdviceAnnotation)")
public void before(){} 10)bean:通过受管Bean的名字来限定连接点所在的Bean。该关键词是Spring2.5新增的。
@Pointcut("bean(person)")
public void before(){}
接下来定义通知,就是行为呗,常用的包括Before,Around,After等
@Before("executeService() &&"+"args(name)")
public void before(String name)
{
System.out.println("before say hello");
System.out.println("name = "+name);
} @AfterReturning("executeService()")
public void after()
{
System.out.println("after return");
}
这样调用com.test.service包下所有类的方法前都先回执行@Before的行为,结束都会执行@AfterReturning的行为。
更为详细的介绍参考:http://www.cnblogs.com/lic309/p/4079194.html
springboot 中使用AOP的更多相关文章
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- SpringBoot图文教程5—SpringBoot 中使用Aop
有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...
- 编写SpringBoot 中的AOP
编写SpringBoot 中的AOP 在程序开发的过程中会使用到AOP的思想,面向切面进行开发,比如登录的验证,记录日志等等-频繁需要操作的步骤,在遇到这种情况时就要使用Spring 的AOP了 Sp ...
- SpringBoot中使用AOP打印接口日志的方法(转载)
前言 AOP 是 Aspect Oriented Program (面向切面)的编程的缩写.他是和面向对象编程相对的一个概念.在面向对象的编程中,我们倾向于采用封装.继承.多态等概念,将一个个的功能在 ...
- spring-boot中的AOP
public class User { private Integer id; private String username; private String note; public User(In ...
- SpringBoot中使用AOP实现计算Service执行时间
1.增加POM.XML的依赖架包 <!-- 引入 spring aop 依赖 --><dependency> <groupId>org.springframewor ...
- springboot中使用aop技术
aop是面向切面编程的意思,它可以需要先选择一些切入点,然后对这些切入点进行拦截,注入统一的代码逻辑,这也是解耦的一种方式,也是为了避免重复的代码,让开发人员把关注点放在业务上. 引用包 'org.s ...
- SpringBoot图文教程6—SpringBoot中过滤器的使用
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文系列教程技术大纲 鹿老师的Java笔记 SpringBo ...
随机推荐
- FZU-2268 Cutting Game(二进制使用)
Problem 2268 Cutting Game Accept: 254 Submit: 605Time Limit: 1000 mSec Memory Limit : 32768 K ...
- OpenResty域名could not be resolved及dnsmasq配置
在本地开发中使用自己配置的域名例如:wuyachao.com配置在/etc/hosts,ping wuyachao.com显示ip为127.0.0.1,在使用lua_resty_http时候,会报错 ...
- hdu 1599 find the mincost route 最小环
题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...
- ttServer缓存的简单使用
ttserver是一款 DBM 数据库,该数据库读写非常快,哈希模式写入100万条数据只需0.643秒,读取100万条数据只需0.773秒,是 Berkeley DB 等 DBM 的几倍.利用Toky ...
- Chromium和Chrome的区别
1.Chromium是谷歌的开源项目,开发者们可以共同去改进它,然后谷歌会收集改进后的Chromium并发布改进后安装包.Chrome不是开源项目,谷歌会把Chromium的东西更新到Chrome中. ...
- 【安居客】资深PHP软件开发工程师
工作职责: 1.网站项目的开发和维护: 2.负责技术部软件开发架构设计: 3.负责生产环境.测试环境和生产环境服务器运维和优化: 4.负责研究较前沿和复杂的技术运用: 岗位要求: 1.熟悉 PHP 程 ...
- 设计模式之模板模式(PHP实现)
github地址:https://github.com/ZQCard/design_pattern * 在模板模式(Template Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板. ...
- C++之纯虚函数
1. 纯虚函数形式 class Parent { public: ; }; 代码中的func1就是纯虚函数,没有函数体,没有函数的具体实现,有virtual,在函数名后面带有“ = 0”形式: 2.对 ...
- oracle sql试题
转载 数据准备 create table student( sno varchar2(10) primary key, sname varchar2(20), sage number(3), ...
- 编译spark源码及塔建源码阅读环境
编译spark源码及塔建源码阅读环境 (一),编译spark源码 1,更换maven的下载镜像: <mirrors> <!-- 阿里云仓库 --> <mirror> ...