语法: 

    <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的更多相关文章

  1. spring 的 切片Aspect 最常用记录方法执行时间

    /** * */ package com.icil.esolution.aspect; import java.util.Date; import org.aspectj.lang.Proceedin ...

  2. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  3. spring aop中aspect和advisor的区别

    之前看到spring AOP配置aspect(切面)有两种方式,一种是利用注解的方式配置,一种是利用XML的方式配置. 我们的配置是这样的<aop:aspect>,还有另外一种<ao ...

  4. Spring AOP 的@Aspect

    Spring AOP 的@Aspect   转自:http://blog.csdn.net/tanghw/article/details/3862987 从Spring 2.0开始,可以使用基于sch ...

  5. 【Spring】基于@Aspect的AOP配置

    Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. ·   本例演示一个基于@Aspect的小demo 1. ...

  6. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  7. Spring切面编程Aspect之@Before和@Around用法

    查看dao层使用的sql import java.util.Arrays; import org.apache.commons.lang.ArrayUtils; import org.aspectj. ...

  8. spring boot使用@Aspect记录日志(请求参数,响应结果)

  9. Spring Aspect 获取请求参数

    切片(Aspect)也就是Spring AOP 实现Aspect的主要步骤: 1.在哪里切入 .在哪个方法起作用 .什么时候起作用 2.起作用的时候执行什么处理逻辑 下面是代码实现 /** * 切片A ...

随机推荐

  1. com.fasterxml.jackson.databind.ObjectMapper. .readValue .convertValue

    String str="{\"student\":[{\"name\":\"leilei\",\"age\": ...

  2. 【HAOI2008】玩具命名

    水题大失败 原题: 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自 ...

  3. mysql 如何选择随机行

    最简单的方式是使用 mysql 的 ORDER BY RAND() 子句. SELECT  col1  FROM  tbl  ORDER  BY  RAND()  LIMIT  10; 它能很好的运行 ...

  4. C#常用插件和工具

    Code generation(代码自动生成) NVelocity CodeSmith X-Code .NET XGoF - NMatrix / DEVerest Compilation(编译工具) ...

  5. 设置Linux系统的空闲等待时间TMOUT

    为了增强Linux系统的安全性,我们需要在用户输入空闲一段时间后自动断开,这个操作可以由设置TMOUT值来实现.将以下字段加入到/etc/profile 中即可(对所有用户生效). export TM ...

  6. winform 程序调用及参数调用

    调用程序: // 新轮廓 -> 调用轮廓扫描程序        private void toolStripMenuItem9_Click(object sender, EventArgs e) ...

  7. The superclass "javax.servlet.http.HttpServlet" was not found

    在eclipse中,需要通过

  8. C#连接Oracle数据库的连接字符串

    来源:http://blog.csdn.net/superhoy/article/details/8108037 两种方式:1.IP+SID方式 2.配置链接方式 1..IP+SID方式 DbHelp ...

  9. android 布局入门

    一.LinearLayout RelativeLayout 这俩的区别详见这里 http://www.cnblogs.com/duanweishi/p/4244233.html 二.android:l ...

  10. Linux下分析磁盘镜像

    我们知道Windows下可以使用WinHex分析磁盘镜像:Linux下可以使用losetup工具.假如我们有一个磁盘镜像disk.img: ## 首先你的系统要支持loop device ## 一般发 ...