Tapestry Inversion of Control ContainerPage edited by Howard M. Lewis ShipChanges (4)
Full ContentTapestry Inversion of Control ContainerWhy do I need to define an interface for my services? Why can't I just use the class itself?First of all: you can do exactly this, but you lose some of the functionality that Tapestry's IoC container provides. The reason for the split is so that Tapestry can provide functionality for your service around the core service implementation. It does this by creating proxies: Java classes One of the primary purposes for proxies is to encapsulate the service's lifecycle: most services are singletons that are created just in time. Just in time means only as soon If you bind a service class (not a service interface and class), then the service is fully instantiated the first time it is injected, rather than at that first method invocation. Further, you The final reason for the service interface / implementation split is to nudge you towards always coding to an interface, which has manifest benefits for code structure, robustness, and testability. My service starts a thread; how do I know when the application is shutting down, to stop that thread?This same concern applies to any long-lived resource (a thread, a database connection, a JMS queue connection) that a service may public MyService buildMyService(ServiceResources resources, RegistryShutdownHub shutdownHub) { final MyServiceImpl service = resources.autobuild(MyServiceImpl.class); shutdownHub.addRegistryShutdownListener(new RegistryShutdownListener() { public void registryDidShutdown() { service.shutdown(); } }); return service; } This code uses the ServiceResources object to build an instance of MyServiceImpl, with all dependencies injected. A valid alternative to this would be to have MyServiceImpl implement RegistryShutdownListener: public MyService buildMyService(ServiceResources resources, RegistryShutdownHub shutdownHub) { final MyServiceImpl service = resources.autobuild(MyServiceImpl.class); shutdownHub.addRegistryShutdownListener(service); return service; }
Change Notification Preferences
View Online
|
View Changes
|
- [CONF] Apache Tapestry > Tapestry Inversion of Control Conta... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence
- [CONF] Apache Tapestry > Tapestry Inversion of Control ... confluence