无法在Spring AOP中检测类级别的自定义注释

PHP复仇者

我正在尝试使用以下设置拦截Spring中的类

拦截器

@Aspect
@Component
public class MyInterceptor {

    @Around("execution(* com.example.services..*(..))")
    public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
        if (pjp.getTarget() != null && pjp.getTarget().getClass().getAnnotation(MyAnnotation.class) != null) {
            System.out.println("Yes annotation present");
        } else {
            System.out.println("Annotation not present");
        }

        return = pjp.proceed();
    }   
}

并且被拦截如下

@Component
@MyAnnotation
public class MyAlert implements IAlert {

}

除非我进行以下更改,否则一切正常

@Configuration
@PropertySource(value = { "file:${catalina.home}/conf/my.properties" })
@Component
@MyAnnotation
public class MyAlert implements IAlert {
    @Autowired
    private Environment environment;
} 

完成这些更改后,我想读取位于tomcat7的conf文件夹中的属性,我的拦截器无法读取我的自定义注释@MyAnnotation它说(写在拦截器中的自定义消息)

注释不存在

这是自定义注释

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

我真的希望该类被拦截,并且如果被注释,则必须在该类上检测到注释。我还想从该拦截类的conf文件夹中读取属性。

克里加克斯

您对批注的检查失败,因为您正在检查动态代理的类型,而不是实际的已注释类的类型。您可以像这样解决它:

pjp.getSignature().getDeclaringType().getAnnotation(RestController.class) != null

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Spring AOP中获取类级别的注释值

Spring AOP自定义注释在Handler Interceptor类中给出错误

Spring AOP自定义注释

在Spring上下文中查找方法级别的自定义注释

自定义注释的Spring AOP切入点在内部静态类中不起作用

在类级别带有RequestMapping注释的Spring Boot Actuator自定义RestControllerEndpoint

在Spring中实现自定义注释

无法从tomcat中的自定义类加载器加载的类中获取注释

Spring Boot中的类级别和方法级别的注释方面(NullPointerException)

自定义ConfigurationSection中无法识别的元素异常

Spring Boot:自定义AutoConfiguration类无法找到属性

自定义Spring AOP注释不适用于默认方法

Spring-无法在自定义ClientHttpRequestInterceptor实现类中记录请求正文

在Spring Boot环境中实现自定义注释

在Hibernate + Spring中无法解析自定义验证消息

自定义Spring AOP周围+ @Transactional

spring aop无法将值传递给自定义参数

Spring自定义注释的行为

Spring Boot AOP无法获取注释参数

无法在连接的自定义UITableViewCell类中创建插座

无法在Xcode中为UITableViewController设置自定义类

数据无法到达AsynTask类中的自定义listview

Spring Boot自定义启动器-无法导入自定义启动器类

Spring Boot和Spring Security在AuthenticationEntryPoint中无法通过自定义消息发送错误

使用Jackson处理自定义json中的“无法识别的令牌”异常

带有自定义配置部分的配置文件中无法识别的元素“ Item”

当前riscv-gnu-toolchain中无法识别的自定义asm助记符

数据透视表中无法识别的最大DateTime(自定义)字段

Spring JPA 无法在 Kotlin 数据类中使用自定义 setter 映射字段