|
From: David H. <dh...@ob...> - 2004-07-23 16:01:57
|
Have=20just=20come=20across=20an=20interesting=20and=20quite=20subtle=20go= tcha=20that=20is=20probably=20worth=20sharing. =20 If=20a=20BeanPostProcessor=20attempts=20to=20locate=20collaborators=20by=20= type=20in=20the=20ListableBeanFactory=20,=20and=20follows=20the=20standard= =20method=20of=20preventing=20circular=20references=20using=20ControlFlow,= =20then=20it=20will=20fail=20to=20process=20dependencies=20of=20non-lazy=20= FactoryBeans. =20 I'll=20elaborate=20on=20that=20mouthful=20of=20a=20sentence=20with=20an=20= example.=20We=20use=20a=20BeanPostProcessor=20to=20auto-register=20JMX=20m= anageable=20beans,=20which=20does=20the=20following=20in=20postProcessAfte= rInitialization: *=09Checks=20that=20we=20are=20not=20in=20a=20circular=20dependency=20usin= g=20the=20ControlFlow=20method,=20as=20in=20AbstractAdvisorAutoProxyCreato= r.shouldSkip *=09If=20this=20test=20succeeds,=20it=20locates=20a=20number=20of=20collab= orating=20handlers=20by=20type=20that=20define=20different=20strategies=20= for=20registering=20MBeans=20(ie=20registering=20standard=20MBeans,=20or=20= POJOs=20with=20attributes).=20=20 *=09The=20found=20handlers=20are=20then=20applied=20to=20the=20bean=20bein= g=20processed =20 This=20seems=20to=20be=20a=20perfectly=20sane=20and=20valid=20series=20of=20= calls.=20In=20fact,=20the=20only=20real=20difference=20between=20this=20an= d=20DefaultAdvisorAutoProxyCreator=20is=20that=20collaborators=20are=20loc= ated=20by=20type=20and=20not=20by=20name,=20which=20is=20not=20unreasonabl= e.=20However,=20an=20issue=20occurs=20when=20a=20bean=20you=20expect=20to=20= be=20post-processed=20is=20itself=20a=20dependency=20of=20a=20FactoryBean=20= that=20is=20not=20lazily=20instantiated.=20All=20FactoryBeans=20are=20auto= matically=20instantiated=20when=20we=20locate=20beans=20by=20type,=20to=20= determine=20what=20type=20of=20object=20they=20return.=20However,=20this=20= in=20turn=20instantiates=20all=20of=20the=20FactoryBean's=20dependencies=20= -=20one=20of=20which=20is=20a=20bean=20we=20expect=20to=20be=20handled=20b= y=20our=20BeanPostProcessor,=20which=20does=20not=20process=20it=20since=20= we=20are=20already=20in=20a=20call=20stack=20protected=20by=20ControlFlow.= =20The=20result=20is=20that=20some=20beans=20are=20registered=20with=20our= =20JMX=20server,=20and=20some=20aren't=20-=20and=20the=20ones=20that=20are= n't=20are=20those=20that=20happen=20to=20be=20dependencies=20of=20non-lazy= =20FactoryBeans.=20 =20 All=20of=20the=20above=20is=20exactly=20the=20behaviour=20you=20would=20ex= pect=20if=20you=20were=20paying=20really=20close=20attention=20-=20except=20= I=20wasn't=20and=20it=20surprised=20the=20hell=20out=20of=20me.=20If=20the= =20TODO=20in=20AbstractAdvisorAutoProxyCreator=20is=20to=20be=20believed=20= (in=20the=201.0.2=20source),=20the=20ControlFlow=20check=20is=20likely=20t= o=20end=20up=20in=20a=20base=20class=20for=20broader=20re-use.=20Thus,=20i= s=20it=20worth=20explicitly=20documenting=20(assuming=20that=20it=20isn't=20= already)=20that=20ListableBeanFactory.getBeansOfType=20can=20cause=20probl= ems=20like=20this=20with=20FactoryBeans=20and=20should=20be=20used=20with=20= care? =20 Dave=20Hewitt ------------------ Systems=20Engineer OBJECTIVITY Tel:=20024=207642=200000 Fax:=20024=207642=200001 Email:=20d...@ob...=20<mailto:dh...@ob...>=20= www.objectivity.co.uk=20<http://www.objectivity.co.uk/>=20 =20 ________________________________________________________________________ This=20e-mail=20has=20been=20scanned=20for=20all=20viruses=20by=20Star=20I= nternet.=20The service=20is=20powered=20by=20MessageLabs.=20For=20more=20information=20on= =20a=20proactive anti-virus=20service=20working=20around=20the=20clock,=20around=20the=20gl= obe,=20visit: http://www.star.net.uk ________________________________________________________________________ |