Description
So far we've followed the JPA spec in Cayenne classic callbacks/listeners implementation. I am using listeners extensively and till now I used to install them in the code. Now that Modeler supports listeners, I discovered that the default mechanism of listener instantiation (Class.forName("listenerClass").newInstance()) is inadequate when a listener has dependencies on its environment. I guess in the J2EE world the dependencies are obtained via JNDI, but I wanted to use injection as a cleaner approach (and to avoid setting up JNDI).
So... I wrote an extension point that would allow users to register a factory object for creating the listeners:
public interface EntityListenerFactory {
<T> T createListener(Class<T> listenerClass, ObjEntity entity);
}
This would support dependency injection (or any other environment specific customization) of the listener objects, as well as using interfaces for the listeners - something JPA does not support. So the same listener configuration may be used with different implementations depending on some app logic.