Powered by Blogger.

Friday, 6 June 2014

Spring Question

By Shambhu Kumar  |  10:45 No comments

Why spring is light weighted

  1. The main reason being Spring Framework light weight is because of its API distribution.
  2. It has so many Jar files in the package. They are designed in such a way that each module of Spring Framework is divided into an Jar API.
  3. So, whenever you design any application with Spring Framework, need to add only those APIs which you want to use from the framework.
  4. So, if you are using Spring's IOC feature then you will add Spring Core and Spring Bean Jar files, rest you don't need to include in your project.
  5. So, that way Spring Framework is light weight, because framework is very much decoupled.

Spring employs four key strategies: 

  1. Lightweight and minimally invasive development with plain old Java objects (POJOs) 
  2. Loose coupling through dependency injection and interface orientation
  3. Declarative programming through aspects and common conventions
  4. Boilerplate reduction through aspects and templates.

What are the advantages of Spring Framework?

Ans. The advantages of spring are as follows:
  1. Spring has layered architecture. So it is up to the programmer what to use and what to leave.
  2. Spring Enables POJO (Plain old Java object) Programming. POJO programming enables continuous integration and testability.
  3. Dependency Injection and Inversion of Control Simplifies JDBC (Java Database Connectivity), open source and no vendor lock-in.
      What are the types of Dependency Injection Spring supports?
           Ans. Following are the types of Dependency Injection Spring supports:
  1.      Setter Injection: Setter-based DI is realized by calling setter methods on the beans after invoking a no-argument constructor or no-argument static factory method to instantiate the bean.
  2.       Constructor Injection: Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.

Explain Bean-LifeCycle.

Ans: Spring framework is based on IOC so we call it as IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java object (POJO).

Following steps explain their life cycle inside container.

  1. Container will look the bean definition inside configuration file (e.g. bean.xml).
  2. using reflection container will create the object and if any property is defined inside the bean definition then it will also be set.
  3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
  4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
  5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization()      methods will be called before the properties for the Bean are set.
  6. If an init() method is specified for the bean or @PostConstruct annotation is used , it will be called.
  7. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
  8. If the Bean definition in the Configuration file contains a 'destroy-method' attribute or @PreDestroy annotation is used , then the corresponding method definition in the Bean class will be called.
What is IOC or inversion of control?
Answer: Inversion of control means invert the control of creating the object from our own using new operator to container or framework.
Now it’s the responsibility of container to create object as required.
We maintain one xml file where we configure our components, services, all the classes and their property.
We just need to mention which service is needed by which component and container will create the object for us.
This concept is known as dependency injection because all object dependency (resources) is injected into it by framework.

<bean id="messanger" class="com.shambhu.bean.HelloWorld">
<property name="msg" value="Hello World" />

In this example HelloWorld class contain getter and setter for msg and container will instantiate msg and set the value automatically when it is used.
This whole process is also called wiring in Spring and by using annotation 

Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.


Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates