gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:22:26 26 4
gpt4 key购买 nike

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

最佳答案

我想到了一些事情:

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

关于java - Spring AOP切面不拦截带注释的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com