Hi, I've noticed a behavior that seems counter-intuitive to me and am wondering if I have made a bad assumption and if this is the way it is supposed to work.
I believe that if I use factory-method, Spring AS should call the method specified and not the constructor of the class. This seems to be true unless the constructor takes an argument; if it does, then Spring AS calls the constructor, then the method specified by factory-method.
Consider this example. I have the following object in my application context XML:
If MySingleton is this:Code:<object id="mySingleton" class="single.MySingleton" factory-method="getInstance"> </object>
Code:package single { public class MySingleton { private static var _instance : MySingleton; public function MySingleton() { trace('Constructor'); } public static function getInstance() : MySingleton { trace('getInstance()'); if (_instance == null) { _instance = new MySingleton(); } return _instance; } } }
Then it works as I expected; Spring AS calls my getInstance method, which internally calls my constructor. In the debugger I see:
If, however, MySingleton is this:Fri Oct 23 17:39:17 GMT-0400 2009 INFO - org.springextensions.actionscript.ioc.factory.supp ort.DefaultListableObjectFactory - Pre-instantiating singletons in [object FlexXMLApplicationContext]
getInstance()
Constructor
I see this:Code:package single { public class MySingleton { private static var _instance : MySingleton; public function MySingleton( value : String ) { trace('Constructor: ' + value); } public static function getInstance() : MySingleton { trace('getInstance()'); if (_instance == null) { _instance = new MySingleton("foo"); } return _instance; } } }
Obviously if the value passed to the constructor is used in a nontrivial way, bad things can happen (null reference or other runtime error).Fri Oct 23 17:38:33 GMT-0400 2009 INFO - org.springextensions.actionscript.ioc.factory.supp ort.DefaultListableObjectFactory - Pre-instantiating singletons in [object FlexXMLApplicationContext]
Constructor: null
getInstance()
Constructor: foo
Can someone please help me understand what's happening here? Thanks.


Reply With Quote