亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了Spring AOP和AspectJ采用相同的方法的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)關(guān)于使用AspectJ和Spring AOP方法攔截的問題。我創(chuàng)建了兩個(gè)批注:@AJTest@SAOPTest。

package com.test.company;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AJTestAspect {

    @Pointcut("@annotation(AJTest)")
    public void aJTest() {
    }

    @Around("aJTest()")
    public Object profile(ProceedingJoinPoint joinPoint) throws Throwable {
        final long start = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } finally {
            long finish = System.currentTimeMillis() - start;
            System.out.println("Method execution time: " + (start - finish));
        }
    }
}

已注冊

package com.test.company;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AJConfiguration {

    @Bean
    public AJTestAspect ajTestAspect() {
        return new AJTestAspect();
    }
}

和其他

package com.test.company;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SAOPInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Number of parameters " + methodInvocation.getArguments().length);
        return methodInvocation.getMethod().invoke(methodInvocation.getThis(), methodInvocation.getArguments());
    }
}

并注冊

package com.test.company;

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
public class SpringSAOPTestConfiguration {

    @Bean
    public Advisor springAopTestAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("@annotation(com.test.company.SAOPTest)");
        return new DefaultPointcutAdvisor(pointcut, new SAOPInterceptor());
    }
}

并將其添加到控制器中的我的方法

package com.test.company;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class TestController {
    
        @SAOPTest
        @AJTest
        @GetMapping("/test")
        public String doSomething(@RequestParam("firstParam") String firstParam, @RequestParam("secondParam") Integer secondParam) throws InterruptedException {
            Thread.sleep(2_500);
            return firstParam + " " + secondParam;
        }
    }

應(yīng)用程序類

package com.test.company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@SpringBootApplication
public class AopTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(AopTestApplication.class, args);
    }
}

但當(dāng)我通過http://localhost:8080/test?firstParam=test&secondParam=2調(diào)用它時(shí),我看不到與方法執(zhí)行時(shí)間相關(guān)的消息,但可以看到傳遞給該方法的參數(shù)數(shù)量。如果我要?jiǎng)h除@SAOPTest-方法的執(zhí)行時(shí)間按預(yù)期工作,但它不能同時(shí)使用兩個(gè)注釋。是Spring創(chuàng)建的代理對(duì)象有問題,還是我遺漏了什么?

推薦答案

您的偵聽器未正確運(yùn)行。請(qǐng)閱讀MethodInterceptorjavadoc。攔截器應(yīng)如下所示:

public class SAOPInterceptor implements MethodInterceptor {
  @Override
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    System.out.println("Number of parameters " + methodInvocation.getArguments().length);
    return methodInvocation.proceed();
  }
}

此外,你的相位計(jì)算時(shí)間也是錯(cuò)誤的。首先,您說finish = System.currentTimeMillis() - start,然后打印start - finish。您應(yīng)該減去finish - start或計(jì)算在變量中花費(fèi)的時(shí)間,但不能同時(shí)計(jì)算兩者,也不能同時(shí)計(jì)算start - finish。為什么不干脆System.out.println("Method execution time: " + (System.currentTimeMillis() - start));?

這篇關(guān)于Spring AOP和AspectJ采用相同的方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:AOP AspectJ Spring 方法 采用
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評(píng)定2018-06-03

通用課目體育訓(xùn)練成績評(píng)定