Re: Injecting one service into another 'ignored path' service
"Thiago H. de Paula Figueiredo" <[email protected]> Thu, 18 Apr 2019 19:00:55 -0300
| Newsgroups | gmane.comp.java.tapestry.user |
|---|---|
| Message-ID | <CAE_88GaKSo_LRp1RdU_AnfQAw35zNw=gyK6UOU2OysSMoyPegA@mail.gmail.com> |
On Mon, Apr 15, 2019 at 8:04 PM Christopher Dodunski < [email protected]> wrote: > By creating a ServletContextListener that sets a static ServletContext > variable in a RegistryProxy class I created, I now have access to the > Tapestry-IoC registry from anywhere in the application. Nice! > However, when I use my custom endpoint Configurator to get an instance of > ServerEndpoint (implementation) from the registry, it seems that what I > get back is not castable: > java.lang.ClassCastException: Cannot cast > $HarbourServerEndpoint_39c9cc24eb8b2a to > com.optomus.harbour.services.HarbourServerEndpointImpl > It's not castable nor it should. You shouldn't cast to the service implementation class. Just use the service interface instead. I'd try this: @Override public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { return RegistryProxy.getService(endpointClass.class); } Why is it even referencing HarbourServerEndpointImpl? This, the service implementation class, should only be referenced in AppModule (or other Tapestry-IoC module) when declaring the service and maybe also when making unit tests. Everything else should use HarbourServerEndpoint instead. My Configurator method: > > @Override > public <T> T getEndpointInstance(Class<T> endpointClass) throws > InstantiationException { > return > endpointClass.cast(RegistryProxy.getService(HarbourServerEndpoint.class)); > } > > My RegistryProxy method: > > public static <T> T getService(Class<T> serviceInterface){ > Registry registry = > (Registry)context.getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME); > return registry.getService(serviceInterface); > } > > Incidentally, altering my Configurator method to use old style casting (as > per below) didn't solve the problem. > > @Override > public <T> T getEndpointInstance(Class<T> endpointClass) throws > InstantiationException { > return (T)RegistryProxy.getService(HarbourServerEndpoint.class); > } > > Finally, with no casting at all, the compiler gives the error: > > Error:(17, 40) java: incompatible types: inference variable T has > incompatible bounds > equality constraints: > com.optomus.harbour.services.HarbourServerEndpoint > upper bounds: T,java.lang.Object > > Regards, > > Chris. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > -- Thiago