-
1. Re: Circular dependencies of generic classes
cernicb Jul 5, 2011 1:23 PM (in response to cernicb)Hello again, maybe I found proper solution for my problem. Simple application I develop consists of three @RequestScoped beans, for country, municipality and town pages. They all extend one base generic class, BaseJsfBean. And each of them has some modules, such as action module for page actions, and data module for data access. These modules are java generics with @Dependent scope. So I created base class for all the modules as well, and in there I placed BaseJsfBean field. In base module class constructor I obtain current RequestScoped instance via getHostInstance method:
public static <T> T getHostInstance(Instance<T> source, InjectionPoint injectionPoint, Annotation... qualifiers) { T result = null; if (source != null && injectionPoint != null) { @SuppressWarnings("unchecked") Class<T> paramClass = (Class<T>) injectionPoint.getMember().getDeclaringClass(); Instance<T> instance = source.select(paramClass, qualifiers); if (instance != null) result = instance.get(); } return result; }
This way, all modules have reference to client proxy of my current @RequestScoped bean. And when dependent modules have to comunicate among themselves, they do it via @RequestScoped bean instance. Base module class constructor is
public BaseModule(Instance<BaseJsfBean<T, I>> jsfBeanSource, InjectionPoint injectionPoint) { this.jsfBean = getHostInstance(jsfBeanSource, injectionPoint); }
Whenever I add new module to module package by extending BaseModule class, I am forced to call this super constructor in order to satisfy compiler, and hence I cannot forget to obtain current @RequestScoped bean for my new module.
And this way, it's enough for @RequestScoped beans only to have constructor and module field declarations, for example:
@Inject public CountryJsfBean(GenericBean<Country, Long> genericBean, GenericDataModel<Country, Long> dataModel, ActionModule<Country, Long> actionMdl, FilterModule<Country, Long> filterMdl) { this.genericBean = genericBean; this.filterMdl = filterMdl; this.actionMdl = actionMdl; this.dataModel = dataModel; }
I just want to ask if all this is too complicated, and is there a simpler way to achieve what I showed here?