简体   繁体   中英

CDI Interceptor vs CDI events

I have a question about the best implementation to have a system in java EE that is as uncoupled as possible, the question is whether it is better to use CDI events or CDI interceptors, obviously for things like security would be an interceptor, for logs for example any of the two according to the specific case, but for business rules in which a certain task must be done while another is executed.

Leaving aside that asynchronous events may or may not occur, my doubt is more in favor of the decoupling of modules, since for my concept the events are more decoupled than the interceptors, since in the interceptor the class that contains the function must be defined intercept, but in the case of events only the message must be defined.

Interceptors and Events are unrelated. They serve different purposes.

Interceptors are more an "Aspect Oriented Programming" style of programming. Events are, as you mentioned, a decoupling aspect.

Interceptors are of the type "the code doesn't know or care about what's happening", where as the code is actively involved in Events, since the code has to generate them. It's just that the code doesn't care what happens to them.

Both have elements of separation of concern.

It should also be noted the CDI events are not asynchronous. You can attach listeners that act asynchronously, but out of the box, they're not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM