@Aspect 示例 及XML方式的xml文件的配置

 

 

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogInterceptor {
	@Pointcut("execution(public * com.userService.*.add(..))")	// 统配方式
	public void mytime(){}	//当多个同时使用同样的
	
	long startTime,endTime;
	@Before("mytime()")
	public void before(){
		startTime=System.nanoTime();
	}
	@After("mytime()")
	public void after(){
		endTime=System.nanoTime();
		System.out.println("程序运行时间: "+(endTime-startTime)+"纳秒");
	}
}

 

=======================================================================


使用XML 配置Aspect 可省略类中的注解

 <!-- 声明类 ,此类作为切面类使用  -->
 <bean id="logInterceptor" class="com.aop.LogInterceptor" />
 <aop:config>
  <!-- 设置切面名,及切面类 -->
  <aop:aspect id="logAspect" ref="logInterceptor">
  
   <!-- 运行前方法配置,先择要执行的方法 ,并设置切入点  -->
   <aop:before method="before" pointcut="execution(public * com.userService.*.add(..))" />
   
   <!-- 先设置切入点,待使用  -->
   <aop:pointcut id="servicePointcut" expression="execution(public * com.userService.*.add(..))" />
   
   <!-- 运行后方法配置,先择要执行的方法,参考预先设置好的切入点  -->
   <aop:after method="after" pointcut-ref="servicePointcut" />
   
  </aop:aspect>
 </aop:config>

 

切面类 如下: 

package com.aop;

public class LogInterceptor {
		
	long startTime,endTime;
	public void before(){
		startTime=System.nanoTime();
	}
	public void after(){
		endTime=System.nanoTime();
		System.out.println("程序运行时间: "+(endTime-startTime)+"纳秒");
	}
}


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值