|
From: Andy D. <an...@ma...> - 2004-03-25 17:50:15
|
=2D----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I posted a message to the Spring "help" forum, and it has become more of = a=20 development idea. I'd like to copy the two help forum messages here and=20 solicit feedback from Spring developers. You will find the two messages=20 below. Thanks for your consideration. =46irst message, posted 3/24/04: <<<<<<<<<<<< I have two questions regarding Spring's instantiation of beans that are not= =20 singletons. First, let's say I have the following XML: =20 ---- <beans> <bean id=3D"a" class=3D"A" singleton=3D"false"> <property name=3D"b"> <ref bean=3D"b"/> </property> </bean> =20 <bean id=3D"b" class=3D"B" singleton=3D"false"> <property name=3D"c"> <ref bean=3D"c"/> </property> </bean> =20 <bean id=3D"c" class=3D"C" singleton=3D"false"> <property name=3D"a"> <ref bean=3D"a"/> </property> </bean> </beans> ---- =20 What will Spring do when I attempt to get "a"? Will this even work? How ma= ny=20 times will each class be instantiated? The second question is similar to the first. Here is another config file: =20 ---- <beans> <bean id=3D"a" class=3D"A" singleton=3D"false"> <property name=3D"b"> <ref bean=3D"b"/> </property> =20 <property name=3D"c"> <ref bean=3D"c"/> </property> </bean> =20 <bean id=3D"b" class=3D"B" singleton=3D"false"> <property name=3D"c"> <ref bean=3D"c"/> </property> </bean> =20 <bean id=3D"c" class=3D"C" singleton=3D"false"> </bean> </beans> ---- When I attempt to get "a", how many times will "c" be instantiated? Once f= or=20 each reference, or once for the entire getBean call? <<<<<<<<<<<<<<<<<<<<< I then decided to perform my own tests based on the two examples above. He= re=20 is my response: <<<<<<<<<<<<<<<<<<<<< Well, I've tested it, and my fears are confirmed. Everytime a non-singleton= is=20 referenced in the XML, it is re-instantiated. I haven't looked at Spring's= =20 source code, but I'm guessing it is using getBean internally to get a bean= =20 referenced in the XML. This means that the first example never returns. It = is=20 either stuck in an infinite loop, or it is in infinite recursion, though=20 after a minute of running I didn't get a StackOverflow. The second example instantiates C twice, though it does return. What I need is the ability to have an object graph instantiated every time= =20 getBean is called, though in that instantiation, referenced objects should= =20 only be instantiated at most one time. Look at the second example. What we would like is the ability to have A, B= ,=20 and C instantiated every time getBean is called, but each class should be=20 instantiated at most once. In other words, the same C reference is passed t= o=20 both A and B, though all these objects will be reinstantiated the next time= =20 getBean is called. The behavior we are looking for would be exactly the sam= e=20 as if all the beans were singletons, but we used a new XmlBeanFactory for=20 every call of getBean. Of course, this is a possible solution to our proble= m,=20 but I'm guessing it would be terribly ineffecient. Right now there are two "types" of bean instantiation: singleton, and=20 non-singleton. Does anyone see value in creating a third type? This new typ= e=20 would indicate, "create a new instance of the object for each getBean(...)= =20 call, but never more than one instance, so that all references within the=20 scope of the getBean(...) call get the same instance on reference."=20 =2D----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFAYxvMdgQy3TUmt38RAtk+AJ4wC9epDPb5wnlBbnwsCmAuB5VVCgCffJ6t fme2cmOWLhe/YUtegfx02LU=3D =3DI6nb =2D----END PGP SIGNATURE----- |