实在是秒啊,我还从来没见过把Spring之AOP讲的这么通俗易懂的,安排!
Spring之AOP
什么是AOP?
AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
目的:
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
作用及其优势
作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强
优势: 减少重复代码,提高开发效率,并且便于维护
AOP 的底层实现
AOP 的底层是通过 Spring 提供的的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。
AOP 的动态代理技术
JDK 代理 : 基于接口的动态代理技术
cglib 代理:基于父类的动态代理技术
JDK 的动态代理
①目标类接口
1 2 3 | public interface TargetInterface { public void method(); } |
②目标类
1 2 3 4 5 6 | public class Target implements TargetInterface { @Override public void method() { System.out.println( "Target running...." ); } } |
③动态代理代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Target target = new Target(); //创建目标对象 //创建代理对象 TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(target.getClass() .getClassLoader(),target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println( "前置增强代码..." ); Object invoke = method.invoke(target, args); System.out.println( "后置增强代码..." ); return invoke; } } ); |
④ 调用代理对象的方法测试
1 2 | // 测试,当调用接口的任何方法时,代理对象的代码都无需修改 proxy.method(); |
cglib 的动态代理
①目标类
1 2 3 4 5 | public class Target { public void method() { System.out.println( "Target running...." ); } } |
②动态代理代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Target target = new Target(); //创建目标对象 Enhancer enhancer = new Enhancer(); //创建增强器 enhancer.setSuperclass(Target. class ); //设置父类 enhancer.setCallback( new MethodInterceptor() { //设置回调 @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println( "前置代码增强...." ); Object invoke = method.invoke(target, objects); System.out.println( "后置代码增强...." ); return invoke; } }); Target proxy = (Target) enhancer.create(); //创建代理对象 |
③调用代理对象的方法测试
1 2 | //测试,当调用接口的任何方法时,代理对象的代码都无需修改 proxy.method(); |
相关知识要点
aop:面向切面编程
aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理
aop的重点概念:
1 2 3 4 5 6 7 | Pointcut(切入点):被增强的方法 Advice(通知/ 增强):封装增强业务逻辑的方法 Aspect(切面):切点+通知 Weaving(织入):将切点与通知结合的过程 |
开发明确事项:
1 2 3 4 5 | 谁是切点(切点表达式配置) 谁是通知(切面类中的增强方法) 将切点和通知进行织入配置 |
基于XML配置文件的AOP开发
①导入 AOP 相关坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!--导入spring的context坐标,context依赖aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version> 5.2 . 5 .RELEASE</version> </dependency> <!--spring测试的依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version> 5.2 . 5 .RELEASE</version> </dependency> <!-- aspectj的织入 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version> 1.9 . 2 </version> </dependency> |
②创建目标接口和目标类(内部有切点)
1 2 3 4 5 6 7 8 9 10 11 12 13 | public interface TestService { public void save(); } public class TestServiceImpl implements TestService { public void save() { System.out.println( "save方法runnning......" ); } } |
③创建切面类(内部有增强方法)
1 2 3 4 5 6 7 8 9 | public class Advice { //前置增强的通知 public void before(){ System.out.println( "前置增强......" ); } } |
④将目标类和切面类的对象创建权交给 spring
1 2 3 4 5 | <!--配置目标类--> <bean id= "testService" class = "com.duanping.service.impl.TestServiceImpl" ></bean> <!--配置切面类--> <bean id= "advice" class = "com.duanping.advices.Advice" ></bean> |
⑤在 applicationContext.xml 中配置织入关系
导入aop命名空间
1 2 3 4 5 6 7 8 9 10 11 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> |
配置切点表达式和前置增强的织入关系
1 2 3 4 5 6 7 8 9 | <!--配置织入: 告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置....) --> <aop:config> <!-- 声明切面 引入advice的bean作为切面对象--> <aop:aspect ref= "advice" > <!--切面: 通知+切点 配置对切点的方法进行前置增强--> <aop:before method= "before" pointcut= "execution(public void com.duanping.service.impl.TestServiceImpl.save())" ></aop:before> </aop:aspect> </aop:config> |
⑥测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 | @RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration ( "classpath:applicationContext.xml" ) public class AopTest { @Autowired private TestService testService; @Test public void test1(){ testService.save(); } } |
⑦测试结果
![](http://upload-images.jianshu.io/upload_images/23140115-1fbd9e5ebe80930f.png)
XML 配置 AOP 详解
1) 切点表达式的写法
表达式语法:
1 | execution([修饰符] 返回值类型 包名.类名.方法名(参数)) |
访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号* 代表任意
包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表
例如:
1 2 3 4 5 | execution( public void com.itheima.aop.Target.method()) execution( void com.itheima.aop.Target.*(..)) execution(* com.itheima.aop.*.*(..)) execution(* com.itheima.aop..*.*(..)) execution(* *..*.*(..)) |
2) 通知的类型
通知的配置语法:
1 | <aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型> |
![](http://upload-images.jianshu.io/upload_images/23140115-9a239138fc97f7e2.png)
3) 切点表达式的抽取
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。
1 2 3 4 5 | <!--对切点的抽取--> <aop:pointcut id= "point" expression= "execution(* com.duanping.service.impl.*.*(..))" /> <aop:before method= "before" pointcut-ref= "point" ></aop:before> |
基于注解的 AOP 开发
在配置文件中开启组件扫描和 AOP 的自动代理
1 2 3 4 5 | <!--组件扫描--> <context:component-scan base- package = "com.itheima.aop" /> <!--aop的自动代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> |
注解配置 AOP 详解
1) 注解通知的类型
通知的配置语法:@通知注解(“切点表达式")
2) 切点表达式的抽取
![](http://upload-images.jianshu.io/upload_images/23140115-18eba81d1f6c52e7.png)
同 xml配置
aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用
1 2 3 4 5 6 7 8 9 10 | @Pointcut ( "execution(* com.duanping..*.*(..))" ) public void pointcut(){} //前置增强的通知 @Before ( "pointcut()" ) public void before(){ System.out.println( "前置增强......" ); } |
知识要点
注解aop开发步骤
①使用@Aspect标注切面类
②使用@通知注解标注通知方法
③在配置文件中配置aop自动代理aop:aspectj-autoproxy/\
最后
大家看完有什么不懂的可以在下方留言讨论,也可以关注我私信问我,我看到后都会回答的。也可以关注我的公众号:前程有光,金三银四跳槽面试季,整理了1000多道将近500多页pdf文档的Java面试题资料,文章都会在里面更新,整理的资料也会放在里面。谢谢你的观看,觉得对你有帮助的话可以专注我点个赞支持一下!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步