Announcement Announcement Module
Collapse
No announcement yet.
Difference between BeanFactory and Applicationcontext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Difference between BeanFactory and Applicationcontext

    Hi,
    Iam new to Spring.. Got one question.
    Apart from the other differnces i came to know that

    BeanFactory container instantiates Beans only when it beanfactory.getBeans().
    and
    ApplicationContext instantiates singleton beans when in the context is up and doesnt waits for applicationContext.getBeans()

    Now,My questions are
    1) how does it know that the bean belongs to BeanFactory or ApplicationContext.cos, we are not telling anything,anywhere in the configuration apart from the class where exactly the wiring happens ie., getBean()
    2) Where do we mention the bean to be singleton?
    3) @1 - Without mentioning nowhere abt ApplicationContext,how come the bean knows the app. context's singleton bean should be instantiated at the context start up?

    Please excuse if am on the wrong path?

    Thanks.

    Regards
    Senthil Kumar Sekar

  • #2
    In spring, all beans are singleton by default.
    If you don't want a singleton bean, you need to add the @Scope annotation in the bean class.

    In terms of the prototype scope, there is a difference between BeanFactory and AppliationContext. Refer to the reference guide for more details.

    Don't understand your first and third question. BeanFactory and AppliationContext can both used to instantiate beans. Which one to use and how to use is up to you.

    Originally posted by contactsenthil View Post
    Hi,
    Iam new to Spring.. Got one question.
    Apart from the other differnces i came to know that

    BeanFactory container instantiates Beans only when it beanfactory.getBeans().
    and
    ApplicationContext instantiates singleton beans when in the context is up and doesnt waits for applicationContext.getBeans()

    Now,My questions are
    1) how does it know that the bean belongs to BeanFactory or ApplicationContext.cos, we are not telling anything,anywhere in the configuration apart from the class where exactly the wiring happens ie., getBean()
    2) Where do we mention the bean to be singleton?
    3) @1 - Without mentioning nowhere abt ApplicationContext,how come the bean knows the app. context's singleton bean should be instantiated at the context start up?

    Please excuse if am on the wrong path?

    Thanks.

    Regards
    Senthil Kumar Sekar

    Comment


    • #3
      Thanks.Tat made sense.

      Reg. question 1 and 3 (Rephrased) -- The Bean is taken care by BeanFactory or ApplicationContext container.Is there any configuration anywhere which will tell that a particular bean has to be taken care by BeanFactory or ApplicationContext.

      Hope its not decided with the below mentioned code.
      Because,In case of ApplicationContext, the beans are instantiated when the context is up.So this wiring done in the below code happens wen the context is up????


      BeanFactory beanFactory = new XMLBeanFactory(new FileSystemResource("c:\Hello.xml"));

      [OR]

      ApplicationContext appContext = new ClassPathXMLApplicationContext("Hello.xml");


      Thanks.

      Regards
      Senthil Kumar Sekar

      Comment


      • #4
        To be short.
        Where exactly the bean is associated with either BeanFactory or ApplicationContext?

        Hope its not happening below

        BeanFactory beanFactory = new XMLBeanFactory(new FileSystemResource("c:\Hello.xml"));

        [OR]

        ApplicationContext appContext = new ClassPathXMLApplicationContext("Hello.xml");


        If it happens in the above code then, there then implementing BeanFactoryAware to the bean is not required.because,only after executing the above 2 lines,we will get the bean and we will invoke getBeanFactory().

        TRUE?

        Thanks.

        Regards
        Senthil Kumar Sekar

        Comment

        Working...
        X