激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用Spring方法攔截器MethodInterceptor

使用Spring方法攔截器MethodInterceptor

2022-03-02 00:33淡淡的倔強 Java教程

這篇文章主要介紹了使用Spring方法攔截器MethodInterceptor,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

 

Spring方法攔截器MethodInterceptor

 

前言

實現MethodInterceptor 接口,在調用目標對象的方法時,就可以實現在調用方法之前、調用方法過程中、調用方法之后對其進行控制。

MethodInterceptor 接口可以實現MethodBeforeAdvice接口、AfterReturningAdvice接口、ThrowsAdvice接口這三個接口能夠所能夠實現的功能,但是應該謹慎使用MethodInterceptor 接口,很可能因為一時的疏忽忘記最重要的MethodInvocation而造成對目標對象方法調用失效,或者不能達到預期的設想。

示例代碼如下

public class TestMethodInterceptor  {
    public static void main(String[] args) {
        ProxyFactory proxyFactory=new ProxyFactory();
        proxyFactory.setTarget(new TestMethodInterceptor());
        proxyFactory.addAdvice(new adviseMethodInterceptor());
        Object proxy = proxyFactory.getProxy();
        TestMethodInterceptor methodInterceptor = (TestMethodInterceptor) proxy;
        methodInterceptor.doSomeThing("通過代理工廠設置代理對象,攔截代理方法");
    }
    public static class adviseMethodInterceptor implements MethodInterceptor{
        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            Object result=null;
            try{
                System.out.println("方法執行之前:"+methodInvocation.getMethod().toString());
                result= methodInvocation.proceed();
                System.out.println("方法執行之后:"+methodInvocation.getMethod().toString());
                System.out.println("方法正常運行結果:"+result);
                return result;
            }catch (Exception e){
                System.out.println("方法出現異常:"+e.toString());
                System.out.println("方法運行Exception結果:"+result);
                return result;
            }
        }
    }
    public String doSomeThing(String someThing){
        //int i=5/0;
        return "執行被攔截的方法:"+someThing;
    }
}

正常運行結果:

方法執行之前:public java.lang.String com.blog.test.aop.TestMethodInterceptor.doSomeThing(java.lang.String)

方法執行之后:public java.lang.String com.blog.test.aop.TestMethodInterceptor.doSomeThing(java.lang.String)

方法正常運行結果:執行被攔截的方法:通過代理工廠設置代理對象,攔截代理方法

異常運行結果:

方法執行之前:public java.lang.String com.blog.test.aop.TestMethodInterceptor.doSomeThing(java.lang.String)

方法出現異常:java.lang.ArithmeticException: / by zero

方法運行Exception結果:null

 

Spring攔截器實現+后臺原理(MethodInterceptor)

 

MethodInterceptor

MethodInterceptor是AOP項目中的攔截器(注:不是動態代理攔截器),區別與HandlerInterceptor攔截目標時請求,它攔截的目標是方法。

實現MethodInterceptor攔截器大致也分為兩種:

(1)MethodInterceptor接口;

(2)利用AspectJ的注解配置;

 

MethodInterceptor接口

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MethodInvokeInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before method invoke....");
        Object object = methodInvocation.proceed();
        System.out.println("after method invoke.....");
        return object;
    }
}
<!-- 攔截器 demo -->
    <bean id="methodInvokeInterceptor" class="com.paic.phssp.springtest.interceptor.method.MethodInvokeInterceptor"/>
    <aop:config>
        <!--切入點,controlller -->
        <aop:pointcut id="pointcut_test"   expression="execution(* com.paic.phssp.springtest.controller..*.*(..))" />
        <!--在該切入點使用自定義攔截器 ,按照先后順序執行 -->
        <aop:advisor pointcut-ref="pointcut_test" advice-ref="methodInvokeInterceptor" />
    </aop:config>
    <!-- 自動掃描使用了aspectj注解的類 -->
    <aop:aspectj-autoproxy/>

執行:

使用Spring方法攔截器MethodInterceptor

 

AspectJ的注解

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AutoAspectJInterceptor {
    @Around("execution (* com.paic.phssp.springtest.controller..*.*(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        System.out.println("AutoAspectJInterceptor begin around......");
        Object object = point.proceed();
        System.out.println("AutoAspectJInterceptor end around......");
        return object;
    }
}

運行結果:

AutoAspectJInterceptor begin around......

>>>>:isAuthenticated=false

AutoAspectJInterceptor end around......

 

