How to avoid Spring configuring bean in Java config -
in modular spring configured application, use factory beans provide bean instances across module boundaries.
for example, 1 module a may expose bean instance name name. module b can consume bean via declaration of style
<bean id="namebean" class="com.zfabrik.springframework.componentfactorybean">     <property name="componentname"  value="a/name" />     <property name="classname"  value="a.ainterface" /> </bean> note modules have separated class loader hierarchies , actual implementation class of a/name may not visible in b. if in osgi (although not osgi).
my goal provide a/name in programmatic application context in b. when trying
@configuration public static class appcontext {   @bean ainterface namebean() {      return lookup("a/name",ainterface.class);   } } (lookup actual instance retrieval) see spring trying configure returned instance. example, attempt resolve @autowired properties of a/names's implementation class - not make sense in context of b (and deal of lookup provide configured anyway). even, if try
@configuration public static class appcontext {   @bean(autowire=autowire.no) ainterface namebean() {      return lookup("a/name",ainterface.class);   } } it go configuring returned instance.
how can provide bean application context without spring touching implementation instance?
edit: suggested sotirios delimanolis, returning factorybean afaict avoids spring configuration of returned instance.
the alternative code this:
@configuration public static class appcontext {   @bean factorybean<ainterface> namebean() {      return new componentfactorybean("a/name",ainterface.class);   } } it's not cool @untouchedbean annotation because of factorybean in return type, solves problem.
@sotirios: please suggest answer can tag suggestion accordingly.
/edit
ok, can closed. suggested , accepted answer return factory bean.
Comments
Post a Comment