java - How to make CDI decide the implementation to use according to generic type of the bean -
this is, briefly, architecture:
what want accomplish create cdi inject right implementation of bo within controller, , right implementation of dao within bo accourding generic type of controller.
how can accomplish that?
i solved problem using javax.enterprise.inject.instance object encapsulate bo's , dao's. way:
@inject private instance<crudbo<t>> bo; public crudbo<t> getbo() { homecoming bo.get(); }
just create things clear, crudbo interface genericcrudbo implements, so, knowing default genericcrudbo annotated @default, annotated specialized classes @alternative:
@alternative public class specializedcrudbo extends genericcrudbo<specificclass>{ }
i declared alternatives bo's , dao's in beans.xml:
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> <alternatives> <class>com.kichel.marcos.business.specializedcrudbo</class> ... </alternatives> </beans>
and cdi can handle generic java beans @ runtime, because dont have create tons of boilerplate classes.
java generics cdi
No comments:
Post a Comment