簡單介紹下關鍵詞

  • AOP=Aspect Oriented Program面向切面(方面/剖面)編程
  • Advice(通知):把各組件中公共業務邏輯抽離出來作為一個獨立 的組件
  • Weave(織入):把抽離出來的組件(Advice),使用到需要使用該邏輯 地方的過程。
  • JoinPoint (連接點): Advice 組件可以weave的特征點。
  • PointCut(切入點):用來明確Advice需要織入的連接點
  • Aspect(切面):Aspect=Advice + PointCut

通知類型

  • @Before 在切點方法之前執行
  • @After 在切點方法之后執行
  • @AfterReturning 切點方法返回后執行
  • @AfterThrowing 切點方法拋異常執行
  • @Around環繞通知

執行順序:

  • @Around環繞通知
  • @Before通知執行
  • @Before通知執行結束
  • @Around環繞通知執行結束
  • @After后置通知執行了!
  • @AfterReturning

 

切面設置

可以使用&&、||、!、三種運算符來組合切點表達式

 

execution表達式

"execution(public * com.xhx.springboot.controller.*.*(..))"
  • *只能匹配一級路徑
  • ..可以匹配多級,可以是包路徑,也可以匹配多個參數
  • + 只能放在類后面,表明本類及所有子類

within(類路徑) 配置指定類型的類實例,同樣可以使用匹配符

within(com.xhx.springboot..*)

@within(annotationType) 匹配帶有指定注解的類(注:與上不同)

"@within(org.springframework.stereotype.Component)"

@annotation(annotationType) 匹配帶有指定注解的方法

"@annotation(IDataSource)"

其中:IDataSource為自定義注解

import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface IDataSource {
    String value() default "dataSource";
}

 

下面分析下Spring @Aspect

1、注冊

org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator

使用Spring方法攔截器MethodInterceptor

看到實現接口BeanPostProcessor,必然在初始化Bean前后,執行接口方法。

2、解析

AspectJAutoProxyBeanDefinitionParser.java#parse()方法

@Nullable
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
        this.extendBeanDefinition(element, parserContext);
        return null;
    }
public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(ParserContext parserContext, Element sourceElement) {
        BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext.getRegistry(), parserContext.extractSource(sourceElement));
        useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
        registerComponentIfNecessary(beanDefinition, parserContext);
    }
@Nullable
    public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, @Nullable Object source) {
        return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
    }

3、具體實現

上面提到實現接口BeanPostProcessor,必然在初始化Bean前后,執行接口方法。看下面時序圖:

AbstractAutoProxyCreator的postProcessAfterInitialization()方法

使用Spring方法攔截器MethodInterceptor

DefaultAopProxyFactory.createAopProxy()方法,具體創建代理類。兩種動態代理:JDK動態代理和CGLIB代理。

public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
        if (!config.isOptimize() && !config.isProxyTargetClass() && !this.hasNoUserSuppliedProxyInterfaces(config)) {
            return new JdkDynamicAopProxy(config);
        } else {
            Class<?> targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.");
            } else {
                return (AopProxy)(!targetClass.isInterface() && !Proxy.isProxyClass(targetClass) ? new ObjenesisCglibAopProxy(config) : new JdkDynamicAopProxy(config));
            }
        }
    }

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/u012834750/article/details/71773887

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色网站免费观看 | 久久久久亚洲视频 | 精品三级内地国产在线观看 | 国产一级毛片不卡 | 成人午夜视频网站 | 国产午夜精品一区二区三区免费 | av免费在线观看免费 | 国产精品久久久久久久久久久久久久久 | 久久国产精品一区 | 国产成人精品区一区二区不卡 | 日本综合久久 | 免费网址黄 | 欧美黑人一级 | 国产精品久久久久久模特 | 免费观看又色又爽又黄的崩锅 | 丰满年轻岳中文字幕一区二区 | 欧美成人免费香蕉 | 精品一区二区三区在线播放 | 亚洲第一页中文字幕 | 91成人久久 | 羞羞羞网站 | 黄色的视频免费看 | 国产免费观看av | 免费三级大片 | 成人免费网站在线观看视频 | 精品欧美一区二区精品久久久 | 蜜桃视频观看麻豆 | 香蕉国产片 | 日本a级一区 | 亚洲人成网站免费播放 | 一级毛片一区 | 久久精品视频免费观看 | 久久亚洲精品国产 | 精品久久久久久综合日本 | 午夜91视频 | 精品91av | 久久视讯 | 一区二区高清视频在线观看 | 日韩精品羞羞答答 | 91看片在线播放 | 国产精品一品二区三区四区18 |