AOP的使用

10 篇文章 0 订阅
package com.whc.noteserver.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.whc.noteserver.entity.NoteBook;
import com.whc.noteserver.param.NoteBookParam;
import com.whc.noteserver.service.NoteBookService;

@Aspect
@Component
public class NoteBookAOP {
	@Autowired
	private NoteBookService noteBookService;
	
	@Pointcut("execution (* com.whc.noteserver.service.NoteBookService.addNoteBook(..))")
	private void pointCut() {
		
	}
	
	@Before("pointCut()")
	public void before() {
		System.out.println("before add");
	}
	
	
//	  @AfterReturning("pointCut()")
//	  public void afterReturning() {
//		  System.out.println("afterreturning add"); 
//	  }
	 
	
	
	  @Around("pointCut()") 
	  public Object around(ProceedingJoinPoint proceedingJoinPoint) {
	  
		  Object obj=null;
		  System.out.println("around add before");
		  Object[] args=proceedingJoinPoint.getArgs();
		  NoteBookParam noteBookParam=new NoteBookParam();
		  noteBookParam.setUserid(((NoteBook)args[0]).getUserid());
		  int re=noteBookService.getCount(noteBookParam); 
		  if(re<5) { 
			  try {
				  obj=proceedingJoinPoint.proceed(); 
			  } catch (Throwable e) { // TODO Auto-generated catch block 
				  e.printStackTrace(); 
				  } 
			  }else{ 
				  System.out.println("around add after return -1"); 
				  return -1;
			}
			  
	  		System.out.println("around add after"); 
	  		
	  		return obj;
	  }
	 
	
	/*
	 * @AfterThrowing("pointCut()") public void afterThrowing(JoinPoint joinpoint,
	 * Throwable e) { System.out.println("error:"+e.getMessage());
	 * 
	 * }
	 */
	
	/*
	 * @After("pointCut()") public void after() { System.out.println("after"); }
	 */
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP 是一个用于实现面向切面编程的框架。它可以通过配置来实现横切关注点的模块化,并将其应用到程序的不同部分。Spring AOP 使用 AspectJ 切入点指示符来定义切入点表达式,用于匹配方法执行连接点。Spring AOP 支持以下 AspectJ 切入点指示符: 1. execution:用于匹配方法执行连接点。这是使用 Spring AOP 时要使用的主要切入点指示符。 2. within:限制匹配以连接某些类型中的点(使用 Spring AOP 时在匹配类型中声明的方法的执行)。 3. this:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中 Bean 引用(Spring AOP 代理)是给定类型的实例。 4. target:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中目标对象(正在代理的应用程序对象)是给定类型的实例。 5. args:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中参数是给定类型的实例。 6. @target:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中执行对象的类具有给定类型的注释。 7. @args:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中传递的实际参数的运行时类型具有给定类型的注释。 8. @within:限制匹配以连接具有给定注释的类型中的点(使用 Spring AOP 时在具有给定注释的类型中声明的方法的执行)。 9. @annotation:限制匹配到连接点的主题(在 Spring AOP 中运行的方法)具有给定注释的连接点。 在使用 Spring AOP 时,需要引入 Spring AOP 和 Spring Context 相关的包,并在配置文件中进行相应的配置。可以通过 Maven 或其他构建工具来引入相关依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值