2

@FeignClient同じインターフェイスに@RequestMapping追加できませんでした。ここで、エラー メッセージを表示するために 2 つの注釈が同時に使用されたかどうかを確認したいと思います。

質問:

isAnnotatedBy(Annotation annotation)春にサポートされているメソッドのようなものはありますか? そうでない場合、どうすればここで目標を達成できますか?

ありがとう!

4

2 に答える 2

1

この問題https://github.com/spring-cloud/spring-cloud-netflix/issues/466に関連する問題が発生していると思われます。

spring applicationContext は、特定の注釈が存在する Bean を検索するためのユーティリティ メソッドを提供します。

解決策には、applicationContext の開始をブートストラップし、そこで重複する注釈を検索することが含まれる場合があります。

これを機能させるには、@FeignClient でさらにアノテーションが付けられたすべての @RequestMapping Bean を検索する ApplicationListener を登録する必要があります。

実装は次のようになります。

@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);
            }
        }
    }
}
于 2016-12-12T18:16:55.693 に答える
0

という名前のメソッドが既にサポートされてい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
于 2016-12-12T07:48:23.263 に答える