Sunday, October 6, 2013

Spring Interview Questions - One


Q1: What is IOC or inversion of control? 

Ans: As the name implies Inversion of control means now we have inverted 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. 

Example: 
<bean id="createNewBook" class="springexample.bookMarket.CreateNewBookStore"> 
<property name="newBook"/> 
</bean> 

In this example CreateNewBookStore class contain getter and setter for newBook and container will instantiate newBook and set the value automatically when it is used. This whole process is also called wiring in Spring and by using annotation it can be done automatically by Spring, refereed as auto-wiring of bean in Spring. 

Q2: 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, 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, then the corresponding method definition in the Bean class will be called. 

Q3: what is Bean Factory, have you used XMLBeanFactory? 

Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code. XmlBeanFactory is one of the implementation of bean Factory which we have used in our project. org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our xml file. 

BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml")); 

Or 

ClassPathResource resorce = new ClassPathResource("beans.xml"); 
XmlBeanFactory factory = new XmlBeanFactory(resorce); 

Q4: What are the difference between BeanFactory and ApplicationContext in spring? 

Ans: ApplicationContext is preferred way of using spring because of functionality provided by it. 

In ApplicationContext:-
  • Here we can have more than one config files possible 
  • Application contexts can publish events to beans that are registered as listeners 
  • Support internationalization (I18N) messages 
  • Support application life-cycle events, and validation. 
  • Support many enterprise services such JNDI access, EJB integration, remoting 
BeanFactory 
  • In this only one config file or .xml file 
  • Doesn’t support. 
  • It’s not 
  • Doesn’t support. 
  • Doesn’t support. 
Q5: What are different modules in spring? 

Ans: spring have seven core modules 

1. The Core container module 
2. Application context module 
3. AOP module (Aspect Oriented Programming) 
4. JDBC abstraction and DAO module 
5. O/R mapping integration module (Object/Relational) 
6. Web module 
7. MVC framework module 

Q6: What is difference between singleton and prototype bean? 

Ans: This is another popular spring interview questions and an important concept to understand. Basically a bean has scopes which defines their existence on the application 

Singleton: means single bean definition to a single object instance per Spring IOC container.

Prototype: means a single bean definition to any number of object instances. 

Whatever beans we defined in spring framework are singleton beans. There is an attribute in bean tag named ‘singleton’ if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true. So, all the beans in spring framework are by default singleton beans. 

<bean id="createNewBook" class="springexample.bookMarket.CreateNewBookStore" singleton=”false”> 
<property name="newBook"/> 
</bean> 

Q7: What type of transaction Management Spring support? 

Ans: Transaction management is critical in any applications that will interact with the database. The application has to ensure that the data is consistent and the integrity of the data is maintained. Two type of transaction management is supported by spring 

1. Programmatic transaction management 
2. Declarative transaction management. 

Q8: What is AOP? 

Ans: The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. AOP is a programming technique that allows developer to modularize crosscutting concerns, that cuts across the typical divisions of responsibility, such as logging and transaction management. Spring AOP, aspects are implemented using regular classes or regular classes annotated with the@Aspect annotation 

Q9: Explain Advice? 

Ans: It’s an implementation of aspect; advice is inserted into an application at join points. Different types of advice include “around,” “before” and “after” advice 

No comments :

Post a Comment

iVTech Pageviews