Spring中的AOP注解开发

2019-01-28 16:41:14

最终通知使用注解@After的时候报错 其他几个注解都没事

Junit测试的错误信息

  1. # A fatal error has been detected by the Java Runtime Environment:
  2. #
  3. # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000051f443b5, pid=3656, tid=8380
  4. #
  5. # JRE version: Java(TM) SE Runtime Environment (7.0_51-b13) (build 1.7.0_51-b13)
  6. # Java VM: Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode windows-amd64 compressed oops)
  7. # Problematic frame:
  8. # V [jvm.dll+0x943b5]
  9. #
  10. # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
  11. #
  12. # An error report file with more information is saved as:
  13. # D:\MyEclipse8.x_Workspaces\spring_day03_aop\hs_err_pid3656.log
  14. # [ timer expired, abort... ]

applicationContext.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!-- 在配置文件中开启注解的AOP开发 -->
  16. <aop:aspectj-autoproxy/>
  17. <bean id="orderDao" class="spring.demo1.OrderDao"></bean>
  18. <bean id="myAspect" class="spring.demo1.MyAspectAnno"></bean>
  19. </beans>

实体类 OrderDao.java

  1. package spring.demo1;
  2. public class OrderDao {
  3. public void save(){
  4. System.out.println("保存订单");
  5. };
  6. public void update(){
  7. System.out.println("修改订单");
  8. };
  9. public String delete(){
  10. System.out.println("删除订单");
  11. return "haha";
  12. };
  13. public void find(){
  14. System.out.println("查询订单");
  15. //int i = 1/0;
  16. };
  17. }

切面类 MyAspectAnno.java

  1. package spring.demo1;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.AfterReturning;
  5. import org.aspectj.lang.annotation.AfterThrowing;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Before;
  9. /**
  10. * 切面类的注解
  11. * @author hello
  12. *
  13. */
  14. @Aspect
  15. public class MyAspectAnno {
  16. @Before(value="execution(* spring.demo1.OrderDao.save(..))")
  17. public void before(){
  18. System.out.println("=====前置增强=====");
  19. }
  20. //后置通知
  21. @AfterReturning(value="execution(* spring.demo1.OrderDao.delete(..))",returning="result")
  22. public void afterReturning(Object result){
  23. System.out.println("=====后置增强=====" + result);
  24. }
  25. //环绕通知
  26. @Around(value="execution(* spring.demo1.OrderDao.update(..))")
  27. public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
  28. System.out.println("=====环绕前增强=====");
  29. Object obj = joinPoint.proceed();
  30. System.out.println("=====环绕后增强=====");
  31. return obj;
  32. }
  33. //异常抛出通知
  34. @AfterThrowing(value="execution(* spring.demo1.OrderDao.find(..))",throwing="e")
  35. public void afterThrowing(Throwable e){
  36. System.out.println("=====异常抛出增强=====" + e.getMessage());
  37. }
  38. //最终通知
  39. @After(value="execution(* spring.demo1.OrderDao.find(..))")
  40. public void after(){
  41. System.out.println("===最终通知===");
  42. }
  43. }

测试类 SpringDemo1.java

  1. package spring.demo1;
  2. import javax.annotation.Resource;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @ContextConfiguration("classpath:applicationContext.xml")
  9. public class SpringDemo1 {
  10. @Resource(name = "orderDao")
  11. private OrderDao orderDao;
  12. @Test
  13. public void demo1(){
  14. orderDao.find();
  15. orderDao.update();
  16. orderDao.save();
  17. orderDao.delete();
  18. }
  19. }

0
1
0

添加评论

正在回复:
取消
5
0
1
0