java ee - How to Inject no interface, external library classes into JavaEE application? -


i new javaee , concept of dependency injection. have fair understanding of though not know ways can used.

i have local interface below:

@local public interface myinterfacelocal {     sometype getmesometype(); } 

the class implements interface stateless ejb.

@stateless public class myinterfaceimpl {     public sometype getmesometype() {         //some implementation details...         externallibraryclass externallib = new externallibrary(arg1, arg2);         return externallib.externallibmethod();     } } 

now problem is, how can avoid instantiating externallib , let injected in someway? example if ejb created interface, let ejb container handle instantiation, @ejb annotation below.

@stateless public class myinterfaceimpl {     @ejb     anotherinterface anotherinterfaceimpl;      public someothertype getmesometype() {         //some implementation details...          return anotherinterfaceimpl.somemethod();     } } 

i want able (something like) external library using because, allows me to:

  1. change underlying external library being used minimal change code-base. may change better 1 if need arises.
  2. easily inject mock when want unit test myinterfaceimpl class.

i have far looked at-

  1. creating wrapper method parameter externallibrary , can perform kind of manual method parameter injection. still makes implementation tightly coupled underlying library. (or not doing right)

  2. using context & dependency injection container injection (like how ejb container does. aware not same). researched ability of using producers. although understand producers respect cdi, not able wrap head around how can make use of this? or if on right path?

update: found few articles helped me understand cdi producers better , tried going approach faced problem. have:

externallibraryproducer.java

public class externallibraryproducer {     @produces     private externallibraryclass1 extrnallibraryclassproducer() {         return new externallibraryclass1("somestring", 7);         //the constructor takes string , commplex type          //as parameters. keeping little simple here.         //i trying set externallibraryclass1() arguments          //programmatically @ runtime.     } } 

now constructor of object want produce, takes in parameters, lets string , integer. thought create qualifier pass in these parameters produce object want.

externallibraryclass1qualifier.java

@qualifier @retention(runtime) @target({method}) public @interface externallibraryclass1qualifier {     string argument1();     int argyment2(); //this complex type. keeping      //simple here. } 

now want is, want argument values set programmatically @ runtime (assume, properties file). , not able figure out how this. final injection below.

@stateless public class myinterfaceimpl {     @inject     @externallibraryclass1qualifier(argument1 = "something", argument2 = 7)     externallibrary externallib;      public sometype getmesometype() {         //some implementation details...         return externallib.externallibmethod();     } } 

thanks guidance.

cdi producers way go here, question is, how want build them.

since said arguments come property file, suggest invert approach , let producer method check property file , extract values (or ask "reader" did before , caches it):

@produces private externallibraryclass1 produceexternallibraryinstance() {     // read properties file or cache use     string arg1 = propertyreader.getarg1();     integer arg2 = propertyreader.getarg2();     // create object args     return new externallibraryclass1(arg1, arg2); } 

with need no qualifier , can @inject externallibraryclass1.

just note producer invoked based on when create object injects externallibraryclass1 - make sure can grab args property file soon. (should't trouble when produces reading, might trickier if have cache in place)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -