您好,登錄后才能下訂單哦!
這篇“Spring AOP核心功能源碼分析”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Spring AOP核心功能源碼分析”文章吧。
package com.zxc.boot.proxy; public class OrderService { public void create() { System.out.println("創(chuàng)建訂單"); } public void payOrder() { System.out.println("支付訂單"); } }
假設(shè)你有如上的對(duì)象,需要對(duì)兩個(gè)方法前面都插入生成訂單號(hào)的邏輯,如果是傳統(tǒng)的方式就可以直接加入,但是過(guò)于麻煩,如果使用spring的話,就可以借助如下的工具類,如
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) { //被代理對(duì)象 OrderService orderService = new OrderService(); ProxyFactory proxyFactory = new ProxyFactory(); //設(shè)置代理對(duì)象 proxyFactory.setTarget(orderService); //添加代理邏輯 proxyFactory.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("-----生成訂單號(hào)------"); } }); //獲取代理對(duì)象 OrderService orderServiceProxy = (OrderService) proxyFactory.getProxy(); orderServiceProxy.create(); orderServiceProxy.payOrder(); } }
生成的結(jié)果如下(注:這里沒(méi)有接口,肯定是使用cglib生成的代理對(duì)象)
是不是很簡(jiǎn)單呢,底層邏輯都是spring幫我們實(shí)現(xiàn)的,而MethodBeforeAdvice就是進(jìn)行的代理邏輯,它的父接口是
這個(gè)簡(jiǎn)單理解就是對(duì)象被代理的邏輯,主要有以下的實(shí)現(xiàn),如
MethodBeforeAdvice、AfterReturningAdvice、MethodInterceptor等等見名思義
但是這里有一個(gè)問(wèn)題,我們兩個(gè)方法都被進(jìn)行了代理,那么是否有辦法實(shí)現(xiàn)只代理某個(gè)方法,而某些方法不進(jì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) { //被代理對(duì)象 OrderService orderService = new OrderService(); ProxyFactory proxyFactory = new ProxyFactory(); //設(shè)置代理對(duì)象 proxyFactory.setTarget(orderService); //添加代理邏輯 proxyFactory.addAdvisor(new PointcutAdvisor() { @Override public Pointcut getPointcut() { //哪些方法進(jìn)行代理 return new StaticMethodMatcherPointcut() { @Override public boolean matches(Method method, Class<?> aClass) { //方法名為create進(jìn)行代理 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; } }); //獲取代理對(duì)象 OrderService orderServiceProxy = (OrderService) proxyFactory.getProxy(); orderServiceProxy.create(); orderServiceProxy.payOrder(); } }
可以看到,只有創(chuàng)建訂單的方法才會(huì)添加代理邏輯,而支付訂單并不會(huì)加入這段邏輯,而核心的功能點(diǎn)就是依賴于Pointcut對(duì)象
Pointcut簡(jiǎn)單理解就是切掉,也就是用于判斷要在哪些方法或者哪些類注入代理邏輯用的
而Advisor簡(jiǎn)單理解就是Advice和Pointcut的組合,spring當(dāng)中進(jìn)行代理的邏輯也是用Advisor為維度進(jìn)行處理的
以上,就是使用ProxyFactory進(jìn)行代理邏輯的spring工具類,但是很明顯這樣使用相對(duì)來(lái)說(shuō)還是比較麻煩的,所以spring提供了簡(jiǎn)易的方式讓我們使用這種邏輯,如下
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(); } }
只要進(jìn)行如上的配置,就可以識(shí)別到了,這種方式其實(shí)跟原有的差不多,只不過(guò)spring幫我們處理了最終會(huì)返回對(duì)應(yīng)的代理bean回去,但是還有更簡(jiǎn)單的方式,如下
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() { //方法名稱藍(lán)機(jī)器 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; } //核心類,一個(gè)BeanPostProccess后置處理器,用于把掃描到的Advisor進(jìn)行代理 @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(); } }
不用我們多做其他處理,就可以對(duì)ioc容器中方法有create的類進(jìn)行代理,你可以再添加一個(gè)類,如下
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(); } }
這樣的方式就方便多了
其實(shí)DefaultAdvisorAutoProxyCreator只是需要導(dǎo)入到ioc容器中,所以配置類可以使用import進(jìn)行處理,效果是一樣的,如下
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() { //方法名稱藍(lán)機(jī)器 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; } // //核心類,一個(gè)BeanPostProccess后置處理器,用于把掃描到的Advisor進(jìn)行代理 // @Bean // public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { // return new DefaultAdvisorAutoProxyCreator(); // } }rvice(); } @Bean public DefaultPointcutAdvisor defaultPointcutAdvisor() { //方法名稱藍(lán)機(jī)器 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; } // //核心類,一個(gè)BeanPostProccess后置處理器,用于把掃描到的Advisor進(jìn)行代理 // @Bean // public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { // return new DefaultAdvisorAutoProxyCreator(); // } }
如果你不導(dǎo)入DefaultAdvisorAutoProxyCreator對(duì)象,那么代理邏輯就不會(huì)生效,本質(zhì)就是DefaultAdvisorAutoProxyCreator類就是一個(gè)BeanPostProcessor處理器,它會(huì)針對(duì)所有類進(jìn)行判斷然后處理。
以上就是關(guān)于“Spring AOP核心功能源碼分析”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。