2010-08-30 68 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。您可以将消息路由,过滤或拆分,以满足您的需要。

相关问题