多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
面向切面的编程。 类似事务管理。 AOP是Spring的一个关键组件,但是IoC容器不依赖于AOP,也就是AOP可以选择使用,Spring提供了一个非常好的中间件解决方案。 Spring AOP主要用于: -提供声明式企业服务, 特别是作为EJB声明式服务的替代 -允许用户实现自己的客制化面, 用AOP补充OOP的使用。 5.1.1 AOP相关概念 连接点(Joinpoint)、切点(Pointcut)、增强(Advice)、引介(Introduction)、织入(Weaving)、切面(Aspect) -Aspect, 切面 @Aspect -Join point,在Spring AOP中, 一般是方法的执行 -Advice:特殊的join point."around", "before" and "after" advice -Pointcut, 匹配切入点的谓词。 -Introduction -Target object. -AOP proxy. JDK动态代理或者CGLIB代理 -Weaving:将切面与其他应用程序类型或对象链接以创建建议对象。 5.1.2 Spring AOP的作用和目标 5.1.3 AOP 代理 5.2 @AspectJ 支持 5.2.1 开启@AspectJ支持 Java配置 @Configuration @EnableAspectJAutoProxy public class AppConfig { } } 使用xml 配置方式 <aop:aspectj-autoproxy/> 5.2.2 声明一个切面 <bean id="myAspect" class="org.xyz.NotVeryUsefulAspect"> <!-- configure properties of aspect here as normal --> </bean> 类声明方式 package org.xyz; import org.aspectj.lang.annotation.Aspect; @Aspect public class NotVeryUsefulAspect { } } 5.2.3 声明一个切入点 @Pointcut("execution(* transfer(..))")// the pointcut expression private void anyOldTransfer() {}// the pointcut signature 在执行transfer方法是执行anyOldTransfer 5.2.4 声明advice 5.2.5 引介 - Introductions 将增强应用在指定的接口上。