I have maven module_A service rom where i need to call module_B service. Intention is to keep the coupling minimum
Approach1:- This is approach we are generally used to. module_B will be injected under module_A and the call service_B from service_A. But it tightly couples the module_B with module_A.
Approach2:- Spring events like this example. With this approach listener can be in module_A. For example :- I need to call module_A(EmployeeService.findEmployee(int id)) from module_B. What i can do is i will create
EmployeeFinderCustomEvent
which will containemployeeId
. Event will be published from module_B.
EmployeeFinderCustomEventListener
will lie under module_A and find the employee. But I need to return Employee
object from listener which
should be returned to module_B
calling service. But onApplicationEvent
return type is void..
@Override
public void onApplicationEvent(EmployeeFinderCustomEvent cse) {
// find employee
}
How can I return the object from spring event listener to caller to achieve loose coupling?