欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?AOP核心功能示例代碼詳解

 更新時間:2023年02月28日 09:28:56   作者:zxc_user  
AOP面向切面編程,它是一種思想,它是對某一類事情的集中處理,而AOP是一種思想,而Spring?AOP是一個框架,提供了一種對AOP思想的實現(xiàn),它們的關(guān)系和loC與DI類似,這篇文章主要介紹了Spring?AOP統(tǒng)一功能處理示例代碼,需要的朋友可以參考下

前言

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訪問圖片本地路徑并映射成url

    使用springboot訪問圖片本地路徑并映射成url

    這篇文章主要介紹了使用springboot訪問圖片本地路徑并映射成url的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • SpringMVC方法返回值多種情況代碼實例

    SpringMVC方法返回值多種情況代碼實例

    這篇文章主要介紹了SpringMVC方法返回值多種情況代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • 對handlerexecutionchain類的深入理解

    對handlerexecutionchain類的深入理解

    下面小編就為大家?guī)硪黄獙andlerexecutionchain類的深入理解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 基于Java制作一個簡易的遠控終端

    基于Java制作一個簡易的遠控終端

    這篇文章主要為大家詳細介紹了如何基于Java制作一個簡易的遠控終端,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的可以了解一下
    2023-04-04
  • IDEA編譯亂碼Build Output提示信息亂碼

    IDEA編譯亂碼Build Output提示信息亂碼

    IDEA編譯的時候亂碼,Build Output提示信息亂碼,本文就詳細的介紹一下解決方法,有需要的同學(xué)可以了解一下
    2021-06-06
  • 基于SpringBoot和Vue實現(xiàn)分片上傳系統(tǒng)

    基于SpringBoot和Vue實現(xiàn)分片上傳系統(tǒng)

    最近想做一個關(guān)于文件上傳的個人小網(wǎng)盤,一開始嘗試使用了OSS的方案,但是該方案對于大文件來說并不友好,所以開始嘗試分片上傳方案的探索,接下來小編給大家詳細的介紹一下如何基于SpringBoot和Vue實現(xiàn)分片上傳系統(tǒng),需要的朋友可以參考下
    2023-12-12
  • Java 正則表達式詳細使用

    Java 正則表達式詳細使用

    這篇文章主要介紹了Java 正則表達式詳細使用,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-10-10
  • JAVA 深層拷貝 DeepCopy的使用詳解

    JAVA 深層拷貝 DeepCopy的使用詳解

    最近需要用到比較兩個對象屬性的變化,其中一個是oldObj,另外一個是newObj,oldObj是newObj的前一個狀態(tài),所以需要在newObj的某個狀態(tài)時,復(fù)制一個一樣的對象,由于JAVA不支持深層拷貝,因此專門寫了一個方法
    2013-07-07
  • 詳解Java?POI?excel自定義設(shè)置單元格格式

    詳解Java?POI?excel自定義設(shè)置單元格格式

    這篇文章主要介紹了Java?POI?excel設(shè)置單元格格式,自定義設(shè)置,設(shè)置單元格格式:來源_formats,更多數(shù)據(jù)類型從formats里面發(fā)現(xiàn),需要的朋友可以參考下
    2024-01-01

最新評論