Swiz Prototype Bean

Chris Scott just blogged about the new Swiz Prototype bean which was introduced in Swiz 0.0.5.

Instead of getting a specific (singleton) bean instance there is a factory behind which always returns a new instance of the bean you try to autowire. Damn this is really a great feature which I also needed in my current project. I solved it differently until now but I will refactor to the Prototype approach which makes my code a lot cleaner since Swiz has to worry about the bean creation and not me!

Let’s assume every SomeView.mxml needs a single instance of SomeController. The code in the BeanLoader would look like this:

<?xml version="1.0" encoding="utf-8"?>
<BeanLoader xmlns="org.swizframework.util.*">
 
    <factory:Prototype xmlns:factory="org.swizframework.factory.*"
          id="someController"
          className="foo.controllers.SomeController"/>
 
</BeanLoader>

And in SomeView.mxml you autowire:

[Autowire(bean="someController")]
public var someController:SomeController;

Now every instance of SomeView get’s it’s own instance of SomeController. Ohhsumm Chris!

1 comment to Swiz Prototype Bean

  • Rajashekhar

    In which case we need specific instance for each controller in each view. And if we create an object inside a class that object will be instantiated automatically when we create an object to that class. Why we need this Auto wire?. I am new to this concept please explain about this clearly.