标签:aspect 处理程序 system struts2 method 完成后 sign round before
<aop:aspectj-autoproxy />
@Component @Aspect public class Operator { @Pointcut("execution(* com.lynn.learning.spring.service..*.*(..))") public void pointCut(){} @Before("pointCut()") public void doBefore(JoinPoint joinPoint){ System.out.println("AOP Before Advice..."); } @After("pointCut()") public void doAfter(JoinPoint joinPoint){ System.out.println("AOP After Advice..."); } @AfterReturning(pointcut="pointCut()", returning="returnVal") public void afterReturn(JoinPoint joinPoint, Object returnVal){ System.out.println("AOP AfterReturning Advice:" + returnVal); } @AfterThrowing(pointcut="pointCut()",throwing="error") public void afterThrowing(JoinPoint joinPoint, Throwable error){ System.out.println("AOP AfterThrowing Advice:" + error); } @Around("pointCut()") public Object around(ProceedingJoinPoint pjp){ Object obj = null; System.out.println("AOP Around before..."); try { obj = pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("AOP Around after..."); return obj; } }
@Service public class UserService { public void add(){ System.out.println("UserService add()"); } public String delete(){ System.out.println("UserService delete()"); return "delete函数返回值"; } public void edit(){ System.out.println("UserService edit()"); int i = 5/0; } }
<context:component-scan base-package="com.lynn.learning.spring"/>
<aop:aspectj-autoproxy />
public class UserServiceTest { @Test public void add() { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.add(); } @Test public void delete() { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.delete(); } @Test public void edit() { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.edit(); } }
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface MyLog { String requestUrl(); }
@Component @Aspect public class MyLogAspect { @Pointcut(value = "@annotation(com.lynn.learning.spring.annotation.MyLog)") public void pointcut() {} @Around(value = "pointcut() && @annotation(myLog)") public Object around(ProceedingJoinPoint point, MyLog myLog) { System.out.println("+++++执行了around方法+++++"); String requestUrl = myLog.requestUrl(); Class clazz = point.getTarget().getClass(); Method method = ((MethodSignature) point.getSignature()).getMethod(); System.out.println("执行了类:" + clazz + ", 方法:" + method + " 自定义请求地址:" + requestUrl); try { return point.proceed(); } catch (Throwable throwable) { return throwable.getMessage(); } } @AfterReturning(value = "pointcut() && @annotation(myLog)", returning = "result") public Object afterReturning(JoinPoint joinPoint, MyLog myLog, Object result) { System.out.println("+++++执行了afterReturning方法+++++"); System.out.println("执行结果:" + result); return result; } @AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex") public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) { System.out.println("+++++执行了afterThrowing方法+++++"); System.out.println("请求:" + myLog.requestUrl() + " 出现异常"); } }
@Service public class UserService { @MyLog(requestUrl = "add") public void add(){ System.out.println("UserService add()"); } public String delete(){ System.out.println("UserService delete()"); return "delete函数返回值"; } public void edit(){ System.out.println("UserService edit()"); int i = 5/0; } }
public class UserServiceTest { @Test public void add() { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.add(); } }
+++++执行了around方法+++++ 执行了类:class com.lynn.learning.spring.service.UserService, 方法:public void com.lynn.learning.spring.service.UserService.add() 自定义请求地址:add AOP Around before... AOP Before Advice... UserService add() AOP Around after... AOP After Advice... AOP AfterReturning Advice:null +++++执行了afterReturning方法+++++ 执行结果:null
标签:aspect 处理程序 system struts2 method 完成后 sign round before
原文地址:https://www.cnblogs.com/lynn16/p/10687106.html