2016-02-13 53 views
0

我實現了一個CDI豆從另一豆觀察事件:爲什麼在faces-config.xml中的CDI託管Bean沒有註冊爲obersver?

@SessionScoped 
public class FixedItemController implements Serializable { 
    .... 
    public void onWorkflowEvent(@Observes WorkflowEvent workflowEvent) throws AccessDeniedException { 
     logger.info("evaluate event..."); 
     .... 
    } 
    .... 
} 

這工作得很好,只要我使用JSF頁面豆,其默認名稱「fixedItemController」。

但如果我聲明bean的另一個實例在faces-config.xml中像這樣:

<managed-bean> 
     <managed-bean-name>myOrderItemController</managed-bean-name> 
     <managed-bean-class>org.imixs.marty.workflow.FixedItemController</managed-bean-class> 
     <managed-bean-scope>session</managed-bean-scope> 
     <managed-property> 
      <property-name>childItemProperty</property-name> 
      <property-class>java.lang.String</property-class> 
      <value>_orderItems</value> 
     </managed-property> 
    </managed-bean> 

二審(myOrderItemController)不會自動爲我WorkflowEvent觀察員登記。

我該怎麼做,以確保我的第二個實例(由faces-config.xml聲明)將立即實例化並註冊爲我的workitemEvent的觀察者?

回答

1

faces-config.xml不註冊CDI託管的bean。它註冊JSF管理的bean。實際上,您的#{myOrderItemController}是JSF管理的bean。就好像你使用@ManagedBean而不是@Named。 JSF bean管理工具不會掃描CDI特定的@Observes註釋。

將其保留爲CDI管理的bean。無論您嘗試解決哪些問題,您認爲在faces-config.xml中註冊它是正確的解決方案,必須使用CDI API而不是JSF API來解決。

+0

感謝 - 所以你的意思是,我必須使用beans.xml描述符而不是faces-config.xml? – Ralph

+0

取決於具體的功能要求。通常,您只使用註釋/限定符/生產者,並將beans.xml保留爲原來的內容。 – BalusC