2013-08-22 82 views
0

我有幾個EJB(使用@Stateless註釋),當我調用它們時(即,當我的應用程序服務器引導時它們不是 急切加載)時加載。如何在加載時攔截EJB's

其中一些包含自定義方法註釋,我們可以將其稱爲@Foo。

當我的應用程序服務器(AS) 啓動時,我想找到一種方法來掃描所有這些文件,並找到哪些文件用@Foo註釋。

到目前爲止,我已經在web.xml中註冊了一個生命週期監聽器,它在AS啓動時被稱爲 。

  • PS#1:當我的EJB是第一個叫@PostConstruct被稱爲其 可能是對我的AP啓動後以後的時間。
  • PS#2:當我的EJB註冊在JNDI上時是否引發了一個事件?
  • PS#3:我的事春天有類似的功能,當你將其配置爲掃描所有類 包
+0

@Foo是CDI quailifer嗎? –

回答

0

下,如果EJB是由Spring構建的,那麼你可以使用一個Spring bean後處理器(此是Spring在執行掃描時使用的)。

@Component 
public class FooFinder implements BeanPostProcessor { 

    List<Method> fooMethods = new ArrayList<>(); 

    @Override 
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 
     Class<?> targetClass = AopUtils.getTargetClass(bean); 
     for (Method method : targetClass.getDeclaredMethods()){ 
      for (Annotation annotation : AnnotationUtils.getAnnotations(method)){ 
       if (annotation instanceof Foo){ 
        fooMethods.add(method); 
       } 
      } 
     } 
     return bean; 
    } 

    @Override 
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 
     return bean; 
    } 
} 

也被警告......注意依賴注入到你的BeanPostProcessor中。您可能會創建Spring依賴關係圖的問題,因爲必須首先創建BeanPostProcessor。如果您需要向其他bean註冊該方法,請創建BeanPostProcessor ApplicationContextAware或BeanFactoryAware,然後以這種方式獲取bean。