java - How to override injection by another instance -


i have class cannot change:

class somebean {    @inject private dep1 dep1;    @inject private dep2 dep2;    ...    @inject private depn depn; } 

i have class:

class mybean {    @inject@named("bean1") private somebean bean1;    @inject@named("bean2") private somebean bean2; } 

how make module configuration bean1 , bean2 injected different instances have different dep2 instances, other dependencies same?

if using spring, create bean in context file such as:

   <bean class="mybean">      <property name="bean1">        <bean class="somebean">          <property name="dep2" ref="dep2instancea"/>        </bean>      </property>      <property name="bean2">        <bean class="somebean">          <property name="dep2" ref="dep2instanceb"/>        </bean>      </property>    </bean> 

so explicitly override 1 property, other autowired.

you need use privatemodules read how build 2 similar different trees of objects?.

some pseudocode give feeling:

class bean1module extends privatemodule    bind(dep2.class).to(dep2a.class)    bind(somebean.class)    expose(somebean.class).annotatedwith("bean1")  class bean2module extends privatemodule    bind(dep2.class).to(dep2b.class)    bind(somebean.class)    expose(somebean.class).annotatedwith("bean2") 

it might bit more lines of code spring xml lines typesafe, code complete works without plugins, can put logic in module, etc. ;)


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -