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.
- get fields of a
- extract b a's fields , object.
- get fields of b
- extract b1 b's fields , object.
- repeat 2-4 c , c1.
by way getresourceasstream
is reading class path resources inputstream. has noting reflection far know.
but in ognl lot simpler.
- build context existing object. instance of
a
- 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
Post a Comment