Spring ApplicationContext 获取类注解为null

问题描述

在controller层使用了自定义注解,使用了切面。使用如下代码,无法获取到自定义注解。

//如何写SpringUtil请参照其他博客
applicationContext().getBean(name);
//以下代码报错空指针 sqlFile.getClass().getAnnotation(SQLFileAnnotation.class)
Object sqlFile=sqlFile.getClass().getAnnotation(SQLFileAnnotation.class).SQLFileName()

原因

由于使用切面导致获取到的类发生了变化。

正常类:

Spring ApplicationContext 获取类注解为null

使用切面类:

Spring ApplicationContext 获取类注解为null

使用切面类 getClass方法后,无法获取注解。

Spring ApplicationContext 获取类注解为null

正常类:

Spring ApplicationContext 获取类注解为null

解决办法

不再需要获取注解的类中使用切面。