spring 的 切片Aspect
语法:
<aop:config>
<!-- 配置多个切点,&& || ! -->
<aop:pointcut id="pc" expression="execution(public * com.wtas.*.service.*.*(..)) || execution(public * com.wtas.*.*.service.*.*(..)) || execution(public * com.wtas.*.*.*.service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>
其中,&&,||,可以写成and,or,但是需要注意大小写,我在讲||换成大写的OR的时候,不能进行事务控制,不知道是不是区别别大小写的。
1.controller方法:

@Aspect
@Component
public class TimeAspect { @Around("execution(* com.sea.web.controller.UserController.*(..))")
public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("time aspect start");
//说明:ProceedingJoinPoint 可获取调用的方法的传入参数的值
//如:http:localhost:8080/user/1
//comtroller 方法:public User getInfo(@PathVarable String id)
Object[] args = pjp.getArgs();
for (Object arg : args) {
System.out.println("arg is " + arg); //id 1
}
long start = new Date().getTime();
// 此object 就是条用 方法的返回值 如:User user= UserController.findUser(),
//该方法相当于过滤器中dofilter()方法
Object object = pjp.proceed(); System.out.println("time aspect 耗时:" + (new Date().getTime() - start));
System.out.println("time aspect end");
return object;
} }
//@Around : 包含以下三种
| Before Advice |
代用之前的处理 |
|
After Advice |
调用之后的处理 |
|
Throw Advice |
调用的时候抛出的异常处理 |
execution(* com.sea.web.controller.UserController.*(..))
execution :表示执行
第一个 * :返回值 ;这代表 无论为什么返回值都行
com.sea.web.controller.UserController: 表示那个类
第二个 * :表示方法:类下的所有方法
*(..) :所有方法,任何参数的方法都执行
Anotation注解规则:
@Aspect表示注解的类是抽象的服务方法模块;
@Pointcut定义服务的使用规则,也就是服务方法要应用到目标类中哪些方法上。
@Pointcut("execution(*add*(..))") 第一个*表示不论是否有返回值,所有以add开头的方法,不管是否有参数。
private voidaddAddMethod(){};该方法只是注解模式中一个空的方法体,是一个模式化的方法定义结构,该方法不能有返回值,不能有任何参数,也不能对其进行实现。在Advice中要使用该方法名的标识。
Advice注解checkSecurity()方法,表示该方法是具体的服务方法,使用注解的方式,Advice不出现,而是使用上面理论部分提到的使用@After、@Befor、@Throw来表示,同时要在Advice中关联pointcut中定义的规则。
/*
* 拦截器
*/
@Component
@Aspect //切面
public class NamecheckIntercepter {
@Before("execution(public com.icil.entity.Customer com.icil.service.CustomerServiceImpl.*(..))")
public void beforeInteface(){
System.out.println("findCustomerById "+"切面 调用前*******************"); }
@After("execution(public com.icil.entity.Customer com.icil.service.CustomerServiceImpl.*(..))")
public void afterInteface(){
System.out.println("findCustomerById "+"切面 调用后"); } }
spring 的 切片Aspect的更多相关文章
- spring 的 切片Aspect 最常用记录方法执行时间
/** * */ package com.icil.esolution.aspect; import java.util.Date; import org.aspectj.lang.Proceedin ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- spring aop中aspect和advisor的区别
之前看到spring AOP配置aspect(切面)有两种方式,一种是利用注解的方式配置,一种是利用XML的方式配置. 我们的配置是这样的<aop:aspect>,还有另外一种<ao ...
- Spring AOP 的@Aspect
Spring AOP 的@Aspect 转自:http://blog.csdn.net/tanghw/article/details/3862987 从Spring 2.0开始,可以使用基于sch ...
- 【Spring】基于@Aspect的AOP配置
Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. · 本例演示一个基于@Aspect的小demo 1. ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- Spring切面编程Aspect之@Before和@Around用法
查看dao层使用的sql import java.util.Arrays; import org.apache.commons.lang.ArrayUtils; import org.aspectj. ...
- spring boot使用@Aspect记录日志(请求参数,响应结果)
- Spring Aspect 获取请求参数
切片(Aspect)也就是Spring AOP 实现Aspect的主要步骤: 1.在哪里切入 .在哪个方法起作用 .什么时候起作用 2.起作用的时候执行什么处理逻辑 下面是代码实现 /** * 切片A ...
随机推荐
- Battle City 优先队列+bfs
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- CTF-练习平台-Misc之 Linux??????
八.Linux?????? 下载文件,解压后只得到一个没有后缀名的文件,添加后缀名为txt,打开搜索,关键词为“flag”,没有找到:改关键词为“key”得到答案
- hdu2060-2062
hdu 2060 斯诺克,读懂题意直接模拟 #include<stdio.h> int main(){ int N; ]; a[]=; ;i<=;i++){ a[i]=(-i)*i/ ...
- sdut2408 pick apples (贪心+背包)山东省第三届ACM省赛
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/24642587 本文出自:http://blog.c ...
- 按的第一个greasemonkey插件:评论时可以粘贴啦~~
原来的样子:如果按ctrl+V会跳出错误
- MySQL中character set与collation的理解(转)
character set和collation的是什么? character set即字符集 我们常看到的UTF-8.GB2312.GB18030都是相互独立的character set.即对Unic ...
- 理解js事件循环(event loop)
队列:先进先出 栈:后进先出 javascript的Event Loop 和 Node.js的Event Loop 区别: js(运行在浏览器),有主线程.异步任务队列的概念: node.js使用li ...
- Spring Boot 整合 FastDFS 客户端
原文地址:Spring Boot 整合 FastDFS 客户端 博客地址:http://www.extlight.com 一.前言 前两篇介绍整体上介绍了通过 Nginx 和 FastDFS 的整合来 ...
- 【python】class之类属性
class Foo(object): x=1.5 foo=Foo() print foo.x#通过实例访问类属性 >>>1.5 print Foo.x #通过类访问类属性 >& ...
- theme为dialog的Activity如何充满全屏
转自:http://blog.csdn.net/fzh0803/article/details/9787615 分类: android_点滴记录2013-08-06 10:33 2005人阅读 评论 ...