Spring AOP方面不拦截带注释的方法

时间:2019-08-12 06:22:18

标签: java aspectj spring-aop

我想在我的函数中使用@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函数。但是我发现它不能在调试模式下工作。为什么?

1 个答案:

答案 0 :(得分:2)

一些事情浮现在脑海:

  • 您的注释需要保留运行时,但是在您的代码中看不到@Retention(RetentionPolicy.RUNTIME)
  • 您的外观应为@Component,但我也看不到相应的注释。
  • 包含方法public Response get(..)的目标类也必须是Spring bean /组件。因为您只显示不连贯的代码片段,而不是完整的类定义,所以我不知道该类驻留在哪个包中(如果它是Spring组件)以及是否由组件扫描来拾取。
相关问题