切面功能添加日志

1、自定义注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public  @interface LogTarget {
    String operationDesc() default "";//操作详情operationDesc
    String operationType() default "";// 操作类型operationType 
}

2、切面类

@Aspect
@Component
public class Aop2Log {

	@Autowired
	private IDbSupport dbsupport;

	@Autowired
	private TransactionTemplate transactionTemplate;
	
	/**
	 * 注解切入点
	 */
	@Pointcut("@annotation(awb.operations.aop.LogTarget)")
	public void logTarget() {
	}

	/**
	 * update切入点
	 */
	@Pointcut("execution(* awb.operations.service..*.*Service.update*(..))")
	public void update() {
	}

	/**
	 * save切入点
	 */
	@Pointcut("execution(* awb.operations.service..*.*Service.save*(..))")
	public void save() {
	}

	/**
	 * create切入点
	 */
	@Pointcut("execution(* awb.operations.service..*.*Service.create*(..))")
	public void create() {
	}

	/**
	 * delete切入点
	 */
	@Pointcut("execution(* awb.operations.service..*.*Service.delete*(..))")
	public void delete() {
	}

	/**
	 * add切入点
	 */
	@Pointcut("execution(* awb.operations.service..*.*Service.add*(..))")
	public void add() {
	}
	
	
	@AfterReturning(value = "logTarget()")
	public void afterOperate(JoinPoint point) throws Exception{
        //具体操作
    }
    
    @AfterThrowing(value = "logTarget()",throwing = "e")
	public void afterOperateThrow(JoinPoint point,Throwable e) throws Exception{
    //具体操作
    }
}


类上加入注解@Aspect,定义切入点,@Pointcut(表达式),具体可以直接搜索表达式怎么写。操作成功的方法上的注解为@AfterReturning,值为logTarget()方法,即它的切人点是logTarget方法,也就是有LogTarget.class注解的地方。

3、引入jar包注意事项

aspectjweaver-1.8.13.jar,低版本的jar可能会出问题

aopalliance-1.0.jar

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值