spring框架AOP配置

左手的ㄟ右手 2022-05-14 16:09 363阅读 0赞

第一步:导入包
aopalliance-1.0.jar
aspectjweaver-1.8.13.jar
spring-aop-3.2.13.RELEASE.jar

spring开头的包自带的有
aopalliance,aspectjweaver去https://repo.spring.io/下载
还加上ioc的5个包:

commons-logging-1.2.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar

第二步:配置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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd">
  12. <bean id="usLogger" class="com.big3.aop.UserServiceLogger"/>
  13. <!-- 日志增强注入 -->
  14. <aop:config>
  15. <!--定义切入点-->
  16. <aop:pointcut id="pointcut" expression="execution(public void eat(com.big3.pojo.User))"/>
  17. <!--织入增强处理,ref:增强处理对象,注入的日志类-->
  18. <aop:aspect ref="usLogger">
  19. <!--前置增强处理,method:被织入的方法,pointcut-ref:切入点-->
  20. <aop:before method="before" pointcut-ref="pointcut"/>
  21. <!--后置增强处理,method:被织入的方法,pointcut-ref:切入点,returning:指定返回值的属性名为result-->
  22. <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
  23. </aop:aspect>
  24. </aop:config>
  25. <bean id="errorLogger" class="com.big3.aop.ErrorLogger"/>
  26. <!-- 异常增强注入 -->
  27. <aop:config>
  28. <!-- 定义切入点 -->
  29. <aop:pointcut id="point" expression="execution(public void save(com.big3.pojo.User))"/>
  30. <!-- 织入增强处理,ref:增强处理对象,注入的异常类 -->
  31. <aop:aspect ref="errorLogger">
  32. <!--after-throwing:异常增强, method:被织入的方法,pointcut-ref:切入点,throwing:为e参数注入异常实例 -->
  33. <aop:after-throwing method="afterThrowing" pointcut-ref="point" throwing="e"/>
  34. </aop:aspect>
  35. </aop:config>
  36. <bean id="afterLogger" class="com.big3.aop.AfterLogger" />
  37. <!-- 最终增强注入 -->
  38. <aop:config>
  39. <!-- 定义切入点 -->
  40. <aop:pointcut id="points" expression="execution(public void eat(int ,String))"/>
  41. <!-- 织入增强处理,ref:增强处理对象,注入的异常类-->
  42. <aop:aspect ref="afterLogger">
  43. <!--after:最终增强, method:被织入的方法,pointcut-ref:切入点 ;-->
  44. <aop:after method="after" pointcut-ref="points"/>
  45. </aop:aspect>
  46. </aop:config>
  47. <bean id="aroundLogger" class="com.big3.aop.AroundLogger"/>
  48. <!-- 环绕增强 -->
  49. <aop:config>
  50. <!-- 定义切入点 -->
  51. <aop:pointcut id="pointse" expression="execution(public int saves(com.big3.pojo.User))"/>
  52. <aop:aspect ref="aroundLogger">
  53. <aop:around method="aroundLogger" pointcut-ref="pointse"/>
  54. </aop:aspect>
  55. </aop:config>
  56. </beans>

如果用注解配置文件如下:

  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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd">
  12. <!--扫描包中注解标注的类-->
  13. <context:component-scan base-package="com.big3.dao,com.big3.pojo,com.big3.aop"/>
  14. <!--注册Aspectj代理对象-->
  15. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  16. </beans>

注意:如果用了spring mvc框架,IOC,AOP需要配置在springmvc配置文件里

  1. <!--指定扫描注解定义的包,因为DAO层是自动实现,所以不用注解不用新建bean了,只需对service和pojo层aop层注解,实现loc新建bean的处理-->
  2. <context:component-scan base-package="cn.kgc.service,cn.kgc.pojo,cn.kgc.service.impl,cn.kgc.controller,cn.kgc.aop"/>
  3. <!--注册Aspectj代理对象,AOP切面增强用,注解方式-->
  4. <aop:aspectj-autoproxy/>

第三步:JAVA应用,如果不用注解,就不用注解标示

日志增强

  1. @Aspect
  2. @Component("usLogger")
  3. public class UserServiceLogger {
  4. private static Logger log=Logger.getLogger(UserServiceLogger.class);
  5. //定义切入点方法
  6. @Pointcut("execution(public void eat(com.big3.pojo.User))")
  7. public void pointcut(){}
  8. //前置增强
  9. @Before("pointcut()")
  10. public void before(JoinPoint jp){
  11. log.info("调用"+jp.getTarget()+"的"+jp.getSignature()+"方法,方法参数:\"+Arrays.toString(jp.getArgs()));\n" +
  12. "\t}"+Arrays.toString(jp.getArgs()));
  13. }
  14. //后置增强
  15. @AfterReturning(pointcut="pointcut()",returning="result")
  16. public void afterReturning(JoinPoint jp,Object result){
  17. log.info("调用"+jp.getTarget()+"的"+jp.getSignature()+"方法,方法返回值:"+result);
  18. }

异常增强

  1. @Aspect //增强处理
  2. @Component("errorLogger")
  3. public class ErrorLogger {
  4. private static Logger log=Logger.getLogger(UserServiceLogger.class);
  5. //定义切入点方法
  6. @Pointcut("execution(public void save(com.big3.pojo.User))")
  7. public void pointcut(){}
  8. //定义异常增强方法
  9. @AfterThrowing(pointcut="pointcut()",throwing="e")
  10. public void afterThrowing(JoinPoint jp,RuntimeException e){
  11. log.error(jp.getSignature().getName()+"方法发生异常,"+e);
  12. }
  13. }

最终增强

  1. //最终增强,不管是否发生异常都执行,用于释放资源
  2. @Aspect //增强处理
  3. @Component("afterLogger")
  4. public class AfterLogger {
  5. private static Logger log=Logger.getLogger(UserServiceLogger.class);
  6. //定义切入点方法
  7. @Pointcut("execution(public void eat(int ,String))")
  8. public void pointcut(){}
  9. //定义最终增强方法
  10. @After("pointcut()")
  11. public void after(JoinPoint jp){
  12. log.info(jp.getSignature().getName()+"方法结束了");
  13. }
  14. }

环绕增

  1. //环绕增强
  2. @Aspect //增强处理
  3. @Component("aroundLogger")
  4. public class AroundLogger {
  5. private static final Logger log=Logger.getLogger(AroundLogger.class);
  6. //定义环绕增强方法
  7. @Around("execution(public int saves(com.big3.pojo.User))")
  8. public Object aroundLogger(ProceedingJoinPoint jp)throws Throwable{
  9. User user=(User)jp.getArgs()[0];
  10. log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法入参"+user.getName());
  11. try {
  12. int result=(int)jp.proceed();//执行目标方法并获得返回值
  13. log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法返回值"+(result+200));
  14. return result;
  15. } catch (Throwable throwable) {
  16. log.error(jp.getSignature().getName()+"方法发生异常"+throwable);
  17. throw throwable;
  18. }finally {
  19. log.info(jp.getSignature().getName()+"方法结束执行.");
  20. }
  21. }
  22. }

发表评论

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

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

相关阅读

    相关 Spring 框架AOP技术

    、AOP技术概念         面向切面编程\[底层就是动态代理\]指程序在运行期间动态的将某段代码切入到指定方法位置进行运行的编程方式。AOP是OOP的延续,是软件...

    相关 Spring框架 AOP

    AOP(Aspect Oriented Programming) 原理 正常程序执行顺序都为纵向执行流程 ,在某一个步骤的前后,做一个前置通知 和后置通知,这个