如何确保控制器在 spring 引导中被特定注释注释?
How to make sure a controller is annotated by a specific annotation in spring boot?
@FeignClient
和 @RequestMapping
无法添加到同一界面。现在我想检查这两个注释是否同时使用以给出一些错误消息。
问题:
spring 是否支持类似 isAnnotatedBy(Annotation annotation)
的方法?如果没有,我怎么能在这里实现我的目标?
谢谢!
已支持名为 isAnnotationPresent
:
的方法
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
Returns true if an annotation for the specified type is present on this element, else false. This method is designed primarily for convenient access to marker annotations.
Parameters:
annotationClass - the Class object corresponding to the annotation type
Returns:
true if an annotation for the specified annotation type is present on this element, else false
Throws:
NullPointerException - if the given annotation class is null
Since:
1.5
我怀疑您遇到了与此问题相关的问题 https://github.com/spring-cloud/spring-cloud-netflix/issues/466。
spring applicationContext 提供实用方法来查找带有某些注释的 bean。
解决方案可能包括启动 applicationContext 并在那里搜索重叠注释。
为此,您需要注册一个 ApplicationListener,它会搜索您所有的 @RequestMapping bean,这些 bean 会进一步用 @FeignClient 进行注释。
实现可能如下所示:
@Component
public class ContextStartupListener
implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
for(String beanName : applicationContext.getBeanNamesForAnnotation(RequestMapping.class)) {
if(applicationContext.findAnnotationOnBean(beanName, FeignClient.class)!=null){
throw new AnnotationConfigurationException("Cannot have both @RequestMapping and @FeignClient on "+beanName);
}
}
}
}
@FeignClient
和 @RequestMapping
无法添加到同一界面。现在我想检查这两个注释是否同时使用以给出一些错误消息。
问题:
spring 是否支持类似 isAnnotatedBy(Annotation annotation)
的方法?如果没有,我怎么能在这里实现我的目标?
谢谢!
已支持名为 isAnnotationPresent
:
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
Returns true if an annotation for the specified type is present on this element, else false. This method is designed primarily for convenient access to marker annotations.
Parameters:
annotationClass - the Class object corresponding to the annotation type
Returns:
true if an annotation for the specified annotation type is present on this element, else false
Throws:
NullPointerException - if the given annotation class is null
Since:
1.5
我怀疑您遇到了与此问题相关的问题 https://github.com/spring-cloud/spring-cloud-netflix/issues/466。
spring applicationContext 提供实用方法来查找带有某些注释的 bean。
解决方案可能包括启动 applicationContext 并在那里搜索重叠注释。
为此,您需要注册一个 ApplicationListener,它会搜索您所有的 @RequestMapping bean,这些 bean 会进一步用 @FeignClient 进行注释。
实现可能如下所示:
@Component
public class ContextStartupListener
implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
for(String beanName : applicationContext.getBeanNamesForAnnotation(RequestMapping.class)) {
if(applicationContext.findAnnotationOnBean(beanName, FeignClient.class)!=null){
throw new AnnotationConfigurationException("Cannot have both @RequestMapping and @FeignClient on "+beanName);
}
}
}
}