java - Get dependecy instance from instance with package and name -


let´s have instance a, have dependencies instance b , c. b , c has dependencies b1 , c1

it possible using reflection , package string "b.b1" , "c.c1" instances a.

it

          b1 b1 = a.getmagicallytheinstance("b.b1");           c1 c1 = a.getmagicallytheinstance("c.c1"); 

you can achieve using java reflection or ognl. ognl object graph notation language , notation want use. if want use reflection must follow these steps.

  1. get fields of a
  2. extract b a's fields , object.
  3. get fields of b
  4. extract b1 b's fields , object.
  5. repeat 2-4 c , c1.

by way getresourceasstreamis reading class path resources inputstream. has noting reflection far know.

but in ognl lot simpler.

  1. build context existing object. instance of a
  2. execute graph notation using context. b1 can use "a.b.b1"

and thats all.

update

i realize spring tag. can achieve using spring el easily. here example:

    expressionparser parser = new spelexpressionparser();     expression exp = parser.parseexpression("b.b1");     // assume has field name b, , b has field name b1     b1 b1 = (b1) exp.getvalue(ainsantece); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -