views:

69

answers:

3

In spring it is possible to do this. Does anybody have code samples?

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

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

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

For additional information, see here.

James Earl Douglas
what's the conditional for?
Bozho
It was a joke, as the question asked "how to send an even to another bean".
James Earl Douglas
+1  A: 

If you want to notify a bean about something, simply call a method:

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

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

But event handling is usually asynchronous. In that case you will have to create a new Thread (or use the executors framework since Java 5), pass a reference to / inject the target bean, and let it notify it.

And if instead you want to notify multiple beans, without knowing which exactly, then use the event mechanism that spring provides as an implementation of the observer pattern.

Bozho
+1  A: 

You can use Spring Integration for messaging between beans in your context. Look at MessageChannel and ServiceActivator. You can route, filter, split messages to your beans how ever you need.

Adam B