2016-12-12 4 views
2

@FeignClientおよび@RequestMappingを同じインターフェイスに追加できませんでした。次に、2つの注釈が同時に使用されてエラーメッセージが表示されるかどうかを確認したいと思います。春の起動時に特定の注釈でコントローラに注釈を付ける方法を教えてください。

質問:

春にサポートisAnnotatedBy(Annotation annotation)方法のようなものがありますか?そうでなければ、私はここでどのように目標を達成することができますか?

ありがとうございます!

答えて

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); 
      } 
     } 
    } 
} 
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 
関連する問題