Spring框架AOP入门:基础与实践

0 下载量 6 浏览量 更新于2024-08-27 收藏 179KB PDF 举报
"这篇文章除了介绍Spring AOP的基本概念,还通过一个简单的跟踪和记录方面的示例,演示了如何在Spring框架中声明切入点和通知。文章特别强调,其目的不是全面介绍Spring框架,而是专注于AOP功能,特别是如何在实际应用中使用它们。Spring的AOP特性包括通知(Advice)、切入点(Pointcut)和顾问(Advisor),这些都是为了解决企业级应用的常见问题。" 在Spring Framework中,AOP(面向切面编程)是一种强大的工具,它允许开发者将关注点分离,如日志记录、事务管理或安全性,从核心业务逻辑中解耦出来。AOP的核心概念包括: 1. **通知(Advice)**:通知是在特定连接点(Join Point)执行的代码片段,它们可以是前置通知(Before Advice,在方法调用前执行)、后返回通知(After Returning Advice,在方法成功执行后执行)和异常通知(After Throwing Advice,在方法抛出异常后执行)。Spring允许您声明这些通知,并将其与特定的bean关联。 2. **切入点(Pointcut)**:切入点是程序中可以插入通知的具体点的定义,通常是方法调用。Spring支持基于注解的切入点表达式,使得可以在XML配置或Java配置中声明这些表达式,以精确控制何时应用通知。 3. **顾问(Advisor)**:顾问是切入点和通知的组合,它定义了在何处以及何时应用特定的通知。顾问允许您将通知与切入点逻辑捆绑在一起,形成一个完整的AOP切面。 文章提供了一个简单的例子,通过实现`IBusinessLogic`接口的`foo`方法,创建了一个业务逻辑bean。这个例子用于演示如何在Spring中应用AOP。例如,可以通过定义一个跟踪和记录的切面,使得在`foo`方法执行前后自动记录日志,而无需在`BusinessLogic`类中直接写入这些记录代码。这样,日志记录的关注点就被解耦出来了,使得业务逻辑更清晰,更易于维护。 在实际应用中,Spring AOP不仅可以用于日志记录,还可以用于事务管理、性能监控、安全性等多个领域。通过声明式的AOP配置,可以有效地减少代码重复,提高代码的可读性和可维护性。随着系列的深入,读者将了解到如何使用更多类型的通知(如环绕通知)和更复杂的切入点表达式,以实现更复杂、更实用的面向方面设计模式。

java.lang.NullPointerException: null at com.datech.web.controller.system.TbappController.remove(TbappController.java:667) at com.datech.web.controller.system.TbappController$$FastClassBySpringCGLIB$$ea1c3ba.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:55) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor$1.proceed(AopAllianceAnnotationsAuthorizingMethodInterceptor.java:82) at org.apache.shiro.authz.aop.AuthorizingMethodInterceptor.invoke(AuthorizingMethodInterceptor.java:39) at org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor.invoke(AopAllianceAnnotationsAuthorizingMethodInterceptor.java:115) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at com.datech.web.controller.system.TbappController$$EnhancerBySpringCGLIB$$b3b02033.remove(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2023-07-14 上传