Tuesday, November 5, 2013

Spring Related Question and Answer

1.  What is Spring?
----------------------
Spring is an open source development framework for enterprise Java. The core features of the Spring Framework can be used in developing any Java application,but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based programming model.

2. what are benefits of using spring?
----------------------------------------------
Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.

Inversion of control (IOC): Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.

Aspect oriented (AOP): Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.

Container: Spring contains and manages the life cycle and configuration of application objects.

MVC Framework: Spring's web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks.

Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for  example) and scale up to global transactions (using JTA, for example).

Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.

3. What are the different modules in Spring framework?
--------------------------------------------------------------------
1. Core Container Module : This module is provides the fundamental functionality of the spring framework. In this module BeanFactory is the heart of any spring-based application. The entire framework was built on the top of this module. This module makes the Spring container

2.Application Context Module : The Application context module makes spring a framework. This module extends the concept of BeanFactory, providing support for  internationalization (I18N) messages, application lifecycle events, and validation. This module also supplies many enterprise
services such JNDI access, EJB integration, remoting, and scheduling. It also provides support to other framework.

3.AOP module (Aspect Oriented Programming) : AOP module is used for developing aspects for Spring-enabled application

4.JDBC abstraction and DAO module

5.ORm integration module (Object/Relational)

6.Web module

7. MVC framework module

4. What is a BeanFactory?
---------------------------------
A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.

5. What is XMLBeanFactory?
--------------------------------------
BeanFactory has many implementations in Spring. But one of the most useful one is org. springframework. beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file.

6.What are important ApplicationContext implementations in spring framework?
--------------------------------------------------------------------------------------------------
ClassPathXmlApplicationContext – This context loads a context definition from an XML file located in theclass path, treating context definition files as class path resources.

FileSystemXmlApplicationContext – This context loads a context definition from an XML file in the filesystem.

XmlWebApplicationContext – This context loads the context definitions from an XML file contained within a web application.

7.  Explain Bean lifecycle in Spring framework?
-----------------------------------------------------------
1.The spring container finds the bean’s definition from the XML file and instantiates the bean.

2. Using the dependency injection, spring populates all of the properties as specified in the bean definition.

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 aninstance of itself.

5. If there are any BeanPostProcessors associated with the bean, their postProcessBeforeInitialization() methods will be called.

7 .If an init-method is specified for the bean, it will be called.

8 Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

7. What is bean wiring?
------------------------------
Combining together beans within the Spring container is known as bean wiring or wiring.

8. What are singleton beans and how can you create prototype beans?
-----------------------------------------------------------------------------------
Beans 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.

9. What are the different types of bean injections?
---------------------------------------------------------------
There are two types of bean injections.

    By setter
    By constructor
   
10. What is Auto wiring?
------------------------------
we can wire the beans as you wish. But spring framework also does this work for us. It can auto wire the related beans together. All we have to do is
just set the autowire attribute of bean tag to an autowire type.

<bean id="bar" class="com.act.Foo" Autowire=”autowire type”/>

11. What are different types of Autowire types?
---------------------------------------------------------
There are four different types by which auto-wiring can be done.

        byName
        byType
        constructor
        autodetect

12. What are the different types of events related to Listeners?
--------------------------------------------------------------------------   
There are a lot of events related to ApplicationContext of spring framework. All the events are subclasses oforg.springframework.context.
Application-Event. They are

    ContextClosedEvent – This is fired when the context is closed.
    ContextRefreshedEvent – This is fired when the context is initialized or refreshed.
    RequestHandledEvent – This is fired when the web context handles any request.   

13. What is an Aspect?
----------------------------
An aspect is the cross-cutting functionality that we are implementing. It is the aspect of our application we are modularizing. An example of an aspect is logging. Logging is something that is required throughout an application. However, because applications tend to be broken down into layers based on functionality, reusing a logging module through inheritance does not make sense. However, we can create a logging aspect and apply it throughout the application using AOP.   

14. What is a Jointpoint?
------------------------------
A join point is a point in the execution of the application where an aspect can be plugged in.

