fastcodejava
fastcodejava

Reputation: 41097

How to send an event to another bean in spring?

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

Upvotes: 1

Views: 1752

Answers (2)

Adam B
Adam B

Reputation: 1774

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.

Upvotes: 1

Bozho
Bozho

Reputation: 597134

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.

Upvotes: 1

Related Questions