Announcement Announcement Module
Collapse
No announcement yet.
Factory-method and constructors with arguments Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Factory-method and constructors with arguments

    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:

    Code:
    	<object id="mySingleton" class="single.MySingleton" factory-method="getInstance">
    	</object>
    If MySingleton is this:
    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:
    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
    If, however, MySingleton is 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;
    		}
    	}
    }
    I see this:
    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
    Obviously if the value passed to the constructor is used in a nontrivial way, bad things can happen (null reference or other runtime error).

    Can someone please help me understand what's happening here? Thanks.

  • #2
    as3commons-reflect side effect

    this is very probably related to this:

    http://www.springactionscript.org/faq.html#trace-errors

    Its a workaround for a flashplayer bug in the reflection API, it needs to call the constructor without constructor arguments once, to be able to retrieve the information about the constructor arguments.
    This is a very unfortunate side-effect that right now we can't do anything about.

    cheers,

    Roland

    Comment


    • #3
      Do we need to call the constructor at all?

      Alright, I understand the underlying problem with Flash that requires the constructor to be called in order to figure out its arguments.

      However, what I don't understand now is why should the framework want to even do that when there is a factory-method specified. Is it to support autowiring of the constructor arguments?

      Comment

      Working...
      X