2010-08-30 70 views

回答

1
if (number % 2 == 0) { // is even 

    anotherBean = (AnotherBean) applicationContext.getBean("anotherBean"); 

    // send even to another bean 
    anotherBean.send(number); 
} 

有關其他信息,請參閱here

+0

有什麼條件? – Bozho 2010-08-30 07:05:20

+1

這是一個笑話,因爲問題問「如何發送一個甚至另一個bean」。 – earldouglas 2010-08-30 15:56:23

1

如果要通知有關的東西豆,只需調用一個方法:

@Service 
public class Notifier { 
    @Autowired 
    private Notified notified; 

    public void something() { 
     notified.notify(..); 
    } 
} 

但是事件處理通常是異步的。在這種情況下,您必須創建一個新的Thread(或自Java 5以來使用executors framework),將引用傳遞給/注入目標bean,並讓它通知它。

如果您想要通知多個bean,而不知道其中的哪一個,那麼使用彈簧提供的event mechanism作爲觀察者模式的實現。

1

您可以使用Spring Integration在上下文中的bean之間進行消息傳遞。看看MessageChannel和ServiceActivator。您可以將消息路由,過濾或拆分,以滿足您的需要。

相關問題