开发者

How to send an event to another bean in spring?

In sp开发者_开发技巧ring it is possible to do this. Does anybody have code samples?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