Header Ads

  • Breaking Now

    What is BeanFactory?

    In core packages of Spring org.springframework.beans.factory there is an interface named BeanFactory that can be implemented in various ways.It represents a generic factory that enables objects to be retrieved by name and which can manage relationships between objects.A BeanFactory contains different beans definitions which can be instantiated whenever a client wishes to do so , it is, hence, associated with life cycle of bean instances.

    Bean factories support two modes of object:
    -
    Singleton : a single,uniquely named, shared instance of an object which will be retrieved on lookup. It is the default, and most often used. It's ideal for stateless service objects.
    -
    Prototype or non-singleton: each retrieval results in an independent object creation.It could be used in stateful service objects scenarios where each caller has its own independent object.

    The most commonly used bean factory objects are:

    -XmlBeanFactory which parses a simple XML structure defining classes and properties of named objects.The DTD is provided to make authoring easier.
    -
    DefaultListableBeanFactory
    parses bean definitions in properties files, and create BeanFactories programmatically.

    1 comment:

    1. Simple and nice post!!!

      I would like to add two more points here:

      1) As stated in this post for singleton mode. that "which will be retrieved on lookup" , BeanFactory always loads singleton beans lazily when requested eg. by getBean(). But ApplicationContext by default loads singleton beans eagarly which has its own benefits.

      2) BeanFactory is not preferred way to create Spring container. ApplicationContext is always preferred over this as ApplicationContext provides enterprise level services.

      ReplyDelete

    Post Top Ad

    Post Bottom Ad