Spring Questions

  • What is Spring Beans??

Java Objects which form the backbone of Spring application and they are assembled, instantiated, and managed by Spring IoC container.

These beans are created with configuration metadata that is supplied to the container, for instance, in the XML <bean/> form definitions.


  • How to provide configuration metadata to the Spring Container?

3 important methods to provide configuration metadata to the Spring Container :

    • Annotation-based configuration
    • Java-based configuration
    • An XML based configuration file

  • Explain the different types of bean scopes supported by Spring?

There are 5 different scopes provided by the Spring Framework:
    • Prototype scope, a single bean definition has any number of object instances.
    • Singleton scope, Spring scopes the bean definition to a single instance per Spring IoC container.
    • Request scope, a bean is defined to an HTTP request. This scope is valid only in a web-aware -Spring ApplicationContext.
    • Session scope, a bean definition is scoped to an HTTP session. This scope is also valid only in a -web-aware Spring ApplicationContext.
    • Global-session scope, a bean definition is scoped to a global HTTP session. This is also a case used in a web-aware Spring ApplicationContext.

  • Is Singleton class thread safe in Spring?
    NO


  • Explain Bean lifecycle in Spring framework

The spring container finds the beans definition from XML file, and instantiates the bean and also Spring populates all the properties specified in the bean definition (DI).

    • If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method.
    • If Bean implements BeanFactoryAware interface, spring passes the bean-factory to setBeanFactory() method.
    • If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesser beforeInitialization() method.
    • If the bean implements IntializingBean, its afterPropertySet() method is called.
    •  If the bean has init method declaration, the specified initialization method is called.
    • If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
    • If the bean implements DisposableBean, it will call the destroy() method.


No comments:

Post a Comment