Spring?AOP核心功能示例代碼詳解
前言
spring一直以來都是我們Java開發(fā)中最核心的一個技術(shù),其中又以ioc和aop為主要技術(shù),本篇文章主要講一下aop的核心技術(shù),也就是ProxyFactory技術(shù)的使用,而基本的jdk動態(tài)代理和cglib代理技術(shù)并不涉及,如有需要,請自行尋找資料
背景
package com.zxc.boot.proxy; public class OrderService { public void create() { System.out.println("創(chuàng)建訂單"); } public void payOrder() { System.out.println("支付訂單"); } }
假設(shè)你有如上的對象,需要對兩個方法前面都插入生成訂單號的邏輯,如果是傳統(tǒng)的方式就可以直接加入,但是過于麻煩,如果使用spring的話,就可以借助如下的工具類,如
ProxyFactory
package com.zxc.boot.proxy; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.ProxyFactory; import java.lang.reflect.Method; public class Main { public static void main(String[] args) { //被代理對象 OrderService orderService = new OrderService(); ProxyFactory proxyFactory = new ProxyFactory(); //設(shè)置代理對象 proxyFactory.setTarget(orderService); //添加代理邏輯 proxyFactory.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("-----生成訂單號------"); } }); //獲取代理對象 OrderService orderServiceProxy = (OrderService) proxyFactory.getProxy(); orderServiceProxy.create(); orderServiceProxy.payOrder(); } }
生成的結(jié)果如下(注:這里沒有接口,肯定是使用cglib生成的代理對象)
是不是很簡單呢,底層邏輯都是spring幫我們實現(xiàn)的,而MethodBeforeAdvice就是進行的代理邏輯,它的父接口是
Advice
這個簡單理解就是對象被代理的邏輯,主要有以下的實現(xiàn),如
MethodBeforeAdvice、AfterReturningAdvice、MethodInterceptor等等見名思義
但是這里有一個問題,我們兩個方法都被進行了代理,那么是否有辦法實現(xiàn)只代理某個方法,而某些方法不進行代理呢,答案是有的,代碼如下
package com.zxc.boot.proxy; import org.aopalliance.aop.Advice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.Pointcut; import org.springframework.aop.PointcutAdvisor; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.StaticMethodMatcherPointcut; import java.lang.reflect.Method; public class Main2 { public static void main(String[] args) { //被代理對象 OrderService orderService = new OrderService(); ProxyFactory proxyFactory = new ProxyFactory(); //設(shè)置代理對象 proxyFactory.setTarget(orderService); //添加代理邏輯 proxyFactory.addAdvisor(new PointcutAdvisor() { @Override public Pointcut getPointcut() { //哪些方法進行代理 return new StaticMethodMatcherPointcut() { @Override public boolean matches(Method method, Class<?> aClass) { //方法名為create進行代理 return method.getName().equals("create"); } }; } //代理邏輯 @Override public Advice getAdvice() { return new MethodBeforeAdvice() { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("-----創(chuàng)建訂單-----"); } }; } @Override public boolean isPerInstance() { return false; } }); //獲取代理對象 OrderService orderServiceProxy = (OrderService) proxyFactory.getProxy(); orderServiceProxy.create(); orderServiceProxy.payOrder(); } }
可以看到,只有創(chuàng)建訂單的方法才會添加代理邏輯,而支付訂單并不會加入這段邏輯,而核心的功能點就是依賴于Pointcut對象
Pointcut
Pointcut簡單理解就是切掉,也就是用于判斷要在哪些方法或者哪些類注入代理邏輯用的
Advisor
而Advisor簡單理解就是Advice和Pointcut的組合,spring當(dāng)中進行代理的邏輯也是用Advisor為維度進行處理的
以上,就是使用ProxyFactory進行代理邏輯的spring工具類,但是很明顯這樣使用相對來說還是比較麻煩的,所以spring提供了簡易的方式讓我們使用這種邏輯,如下
Spring提供的代理支持
ProxyFactoryBean
package com.zxc.boot.proxy; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.ProxyFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import java.lang.reflect.Method; @Configuration @ComponentScan("com.zxc.boot.proxy") public class AppConfig { @Bean public ProxyFactoryBean proxyFactoryBean() { ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean(); proxyFactoryBean.setTarget(new OrderService()); proxyFactoryBean.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("-------創(chuàng)建訂單-------"); } }); return proxyFactoryBean; } }
package com.zxc.boot.proxy; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); OrderService orderService = applicationContext.getBean(OrderService.class); orderService.create(); orderService.payOrder(); } }
只要進行如上的配置,就可以識別到了,這種方式其實跟原有的差不多,只不過spring幫我們處理了最終會返回對應(yīng)的代理bean回去,但是還有更簡單的方式,如下
DefaultPointcutAdvisor
package com.zxc.boot.proxy; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.NameMatchMethodPointcut; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import java.lang.reflect.Method; @Configuration @ComponentScan("com.zxc.boot.proxy") public class AppConfig2 { @Bean public OrderService orderService() { return new OrderService(); } @Bean public DefaultPointcutAdvisor defaultPointcutAdvisor() { //方法名稱藍機器 NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut(); nameMatchMethodPointcut.addMethodName("create"); //設(shè)置攔截和代理邏輯 DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); defaultPointcutAdvisor.setPointcut(nameMatchMethodPointcut); defaultPointcutAdvisor.setAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("-------創(chuàng)建訂單------"); } }); return defaultPointcutAdvisor; } //核心類,一個BeanPostProccess后置處理器,用于把掃描到的Advisor進行代理 @Bean public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { return new DefaultAdvisorAutoProxyCreator(); } }
package com.zxc.boot.proxy; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig2.class); OrderService orderService = applicationContext.getBean(OrderService.class); orderService.create(); orderService.payOrder(); } }
不用我們多做其他處理,就可以對ioc容器中方法有create的類進行代理,你可以再添加一個類,如下
package com.zxc.boot.proxy; public class UserService { public void create() { System.out.println("用戶service哦哦哦"); } }
package com.zxc.boot.proxy; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig2.class); OrderService orderService = applicationContext.getBean(OrderService.class); orderService.create(); orderService.payOrder(); UserService userService = applicationContext.getBean(UserService.class); userService.create(); } }
這樣的方式就方便多了
優(yōu)化處理
其實DefaultAdvisorAutoProxyCreator只是需要導(dǎo)入到ioc容器中,所以配置類可以使用import進行處理,效果是一樣的,如下
package com.zxc.boot.proxy; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.NameMatchMethodPointcut; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import java.lang.reflect.Method; @Configuration @ComponentScan("com.zxc.boot.proxy") @Import(DefaultAdvisorAutoProxyCreator.class) public class AppConfig2 { @Bean public UserService userService() { return new UserService(); } @Bean public OrderService orderService() { return new OrderSepackage com.zxc.boot.proxy; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.NameMatchMethodPointcut; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import java.lang.reflect.Method; @Configuration @ComponentScan("com.zxc.boot.proxy") @Import(DefaultAdvisorAutoProxyCreator.class) public class AppConfig2 { @Bean public UserService userService() { return new UserService(); } @Bean public OrderService orderService() { return new OrderService(); } @Bean public DefaultPointcutAdvisor defaultPointcutAdvisor() { //方法名稱藍機器 NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut(); nameMatchMethodPointcut.addMethodName("create"); //設(shè)置攔截和代理邏輯 DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); defaultPointcutAdvisor.setPointcut(nameMatchMethodPointcut); defaultPointcutAdvisor.setAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("-------創(chuàng)建訂單------"); } }); return defaultPointcutAdvisor; } // //核心類,一個BeanPostProccess后置處理器,用于把掃描到的Advisor進行代理 // @Bean // public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { // return new DefaultAdvisorAutoProxyCreator(); // } }rvice(); } @Bean public DefaultPointcutAdvisor defaultPointcutAdvisor() { //方法名稱藍機器 NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut(); nameMatchMethodPointcut.addMethodName("create"); //設(shè)置攔截和代理邏輯 DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); defaultPointcutAdvisor.setPointcut(nameMatchMethodPointcut); defaultPointcutAdvisor.setAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("-------創(chuàng)建訂單------"); } }); return defaultPointcutAdvisor; } // //核心類,一個BeanPostProccess后置處理器,用于把掃描到的Advisor進行代理 // @Bean // public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { // return new DefaultAdvisorAutoProxyCreator(); // } }
如果你不導(dǎo)入DefaultAdvisorAutoProxyCreator對象,那么代理邏輯就不會生效,本質(zhì)就是DefaultAdvisorAutoProxyCreator類就是一個BeanPostProcessor處理器,它會針對所有類進行判斷然后處理
總結(jié)
spring的aop核心技術(shù)就是最終會利用到這個對象進行代理,而這里先把底層的代理邏輯進行講明,后面對整個aop流程進行理解就方便多了
到此這篇關(guān)于Spring AOP核心功能示例代碼詳解的文章就介紹到這了,更多相關(guān)Spring AOP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決
這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06基于SpringBoot和Vue實現(xiàn)分片上傳系統(tǒng)
最近想做一個關(guān)于文件上傳的個人小網(wǎng)盤,一開始嘗試使用了OSS的方案,但是該方案對于大文件來說并不友好,所以開始嘗試分片上傳方案的探索,接下來小編給大家詳細的介紹一下如何基于SpringBoot和Vue實現(xiàn)分片上傳系統(tǒng),需要的朋友可以參考下2023-12-12詳解Java?POI?excel自定義設(shè)置單元格格式
這篇文章主要介紹了Java?POI?excel設(shè)置單元格格式,自定義設(shè)置,設(shè)置單元格格式:來源_formats,更多數(shù)據(jù)類型從formats里面發(fā)現(xiàn),需要的朋友可以參考下2024-01-01