本文主要是介绍强悍的Spring之AOP注解使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring中使用注解方式实现AOP,采用@AspectJ方式实现,首先确定需要切入的方法,也就是连接点
@Service
public class UserServiceMethod {public void add(String name) {System.out.println("UserServiceMethod add name is:" + name);}
}
开发切面
有了连接点,还需要切面通过切面描述AOP其他信息,来描述流程的织入
@Aspect
@Component
public class LogAgent {@Before("execution(* com.niu.dao.UserServiceMethod.add(..))")public void beforeAdd(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("before 方法规则拦截:" + method.getName());}@After("execution(* com.niu.dao.UserServiceMethod.add(..))")public void afterAdd(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("after 方法规则拦截:" + method.getName());}@AfterReturning("execution(* com.niu.dao.UserServiceMethod.add(..))")public void afterReturnAdd(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("afterReturn 方法规则拦截:" + method.getName());}@AfterThrowing("execution(* com.niu.dao.UserServiceMethod.add(..))")public void afterThrowsAdd(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("afterThrows 方法规则拦截:" + method.getName());}
}
其中注解中execution(* com.niu.dao.UserServiceMethod.add(…))是正则匹配,
这篇关于强悍的Spring之AOP注解使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!