深入理解Spring AOP:面向切面编程详解

版权申诉
0 下载量 125 浏览量 更新于2024-07-21 收藏 952KB PDF 举报
"Springframework核心技术AOP详细介绍文档.pdf" 在深入探讨Spring Framework的AOP(面向方面编程)技术之前,我们首先要理解AOP的基本概念。AOP是一种编程范式,它旨在提供一种更清晰的方式来组织代码,特别是那些横切多个类和对象的关注点,如日志、事务管理和安全性。Spring框架的AOP模块允许开发者定义这些关注点作为独立的模块,称为“方面”,并将它们插入到应用程序的适当位置,而无需侵入业务逻辑。 1. **AOP的核心概念** - **连接点(Joinpoint)**:连接点是程序执行的特定时刻,比如方法的调用或异常的处理。在Spring中,连接点通常是方法的执行。 - **切入点(Pointcut)**:切入点定义了哪些连接点将会应用增强(Advice)。它可以基于方法名、注解或其他条件来选择。 - **通知/增强(Advice)**:通知是实际的行为,当切入点匹配时,通知会在特定的连接点被执行。它可以是前置通知(在方法执行前)、后置通知(在方法执行后)、返回通知(在方法正常返回后)、异常通知(在方法抛出异常后)以及最终通知(无论方法是否正常结束都会执行)。 - **目标对象(Target)**:目标对象是指被AOP代理的对象,即不含增强逻辑的原始业务对象。 - **代理(Proxy)**:代理对象是AOP框架创建的,用于包含目标对象并实现对其的增强。代理可以是JDK动态代理或CGLIB代理,取决于目标对象是否实现了接口。 - **织入(Weaving)**:织入是将通知应用到目标对象以创建代理的过程。这可以在编译时、类加载时或运行时完成。 2. **AOP的应用场景** AOP在Spring中的应用广泛,如: - **事务管理**:Spring的声明式事务管理通过AOP实现,无需在业务代码中插入事务管理代码,就能确保事务的一致性。 - **日志记录**:记录方法的执行时间和性能,或在异常时记录错误信息。 - **安全控制**:如权限验证,可以在不修改业务代码的情况下实现权限拦截。 - **缓存管理**:在方法调用前后添加缓存逻辑,提高性能。 3. **Spring AOP的实现方式** Spring支持两种AOP实现:基于代理的AOP和基于注解的AOP。基于代理的AOP包括JDK动态代理(针对接口)和CGLIB代理(针对没有接口的类)。基于注解的AOP则允许开发者直接在方法上使用如`@Transactional`这样的注解来声明增强行为。 4. **配置AOP** 在Spring配置文件中,可以通过<aop:config>标签定义切入点表达式,<aop:advisor>标签定义通知,或直接在bean上使用Spring的注解如`@Aspect`来声明一个方面。 5. **自定义切面** 开发者可以定义自己的切面类,该类包含切入点表达式和通知方法。切面类可以使用`@Before`, `@After`, `@Around`, `@AfterReturning`和`@AfterThrowing`等注解来定义通知。 6. **AOP的优势** - **代码复用**:将通用功能如日志、事务等抽象成独立的方面,避免重复代码。 - **降低耦合**:通过AOP,可以将系统的核心业务逻辑与辅助功能分离,降低不同模块之间的依赖。 - **可扩展性**:新的关注点可以很容易地加入到系统中,无需修改现有的业务代码。 Spring Framework的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 上传
2023-06-03 上传

com.cxm.common.exception.CxmServiceException: 获取日结单数据出错 at com.cxm.service.report.DailyStatManager.reconciliationIndex(DailyStatManager.java:203) at com.cxm.api.report.DailyStatApi.reconciliationIndex(DailyStatApi.java:102) at com.cxm.api.report.DailyStatApi$$FastClassBySpringCGLIB$$4bcc5350.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) at com.cxm.log.CxmLogbackAspect.doAround(CxmLogbackAspect.java:98) at sun.reflect.GeneratedMethodAccessor365.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)

2023-06-01 上传

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.redisson.spring.data.connection.RedissonConnection at org.redisson.spring.data.connection.RedissonConnectionFactory.getConnection(RedissonConnectionFactory.java:113) at org.springframework.data.redis.core.RedisConnectionUtils.fetchConnection(RedisConnectionUtils.java:193) at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:144) at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:105) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191) at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:97) at org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:55) at com.million.mes.basic.commonredis.server.RedisService.getCacheMapValue(RedisService.java:231) at com.million.mes.basic.dbapi.service.DataSourceService.detail(DataSourceService.java:96) at com.million.mes.basic.dbapi.service.DataSourceService$$FastClassBySpringCGLIB$$f8f33523.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)

2023-06-12 上传