String resource = “AspectJapplicationContext.xml”; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource); IAspectJService iAspectJService = (IAspectJService) applicationContext.getBean(“aspectJservice”); iAspectJService.doDestroy(); String doFirst = iAspectJService.doFirst(); System.out.println(doFirst);
AspectJapplicationContext.xml配置:
// 前置通知 public void before() { System.out.println(“AspectJ前置通知”); } // 前置通知,带参数(以下任何通知都能带参数,不一一列举) public void before(JoinPoint jp) { System.out.println(“AspectJ前置通知:JP=” + jp); } // 后置通知 public void afterReturning(Object ret) { System.out.println(“AspectJ后置通知:返回结果ret=” + ret); System.out.println("----------------------"); } // 环绕通知 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println(“AspectJ环绕通知:目标方法执行之前通知”); Object proceed = pjp.proceed(); System.out.println(“AspectJ环绕通知:目标方法执行之后通知”); if (proceed != null) { proceed = ((String) proceed).toUpperCase(); } return proceed; } // 异常通知 public void afterThrowing() { System.out.println(“AspectJ异常通知”); } // 异常通知:带参数 public void afterThrowing(Exception ex) { System.out.println(“AspectJ异常通知:ex=” + ex); } // 最终通知 public void after() { System.out.println(“AspectJ最终通知”); }