使用 Spring 应用程序上下文时 getClass().getAnnotations() 不会返回所有注释
当我使用 applicationContext.getBeansWithAnnotation(...)
要求 Spring 应用程序上下文为我提供应用程序的主类时,我只能访问其中两个类注释:
@SpringBootApplication< /code>
@EnableJpaRepositories
因此,如果我请求带有注释 @EntityScan
的 bean,spring 上下文会正确地给我主类,但我无法访问@EntityScan
注释,就好像它不存在一样。
为什么 ?
主类
@SpringBootApplication(scanBasePackages = "foo")
@EnableJpaRepositories(basePackages = "foo.repository", repositoryBaseClass = AppRepositoryImpl.class)
@EntityScan(basePackages = "foo.entity") // Spring annotation -> I don't see it
@MiscScan(basePackages = "foo.misc") // My own annotation -> I don't see it
public class APIApplication {
public static void main(String[] args) {
SpringApplication.run(APIApplication.class, args);
}
}
从 spring 应用程序上下文中检索主类
// I can search the main class using any of the 4 aforementioned annotations, it does not matter.
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getAnnotations()
请注意:
- 更改注释的顺序并不重要。
APIApplication.class.getAnnotations()
正确地给我所有注释。
编辑:该类由 spring 代理,因此要获取真正的类和所有注释,我必须这样做:
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getSuperclass().getAnnotations()
编辑 2:我找到了一个更干净的解决方案:
org.springframework.util.ClassUtils.getUserClass(applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass()).getAnnotations()
When I ask the spring application context to give me the main class of my application, using applicationContext.getBeansWithAnnotation(...)
, I can only access two of the class annotations :
@SpringBootApplication
@EnableJpaRepositories
So if I ask for the bean with the annotation @EntityScan
, the spring context correctly give me the main class, but I can not access the @EntityScan
annotation, as if it was not present.
Why ?
Main class
@SpringBootApplication(scanBasePackages = "foo")
@EnableJpaRepositories(basePackages = "foo.repository", repositoryBaseClass = AppRepositoryImpl.class)
@EntityScan(basePackages = "foo.entity") // Spring annotation -> I don't see it
@MiscScan(basePackages = "foo.misc") // My own annotation -> I don't see it
public class APIApplication {
public static void main(String[] args) {
SpringApplication.run(APIApplication.class, args);
}
}
Retrieving the main class from the spring application context
// I can search the main class using any of the 4 aforementioned annotations, it does not matter.
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getAnnotations()
Note that :
- Changing the order of the annotations does not matter.
APIApplication.class.getAnnotations()
correctly give me all the annotations.
EDIT : The class was proxied by spring, so the get the real class and all the annotations I have to do this :
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getSuperclass().getAnnotations()
EDIT 2 : I found a cleaner solution :
org.springframework.util.ClassUtils.getUserClass(applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass()).getAnnotations()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论