AOP的Around增强

布满荆棘的人生 2021-07-24 22:53 451阅读 0赞

一 配置

  1. <?xml version="1.0" encoding="GBK"?>
  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
  12. <!-- 指定自动搜索Bean组件、自动搜索切面类 -->
  13. <context:component-scan
  14. base-package="org.crazyit.app.service
  15. ,org.crazyit.app.aspect">
  16. <context:include-filter type="annotation"
  17. expression="org.aspectj.lang.annotation.Aspect" />
  18. </context:component-scan>
  19. <!-- 启动@AspectJ支持 -->
  20. <aop:aspectj-autoproxy />
  21. </beans>

二 切面类

  1. package org.crazyit.app.aspect;
  2. import org.aspectj.lang.annotation.*;
  3. import org.aspectj.lang.*;
  4. // 定义一个切面
  5. @Aspect
  6. public class TxAspect
  7. {
  8. // 匹配org.crazyit.app.service.impl包下所有类的、
  9. // 所有方法的执行作为切入点
  10. @Around("execution(* org.crazyit.app.service.impl.*.*(..))")
  11. public Object processTx(ProceedingJoinPoint jp)
  12. throws java.lang.Throwable
  13. {
  14. System.out.println("执行目标方法之前,模拟开始事务...");
  15. // 获取目标方法原始的调用参数
  16. Object[] args = jp.getArgs();
  17. if(args != null && args.length > 1)
  18. {
  19. // 修改目标方法的第一个参数
  20. args[0] = "【增加的前缀】" + args[0];
  21. }
  22. // 以改变后的参数去执行目标方法,并保存目标方法执行后的返回值
  23. Object rvt = jp.proceed(args);
  24. System.out.println("执行目标方法之后,模拟结束事务...");
  25. // 如果rvt的类型是Integer,将rvt改为它的平方
  26. if(rvt != null && rvt instanceof Integer)
  27. rvt = (Integer)rvt * (Integer)rvt;
  28. return rvt;
  29. }
  30. }

三 接口

Hello

  1. package org.crazyit.app.service;
  2. public interface Hello {
  3. // 定义一个简单方法,模拟应用中的业务逻辑方法
  4. void foo();
  5. // 定义一个addUser()方法,模拟应用中的添加用户的方法
  6. int addUser(String name, String pass);
  7. }

World

  1. package org.crazyit.app.service;
  2. public interface World {
  3. // 定义一个简单方法,模拟应用中的业务逻辑方法
  4. public void bar();
  5. }

四 实现类

HelloImpl

  1. package org.crazyit.app.service.impl;
  2. import org.springframework.stereotype.Component;
  3. import org.crazyit.app.service.*;
  4. @Component("hello")
  5. public class HelloImpl implements Hello {
  6. // 定义一个简单方法,模拟应用中的业务逻辑方法
  7. public void foo() {
  8. System.out.println("执行Hello组件的foo()方法");
  9. }
  10. // 定义一个addUser()方法,模拟应用中的添加用户的方法
  11. public int addUser(String name, String pass) {
  12. System.out.println("执行Hello组件的addUser添加用户:" + name);
  13. return 20;
  14. }
  15. }

WorldImpl

  1. package org.crazyit.app.service.impl;
  2. import org.springframework.stereotype.Component;
  3. import org.crazyit.app.service.*;
  4. @Component("world")
  5. public class WorldImpl implements World {
  6. // 定义一个简单方法,模拟应用中的业务逻辑方法
  7. public void bar() {
  8. System.out.println("执行World组件的bar()方法");
  9. }
  10. }

五 测试类

  1. package lee;
  2. import org.springframework.context.*;
  3. import org.springframework.context.support.*;
  4. import org.crazyit.app.service.*;
  5. public class BeanTest {
  6. public static void main(String[] args) {
  7. // 创建Spring容器
  8. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  9. Hello hello = ctx.getBean("hello", Hello.class);
  10. hello.foo();
  11. hello.addUser("孙悟空", "7788");
  12. World world = ctx.getBean("world", World.class);
  13. world.bar();
  14. }
  15. }

六 测试结果

  1. 执行目标方法之前,模拟开始事务...
  2. 执行Hello组件的foo()方法
  3. 执行目标方法之后,模拟结束事务...
  4. 执行目标方法之前,模拟开始事务...
  5. 执行Hello组件的addUser添加用户:【增加的前缀】孙悟空
  6. 执行目标方法之后,模拟结束事务...
  7. addUser()的返回值为:400
  8. 执行目标方法之前,模拟开始事务...
  9. 执行World组件的bar()方法
  10. 执行目标方法之后,模拟结束事务...

发表评论

表情:
评论列表 (有 0 条评论,451人围观)

还没有评论,来说两句吧...

相关阅读