java - Replace factory-based object creation with CDI mechanism -



java - Replace factory-based object creation with CDI mechanism -

i wanted introduce cdi (weld) our project , having problem objects manually constructed.

so have classes implementing ireport interface, have field should injected. null @ runtime because of classes beingness generated reportfactory in class reportcontroller.

private map<string,object> generatereport(reportinfo ri, ...) { // input validation ireport study = reportcontrollerfactory.getreportinstance( ri.getclassname() ); // ... }

i aware can utilize @produces annotation custom annotation in reportcontrollerfactory, how utilize @inject variable can created, after validation done, inside method? , how submit parameter ri.getclassname()? object ri not known when reportcontroller constructed.

thank much!

kind regards, sebastian

edit @ jul 08, 2011 (10:00):

reportfactory class:

public static ireport getreportinstance( string classname ) throws reportexception { ireport study = null; seek { class<?> clazz = class.forname( classname ); study = (ireport) clazz.newinstance(); } grab ( exception e ) { … } homecoming report; }

edit 2 (selection of right study implementation)

the study instance selected paths go jsf frontend reportcontroller. managedbean calls session bean, has several methods, depending on button pressed where. methods set study name , phone call more generic method sendorgetreport. method selects unique key specified study database , decides whether send e-mail or immediately deliver report. let's assume should delivered.

then reportcontroller comes play. fetches reportinfo object based upon unique key , other info provided methods above , calls reportfactory create study of type ri.getclassname().

fancy, huh? think whole section might need refactoring. if don't see easy spot, skip @inject in study implementation , jdni lookup resource.

the thought behind cdi (and other di-frameworks) in order manage dependencies take on command of managed beans lifecycle. implies - among other things - cannot interfere creation of managed beans if want managed container.

certainly not mean scenario unsolvable, have alter perspective bit ;-)

the thought work managed beans (obviously), allow own logic decide instance of available instances correct.

... @inject instance<ireport> availablereports; ... @produces public ireport createreport() { ireport result; (ireport report: availablereports) { // take right instance, might want query injection // point or attached qualifier bit more in order // determine right instance if ... result = report; ... } homecoming result; } ...

with many beans of beantype ireport like

public class areport implements ireport { ... @inject ... } public class breport implements ireport { ... @inject ... }

and automagical usage this

public class mystuff { ... @inject ireport myreport; ... }

see here , here more information.

if did not misunderstand problem, should bring forwards - sense free post farther questions / comments.

update:

everything might dead simple if fits requirements:

@areport public class areport implements ireport { ... @inject ... } @breport public class breport implements ireport { ... @inject ... }

with usage

public class mystuff { ... @inject @areport ireport myareport; ... @inject @breport ireport mybreport; ... }

java factory factory-pattern cdi jboss-weld

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -