用Spring配置文件或者注解方式实现AOP

配置文件bean.xml:

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans" 
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5. xmlns:context="http://www.springframework.org/schema/context" 
  6. xmlns:aop="http://www.springframework.org/schema/aop" 
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9. http://www.springframework.org/schema/context  
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11. http://www.springframework.org/schema/aop  
  12. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13. ">  
  14. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  15. <bean id="myIterceptor" 
  16. class="com.huqianhao.service.MyIterceptor">  
  17. </bean>  
  18. <bean id="personService" 
  19. class="com.huqianhao.service.impl.PersonServiceBean">  
  20. </bean>  
  21.  
  22. <aop:config>  
  23. <aop:aspect id="aop" ref="myIterceptor">  
  24. <aop:pointcut  
  25. expression="execution(* com.huqianhao.service.impl.PersonServiceBean.*(..))" 
  26. id="mycut" />  
  27. <aop:before method="doAccessCheck" pointcut-ref="mycut" arg-names="name"/>   
  28. <aop:after-returning method="doAfterReturning" pointcut-ref="mycut"/>  
  29. <aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut"/>  
  30. <aop:after method="doAfter" pointcut-ref="mycut"/>  
  31. <aop:around method="doAround" pointcut-ref="mycut"/>  
  32. </aop:aspect>  
  33.  
  34. </aop:config>  
  35. </beans>  

MyIterceptor.java

  
  
  
  
  1. package com.huqianhao.service;  
  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 MyIterceptor {  
  14.  
  15. // @Pointcut("execution (* com.huqianhao.service.impl.PersonServiceBean.*(..))")  
  16. private void anyMethod() {  
  17. }  
  18.  
  19. // @Before("anyMethod()&& args(name)")  
  20. public void doAccessCheck() {  
  21. System.out.println("前置通知:");  
  22. }  
  23.  
  24. // @AfterReturning(pointcut = "anyMethod()", returning = "result")  
  25. public void doAfterReturning() {  
  26. System.out.println("后置通知:");  
  27. }  
  28.  
  29. // @AfterThrowing(pointcut = "anyMethod()", throwing = "e")  
  30. public void doAfterThrowing() {  
  31. System.out.println("例外通知:");  
  32. }  
  33.  
  34. // @After("anyMethod()")  
  35. public void doAfter() {  
  36. System.out.println("最终通知");  
  37. }  
  38.  
  39. // @Around("anyMethod()")  
  40. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  41. System.out.println("进入方法");  
  42. Object result = pjp.proceed();  
  43. System.out.println("退出方法");  
  44. return result;  
  45. }  
  46.  
  47. }  
  48.  
  49. package com.huqianhao.service.impl;  
  50.  
  51. import com.huqianhao.service.PersonService;  
  52.  
  53. public class PersonServiceBean implements PersonService {  
  54.  
  55. public void save(String name) {  
  56. System.out.println("我是save方法");  
  57. // throw new RuntimeException("出��常啦");  
  58. }  
  59.  
  60. public String getPersonName(Integer id) {  
  61. System.out.println("我是getPersonName方法");  
  62. return "XXX";  
  63. }  
  64.  
  65. public void update(String nameInteger id) {  
  66. System.out.println("我是update方法");  
  67.  
  68. }  

测试类

  
  
  
  
  1. package junit.test;  
  2. import org.junit.Test;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import com.huqianhao.service.PersonService;  
  6. public class SpringAOPTest {  
  7. @Test  
  8. public void instanceSpring() {  
  9. ApplicationContext context = new ClassPathXmlApplicationContext(  
  10. "beans.xml");  
  11. PersonService personService = (PersonService) context.getBean("personService");  
  12. personService.save("zhangsan");  
  13. }  

 

你可能感兴趣的:(注解,spring,AOP,配置,文件)