切面定义好之后我们必须交给Spring管理,配置我们的bean.xml文件如下: Xml代码
1.
2. 5. xmlns:aop=\ 6. xsi:schemaLocation=\a/beans 7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd\10. 11. 12.
编写测试类如下: Java代码
1. package com.royzhou.aop; 2.
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.context.support.ClassPathXmlApplicationContext; 5.
6. public class TestAOP {
7. public static void main(String[] args) {
8. ApplicationContext ctx = new ClassPathXmlApplicationContext(\
9. UserBean ub = (UserBean)ctx.getBean(\10. ub.addUser(\
11. } 12.}
运行测试类输出: 前置通知 进入方法
this is addUser() method! 后置通知 最终通知 退出方法
可以看出定义的各个通知的执行顺序,
例外通知只有在程序异常的情况下才会发生. 其他通知都会执行.
我们也可以在环绕通知里面将前面的几个通知实现了.
如果需要获取方法的参数我们必须在定义通知的时候做响应的设置: 比如我在前置通知希望获取到输入的参数需要修改MyInterceptor如下: Java代码
1. package com.royzhou.aop; 2.
3. import org.aspectj.lang.ProceedingJoinPoint; 4. import org.aspectj.lang.annotation.After;
5. import org.aspectj.lang.annotation.AfterReturning; 6. import org.aspectj.lang.annotation.AfterThrowing; 7. import org.aspectj.lang.annotation.Around; 8. import org.aspectj.lang.annotation.Aspect; 9. import org.aspectj.lang.annotation.Before; 10.import org.aspectj.lang.annotation.Pointcut; 11.
12.@Aspect
13.public class MyInterceptor { 14. /**
15. * 定义切入点
16. * 第一个*表示方法的返回值,这里使用通配符,只有返回值符合条件的才拦截
17. * 第一个..表示com.royzhou.aop包及其子包 18. * 倒数第二个*表示包下的所有Java类 19. * 最后一个*表示类的所有方法
20. * (..)表示方法的参数可以任意多个 21. */
22. @Pointcut(\定义一个切入点,名称为pointCutMethod(),拦截类的所有方法 23. private void pointCutMethod() { 24. } 25.
26. //需要两个条件同时成立. args(userName)代表只有一个参数且为String类型 名称必须与doBefore方法的参数名称一样
27. @Before(\定义前置通知
28. public void doBefore(String userName) {
29. System.out.println(\前置通知\30. } 31.
32. @AfterReturning(\定义后置通知 33. public void doAfterReturning() { 34. System.out.println(\后置通知\35. } 36.
37. @AfterThrowing(\定义例外通知 38. public void doAfterException() { 39. System.out.println(\异常通知\40. } 41.
42. @After(\定义最终通知 43. public void doAfter() {
44. System.out.println(\最终通知\45. } 46.
47. @Around(\定义环绕通知
48. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
49. System.out.println(\进入方法\
50. Object object = pjp.proceed(); //必须执行pjp.proceed()方法,如果不执行此方法,业务bean的方法以及后续通知都不执行 51. System.out.println(\退出方法\52. return object; 53. } 54.}
重新运行测试类输出:
前置通知royzhou
进入方法
this is addUser() method! 后置通知 最终通知 退出方法
可见我们成功的获取到了方法的参数
如果需要获取方法的返回值,则修改如下: Java代码
1. package com.royzhou.aop; 2.
3. import org.aspectj.lang.ProceedingJoinPoint; 4. import org.aspectj.lang.annotation.After;
5. import org.aspectj.lang.annotation.AfterReturning; 6. import org.aspectj.lang.annotation.AfterThrowing; 7. import org.aspectj.lang.annotation.Around; 8. import org.aspectj.lang.annotation.Aspect; 9. import org.aspectj.lang.annotation.Before; 10.import org.aspectj.lang.annotation.Pointcut; 11.
12.@Aspect
13.public class MyInterceptor { 14. /**
15. * 定义切入点
16. * 第一个*表示方法的返回值,这里使用通配符,只有返回值符合条件的才拦截
17. * 第一个..表示com.royzhou.aop包及其子包 18. * 倒数第二个*表示包下的所有Java类 19. * 最后一个*表示类的所有方法
20. * (..)表示方法的参数可以任意多个 21. */
22. @Pointcut(\定义一个切入点,名称为pointCutMethod(),拦截类的所有方法 23. private void pointCutMethod() { 24. } 25.
26. //需要两个条件同时成立. args(userName)代表只有一个参数且为String类型 名称必须与doBefore方法的参数名称一样
27. @Before(\定义前置通知
28. public void doBefore(String userName) {

