Java 在Spring引导应用程序中,不会执行基于注释的切入点
您好,我已经创建了SpringBoot应用程序,并尝试使用SpringAOP应用方面。代码如下 自定义计时器注释Java 在Spring引导应用程序中,不会执行基于注释的切入点,java,spring,spring-boot,spring-aop,Java,Spring,Spring Boot,Spring Aop,您好,我已经创建了SpringBoot应用程序,并尝试使用SpringAOP应用方面。代码如下 自定义计时器注释 package org.my.pckg.annotation; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface Timer { } 计时方面 @Aspect @Component public class TimeLoggingAspect { @Pointc
package org.my.pckg.annotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Timer {
}
计时方面
@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 "";
}
}
config类
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();
}
}
示例测试控制器
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
当我使用以下方法对应用程序进行bug时,使用上述设置:
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)")
致:
阅读上的Spring文档。从以下位置更改切入点:
@Pointcut("execution(@org.my.pckg.annotation.Timer)")
致:
阅读上的Spring文档。经过多次尝试和错误,我找到了上述问题的解决方案 创建一个文件aop.xml并将其放入resource/META-INF/中,其中包含以下内容
<?xml version="1.0"?>
<aspectj>
<!-- place all aspects here -->
<aspects>
<aspect name="<full qualified name of aspect class>"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
</weaver>
在这里找到更详细的解决方案
经过多次尝试和错误,我找到了上述问题的解决方案
创建一个文件aop.xml并将其放入resource/META-INF/中,其中包含以下内容
<?xml version="1.0"?>
<aspectj>
<!-- place all aspects here -->
<aspects>
<aspect name="<full qualified name of aspect class>"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
</weaver>
在这里找到更详细的解决方案
您好,谢谢您的回复。。我尝试了@annotation选项,但没有成功。您好,谢谢回复。。我尝试了@annotation选项,但没有成功。