Announcement Announcement Module
No announcement yet.
Factory-Method. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Factory-Method.

    Hi all,

    I understand the scope of bean. I am not understanding the factory-method, could somebody help me out on the need of factory-method.

    Thanks in advance

  • #2
    The factory-method is provided as a way to achieve the Factory Method Pattern.


    • #3
      I am not seeing any difference between just having the bean defination and factory-method.
      <bean name="XYZ" class="customer"/> - here customer instance is created on using applicationContext

      When I use Factorymethod it will be same...
      <bean id="XYZ"
      factory-method="createInstance"/> - here one method I am specifing where the instance is getting created. I see more work here than the previous one.
      Where should I use factory-method it is not clear?


      • #4
        Hi c.madhu.2005,

        You would use a bean with a factory method if the creation of the object is complex. For example, you might need to do some business logic, use a service, or a Dao, etc. in order to instantiate and initialize your object. In that case you would need something more involved than simple instantiation and dependency injection.

        If you can get by having Spring create your beans then do so. Only use the factory method if you need to perform some logic that cannot be captured on a simple bean declaration.

        I hope it clarifies a bit.