15. What is an Advice?
---------------------------
Types of advice:

 Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.

After throwing advice: Advice to be executed if a method exits by throwing an exception.

After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom  behaviour before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method   execution by returning its own return value or throwing an exception.


16. What is a Point cut?
----------------------------
A point cut  defines at what join points an advice should be applied.

17. What is a Target?
--------------------------
A target is the class that is being advised.

18. What is a Proxy?
-------------------------
A proxy is an object that is created after applying advice to a target object.

19. What is meant by Weaving?
------------------------------------
The process of applying aspects to a target object to create a new proxy object is called as Weaving.
The aspects are woven into the target object at the specified join points.

20 What are the different types of AutoProxying?
-----------------------------------------------------------
BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator
Metadata autoproxying

Different points where weaving can be applied :
    Compile Time
    Classload Time
    Runtime
   
21. What are the benefits of IOC?
----------------------------------------
The main benefits of IOC or dependency injection are:

1. It minimizes the amount of code in your application.

2. It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.

3. Loose coupling is promoted with minimal effort and least intrusive mechanism.

4. IOC containers support eager instantiation and lazy loading of services.



22. What bean scopes does Spring support? Explain them.
--------------------------------------------------------
Singleton: Scopes a single bean definition to a single object instance per Spring IoC container. Default Scope.

prototype : Scopes a single bean definition to any number of object instances.

request : Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a  bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session : Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

global session : Scopes a single bean definition to the lifecycle of a global HTTP Session.Typically only valid when used in a portlet context. Only valid in  the context of a web-aware Spring ApplicationContext.

23 : How do you turn on annotation wiring?
---------------------------------------------------
A: Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file by configuring <context:annotation-config/>.

24 What is WebApplicationContext ?
-----------------------------------------------
A: The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes, and that it knows which servlet it is associated with

25. Following are some of the advantages of Spring MVC over Struts MVC:
------------------------------------------------------------------------------------------------
Layered architecture that allows you to use what you need while leaving what you don’t need.
   
Spring's MVC is very versatile and flexible based on interfaces but Struts forces Actions and Form object into concrete inheritance.

Spring provides both interceptors and controllers, thus helps to factor out common behavior to the handling of many requests.

Spring can be configured with different view technologies like Freemarker, JSP, Tiles, Velocity, XLST etc. and also you can create your own custom view mechanism by implementing Spring View interface.

 In Spring MVC Controllers can be configured using DI (IOC) that makes its testing and integration easy.

 Web tier of Spring MVC is easy to test than Struts web tier, because of the avoidance of forced concrete inheritance and explicit dependence of controllers on the dispatcher servlet.

 Struts force your Controllers to extend a Struts class but Spring doesn't, there are many convenience Controller implementations that you can choose to extend.

 In Struts, Actions are coupled to the view by defining ActionForwards within a ActionMapping or globally. SpringMVC has HandlerMapping interface to support this functionality.

 With Struts, validation is usually performed (implemented) in the validate method of an ActionForm. In SpringMVC, validators are business objects that are NOT dependent on the Servlet API which makes these validators to be reused in your business logic before persisting a domain object to a database.

26.  What type of transaction Management Spring support?
--------------------------------------------------------------------------

Two type of transaction management is supported by spring

1. Programmatic transaction management :  used preferably when you have a small number of transactional operations

2. Declarative transaction management: In case of large number of transactional operations it is better to use declarative transaction management.


27. List new features for Spring 3.0
------------------------------------------
1: Spring Expression Language : Spring introduces an expression language which is similar to Unified EL

2. IoC enhancements/Java based bean meta-data : Annotation based configuration and Some core features from the JavaConfig project have been added

3. General-purpose type conversion system and field formatting system

4. Object to XML mapping functionality (OXM) moved from Spring Web Services project

5. Comprehensive REST support

6. @MVC additions

7. Declarative model validation

8. Early support for Java EE 6

9. Embedded database support
                

No comments:

Post a Comment

AWS Services

      1.         Identity Access Management (IAM): Used to control Identity (who) Access (what AWS resources).                   1....