gpt4 book ai didi

java - 在Spring Boot应用程序中,不执行基于注释的切入点

转载 作者:行者123 更新时间:2023-12-02 03:41:04 25 4
gpt4 key购买 nike

嗨,我已经创建了 Spring Boot 应用程序,并尝试使用 Spring AOP 应用 Aspect。代码如下...

自定义定时器注释

package org.my.pckg.annotation;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Timer {
}

TimerLoggingAspect 方面

@Aspect
@Component
public class TimeLoggingAspect {

@Pointcut("annotation(@org.my.pckg.annotation.Timer)")
public void loggingPointCutDefinition(){}

@Around("loggingPointCutDefinition()")
public void userAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
createJsonString(joinPoint);
joinPoint.proceed();
}

private String createJsonString(ProceedingJoinPoint joinPoint) {
//logic for creating and printing json
return "";
}
}

配置类

package org.my.pckg.config;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.my.pckg.utilities","org.my.pckg.annotation"})
public class AssetConfig {

@Bean
public TimeLoggingAspect timeLoggingAspect() {
return new TimeLoggingAspect();
}
}

示例测试 Controller

package org.my.pckg;
@SpringBootApplication
@PropertySources({
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
})
@Configuration
@ComponentScan(basePackages = {"org.my.pckg.config","org.my.pckg.annotation"})
@RestController
@EnableAutoConfiguration
@EnableAspectJAutoProxy
public class Example {

@Timer
@RequestMapping("/")
String home() {
return "Hello World!";
}

public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}

还有application.properties包含以下内容:

spring.aop.proxy-target-class=true

当我使用上述设置来调试应用程序时:

spring-boot:run "-Drun.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

方面未执行,但如果我更改切入点来自@Around("@annotation(org.my.pckg.annotation.Timer)")@Around("execution( * org.my.pckg.*.*(..))")效果非常好!

请帮助找出定义自定义注释时缺少的内容..

最佳答案

更改切入点:

@Pointcut("execution(@org.my.pckg.annotation.Timer)")

致:

@Pointcut("@annotation(org.my.pckg.annotation.Timer)")

阅读 Declaring a Pointcut 上的 Spring 文档.

关于java - 在Spring Boot应用程序中,不执行基于注释的切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36830123/

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