Results 1 to 2 of 2

Thread: [Autowired] property in constructor

  1. #1
    Join Date
    Feb 2011
    Location
    Milan
    Posts
    7

    Default [Autowired] property in constructor

    Hi again
    I'm going forward in the study of SAS, and I'm trying to use the metadata [Autowired] to inject the properties into a class.
    In the test I did, I saw that the property to be injected is null when I'm in the class constructor.

    my question is: can I get properties with [Autowired] and use them immediately in the constructor? or [Autowired] happens next?

    Code:
    [Command(eventType="startupEvent")]
    	public class StartupCommand extends GenericOperationCommand {
    		[Autowired]
    		public var appModel : ApplicationModel;
    
    		public function StartupCommand() {
    			super(LoadURLOperation, appModel.url);
    		}
    My intention is to retrieve the url to load from appModel, but in constructor this property is not available.
    I know that i can use a <constructor-arg > tag in application-context.xml, but [Autowired] solution seems to be so elegant

    When the execute method is performed, however, the property is accessible.
    For now the solution I adopted is this

    Code:
    public function StartupCommand(appModel:ApplicationModel) {
    			super(LoadURLOperation, appModel.url);
    		}

    Thanks, and sorry if I'm full of questions, but this framework inspires me a lot and I would like to start using it professionally, but without these tests i cannot tell my boss "I'm sorry but I'm not inside at the time of delivery because I have developed with a framework that I do not know "

    Marco

  2. #2
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    406

    Default chicken and egg problem :)

    Hey Marco,

    you need to understand how SpringAS injects properties into an object instance, its very easy because its exactly the way you would create an object and assign properties in actionscript.

    What SAS does is basically this:

    var object:StartupCommand = new StartupCommand();
    object.appModel = new ApplicationModel();

    Do you see now that its completely logical that the appModel property isn't set yet when you try to access it in the constructor?

    Hope that clears things up a little for you And don't apologize for asking questions, we're happy to help

    cheers,

    Roland

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •