簡體   English   中英

Spring AOP方面不攔截帶注釋的方法

[英]Spring AOP aspect does not intercept annotated method

我想在函數中使用@before@after@AfterThrowing 如果其他函數使用@MyAspectTest之類的注釋,則應在相關時間運行beforeAction()afterAction()afterExcept() 但是,這似乎不起作用。

我已經輸入了依賴項並修改了bean。

package com.service.metrics;

import com.mgr.CMPMgr;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.AfterThrowing;

import java.lang.reflect.Method;

@Aspect
public class CMPAspect {
    @Before(value="@annotation(com.mgr.CMPMgr)")
    public void beforeAction(JoinPoint joinPoint) throws ClassNotFoundException {
      testcode
    }

    @After(value="@annotation(com.mgr.CMPMgr)")
    public void afterAction(){
      testcode
    }

    @AfterThrowing(value="@annotation(com.mgr.CMPMgr)")
    public void afterExcept(){
      testcode
    }
}
package com.mgr;

public @interface CMPMgr {
    String name() default "";
    long startTime = System.currentTimeMillis();
}
    @CMPMgr(name = "vipGet")
    @GET
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    @Path("/{lbId}")
    public Response get(@PathParam("lbId")String lbId,
                        @HeaderParam("Authorization") String basicAuthData,
                        @HeaderParam("UserID") String behalf) {
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <aop:aspectj-autoproxy />
    <!-- enabling annotation driven configuration / -->
    <context:annotation-config />
    <context:component-scan />

    <!-- responsible for registering the necessary Spring components that power
        annotation-driven transaction management; such as when @Transactional methods
        are invoked -->
    <tx:annotation-driven />

它應該進入Aspect函數。 但是我發現它不能在調試模式下工作。 為什么?

我想到了一些事情:

  • 您的注釋需要運行時保留,但在您的代碼@Retention(RetentionPolicy.RUNTIME)不到@Retention(RetentionPolicy.RUNTIME)
  • 您的方面應該是@Component ,但是我也看不到相應的注釋。
  • 包含方法public Response get(..)的目標類也必須是Spring Bean /組件。 因為您只顯示了不連貫的代碼片段,而不是完整的類定義,所以我不知道該類駐留在哪個包中(如果它是Spring組件)以及是否由組件掃描拾取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM