Just finished a very long debug session. Please help me understand what root-cause/bad-practice caused this bug:
Beans A,B both had a property with an inner bean that looked exactly the same. Happily, I created bean C and reused it by doing <bean ref="C">.
However, bean C had a method called "setX" and a method called "getLongComputationBasedOnX". Turns out that upon first call to this method, it lazily cached with a member variable the computation. Further calls to setX do not reset the cache.
So, after I tried to make the world a better place and reuse, the 2 uses of C in A and B each set a different value to X, but now it was a single instance and the cache gave the value appropriate to A when called in B… 🙁
Is this a spring issue? Should I use parent instead of ref? Or is it a bad design of my “setX” and it should reset the cache?
Just use
prototypescope:This way you have a single
Cdeclaration but two instances, one created when injecting toAand another one forB.In general it is hard to tell what the right design should be. Based on your explanation it seems like
Cwas supposed to be used with only onexvalue. It should have been passed via constructor and madefinalto make this explicit though.Otherwise you will get nasty concurrent errors: one thread calls
setXandgetLongComputationBasedOnXsubsequently but in the meantime other thread calledsetX. That being said I believe myprototypesolution is the right one.